Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

realms: Add caching to the get_seat_count calculation for upload limit.

Mateusz Mandera4a2a9176c278
diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.py
index f3bed6883ba..fb45f2e13d5 100644
--- a/corporate/lib/stripe.py
+++ b/corporate/lib/stripe.py
@@ -52,6 +52,7 @@ from corporate.models import (
get_customer_by_remote_realm,
get_customer_by_remote_server,
)
+from zerver.lib.cache import cache_with_key, get_realm_seat_count_cache_key
from zerver.lib.exceptions import JsonableError
from zerver.lib.logging_util import log_to_file
from zerver.lib.send_email import (
@@ -155,6 +156,14 @@ def get_latest_seat_count(realm: Realm) -> int:
return get_seat_count(realm, extra_non_guests_count=0, extra_guests_count=0)
+@cache_with_key(lambda realm: get_realm_seat_count_cache_key(realm.id), timeout=3600 * 24)
+def get_cached_seat_count(realm: Realm) -> int:
+ # This is a cache value we're intentionally okay with not invalidating.
+ # All that means is that this value will lag up to 24 hours before getting updated.
+ # We use this for calculating the uploaded files storage limit for paid Cloud organizations.
+ return get_latest_seat_count(realm)
+
+
def get_seat_count(
realm: Realm, extra_non_guests_count: int = 0, extra_guests_count: int = 0
) -> int:
diff --git a/zerver/lib/cache.py b/zerver/lib/cache.py
index 0200718ffdd..52d8c9934ce 100644
--- a/zerver/lib/cache.py
+++ b/zerver/lib/cache.py
@@ -486,6 +486,10 @@ def get_realm_used_upload_space_cache_key(realm_id: int) -> str:
return f"realm_used_upload_space:{realm_id}"
+def get_realm_seat_count_cache_key(realm_id: int) -> str:
+ return f"realm_seat_count:{realm_id}"
+
+
def active_user_ids_cache_key(realm_id: int) -> str:
return f"active_user_ids:{realm_id}"
diff --git a/zerver/models/realms.py b/zerver/models/realms.py
index 8689cb153e5..9af0ec4bd76 100644
--- a/zerver/models/realms.py
+++ b/zerver/models/realms.py
@@ -847,11 +847,11 @@ class Realm(models.Model): # type: ignore[django-manager-missing] # django-stub
elif plan_type == Realm.PLAN_TYPE_STANDARD_FREE:
return Realm.UPLOAD_QUOTA_STANDARD_FREE
elif plan_type in [Realm.PLAN_TYPE_STANDARD, Realm.PLAN_TYPE_PLUS]:
- from corporate.lib.stripe import get_seat_count
+ from corporate.lib.stripe import get_cached_seat_count
# Paying customers with few users should get a reasonable minimum quota.
return max(
- get_seat_count(self) * settings.UPLOAD_QUOTA_PER_USER_GB,
+ get_cached_seat_count(self) * settings.UPLOAD_QUOTA_PER_USER_GB,
Realm.UPLOAD_QUOTA_STANDARD_FREE,
)
else: