Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pass user and shared params when checking for cache keys (backport #26402) #26403

Merged
merged 2 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions frappe/tests/test_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,31 @@ def calculate_area(radius: float) -> float:
calculate_area(radius=10)
# kwargs should hit cache too
self.assertEqual(function_call_count, 4)

def test_user_cache(self):
function_call_count = 0
PI = 3.1415
ENGINEERING_PI = _E = 3

@redis_cache(user=True)
def calculate_area(radius: float) -> float:
nonlocal function_call_count
PI_APPROX = ENGINEERING_PI if frappe.session.user == "Engineer" else PI
function_call_count += 1
return PI_APPROX * radius**2

with self.set_user("Engineer"):
self.assertEqual(calculate_area(1), ENGINEERING_PI)
self.assertEqual(function_call_count, 1)

with self.set_user("Mathematician"):
self.assertEqual(calculate_area(1), PI)
self.assertEqual(function_call_count, 2)

with self.set_user("Engineer"):
self.assertEqual(calculate_area(1), ENGINEERING_PI)
self.assertEqual(function_call_count, 2)

with self.set_user("Mathematician"):
self.assertEqual(calculate_area(1), PI)
self.assertEqual(function_call_count, 2)
18 changes: 9 additions & 9 deletions frappe/utils/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,13 @@ def site_cache_wrapper(*args, **kwargs):
return time_cache_wrapper


def redis_cache(ttl: int | None = 3600, user: str | bool | None = None) -> Callable:
def redis_cache(ttl: int | None = 3600, user: str | bool | None = None, shared: bool = False) -> Callable:
"""Decorator to cache method calls and its return values in Redis

args:
ttl: time to expiry in seconds, defaults to 1 hour
user: `true` should cache be specific to session user.
shared: `true` should cache be shared across sites
"""

def wrapper(func: Callable | None = None) -> Callable:
Expand All @@ -149,14 +150,13 @@ def clear_cache():

@wraps(func)
def redis_cache_wrapper(*args, **kwargs):
func_call_key = func_key + str(__generate_request_cache_key(args, kwargs))
if frappe.cache().exists(func_call_key):
return frappe.cache().get_value(func_call_key, user=user)
else:
val = func(*args, **kwargs)
ttl = getattr(func, "ttl", 3600)
frappe.cache().set_value(func_call_key, val, expires_in_sec=ttl, user=user)
return val
func_call_key = func_key + "::" + str(__generate_request_cache_key(args, kwargs))
if frappe.cache().exists(func_call_key, user=user, shared=shared):
return frappe.cache().get_value(func_call_key, user=user, shared=shared)
val = func(*args, **kwargs)
ttl = getattr(func, "ttl", 3600)
frappe.cache().set_value(func_call_key, val, expires_in_sec=ttl, user=user, shared=shared)
return val

return redis_cache_wrapper

Expand Down
Loading