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.

per_request_cache: Generalize to cache_for_current_request.

Sairam Bisoyia41bb0e6b6a4
diff --git a/docs/subsystems/caching.md b/docs/subsystems/caching.md
index b62b274e256..b3356818a63 100644
--- a/docs/subsystems/caching.md
+++ b/docs/subsystems/caching.md
@@ -240,7 +240,7 @@ We generally try to avoid in-process backend caching in Zulip's Django
codebase, because every Zulip production installation involves
multiple servers. We do have a few, however:
-- `@return_same_value_during_entire_request`: We use this decorator to
+- `@cache_for_current_request`: We use this decorator to
cache values in memory during the lifetime of a request. We use this
for linkifiers and display recipients. The middleware knows how to
flush the relevant in-memory caches at the start of a request.
diff --git a/zerver/lib/display_recipient.py b/zerver/lib/display_recipient.py
index 38f201060f9..e05a20300fb 100644
--- a/zerver/lib/display_recipient.py
+++ b/zerver/lib/display_recipient.py
@@ -9,7 +9,7 @@ from zerver.lib.cache import (
generic_bulk_cached_fetch,
single_user_display_recipient_cache_key,
)
-from zerver.lib.per_request_cache import return_same_value_during_entire_request
+from zerver.lib.per_request_cache import cache_for_current_request
from zerver.lib.types import DisplayRecipientT, UserDisplayRecipient
if TYPE_CHECKING:
@@ -209,7 +209,7 @@ def bulk_fetch_display_recipients(
return {**stream_display_recipients, **direct_message_display_recipients}
-@return_same_value_during_entire_request
+@cache_for_current_request
def get_display_recipient_by_id(
recipient_id: int, recipient_type: int, recipient_type_id: int | None
) -> list[UserDisplayRecipient]:
diff --git a/zerver/lib/per_request_cache.py b/zerver/lib/per_request_cache.py
index 9a759ad4009..de7b59dea9d 100644
--- a/zerver/lib/per_request_cache.py
+++ b/zerver/lib/per_request_cache.py
@@ -1,23 +1,30 @@
-from collections.abc import Callable
+import functools
+from collections.abc import Callable, Hashable
from typing import Any, TypeVar
ReturnT = TypeVar("ReturnT")
-FUNCTION_NAME_TO_PER_REQUEST_RESULT: dict[str, dict[int, Any]] = {}
+FUNCTION_NAME_TO_PER_REQUEST_RESULT: dict[str, dict[Any, Any]] = {}
-def return_same_value_during_entire_request(f: Callable[..., ReturnT]) -> Callable[..., ReturnT]:
+def cache_for_current_request(f: Callable[..., ReturnT]) -> Callable[..., ReturnT]:
+ """Cache the return value for each distinct set of arguments
+ during the current request. Unlike
+ @cache_for_current_request, this supports functions
+ with any hashable arguments, not just a single int key."""
cache_key = f.__name__
assert cache_key not in FUNCTION_NAME_TO_PER_REQUEST_RESULT
FUNCTION_NAME_TO_PER_REQUEST_RESULT[cache_key] = {}
- def wrapper(key: int, *args: Any) -> ReturnT:
- if key in FUNCTION_NAME_TO_PER_REQUEST_RESULT[cache_key]:
- return FUNCTION_NAME_TO_PER_REQUEST_RESULT[cache_key][key]
-
- result = f(key, *args)
- FUNCTION_NAME_TO_PER_REQUEST_RESULT[cache_key][key] = result
+ @functools.wraps(f)
+ def wrapper(*args: Hashable, **kwargs: Hashable) -> ReturnT:
+ cache = FUNCTION_NAME_TO_PER_REQUEST_RESULT[cache_key]
+ key = (args, frozenset(kwargs.items()))
+ if key in cache:
+ return cache[key]
+ result = f(*args, **kwargs)
+ cache[key] = result
return result
return wrapper
diff --git a/zerver/models/linkifiers.py b/zerver/models/linkifiers.py
index 92ea581e1f4..35efbe2d46c 100644
--- a/zerver/models/linkifiers.py
+++ b/zerver/models/linkifiers.py
@@ -10,10 +10,7 @@ from typing_extensions import override
from zerver.lib import cache
from zerver.lib.cache import cache_delete, cache_with_key
from zerver.lib.exceptions import MissingDependentParameterError
-from zerver.lib.per_request_cache import (
- flush_per_request_cache,
- return_same_value_during_entire_request,
-)
+from zerver.lib.per_request_cache import cache_for_current_request, flush_per_request_cache
from zerver.lib.types import LinkifierDict
from zerver.models.realms import Realm
@@ -174,7 +171,7 @@ def get_linkifiers_cache_key(realm_id: int) -> str:
return f"{cache.KEY_PREFIX}:all_linkifiers_for_realm:{realm_id}"
-@return_same_value_during_entire_request
+@cache_for_current_request
@cache_with_key(get_linkifiers_cache_key, timeout=3600 * 24 * 7)
def linkifiers_for_realm(realm_id: int) -> list[LinkifierDict]:
return [