summaryrefslogtreecommitdiff
path: root/accounts
diff options
context:
space:
mode:
authorAdam Zapletal <adamzap@gmail.com>2025-06-12 02:30:54 -0500
committerGitHub <noreply@github.com>2025-06-12 09:30:54 +0200
commit3b359fc0566eeaaf1f80b59ac0e4a949c75b6413 (patch)
tree090d5b5c8faaaf077f42dc4f631d1af44bc50be2 /accounts
parent5afbcb075cc46f36b47bc72ba63993ba1ac93468 (diff)
Simplified cache usage in `accounts` view helper (#2089)
Importing `cache` instead of `caches` uses the default cache.
Diffstat (limited to 'accounts')
-rw-r--r--accounts/tests.py20
-rw-r--r--accounts/views.py7
2 files changed, 23 insertions, 4 deletions
diff --git a/accounts/tests.py b/accounts/tests.py
index bc6c48e5..65af63cf 100644
--- a/accounts/tests.py
+++ b/accounts/tests.py
@@ -1,4 +1,7 @@
+import hashlib
+
from django.contrib.auth.models import User
+from django.core.cache import cache
from django.test import TestCase, override_settings
from django_hosts.resolvers import reverse
@@ -152,6 +155,23 @@ class UserProfileTests(TracDBCreateDatabaseMixin, TestCase):
response = self.client.get(self.user1_url)
self.assertContains(response, "New tickets triaged: 1.")
+ @override_settings(
+ CACHES={
+ "default": {
+ "BACKEND": "django.core.cache.backends.locmem.LocMemCache",
+ "LOCATION": "unique-snowflake",
+ }
+ }
+ )
+ def test_caches_trac_stats(self):
+ key = "user_vital_status:%s" % hashlib.md5(b"user1").hexdigest()
+
+ self.assertIsNone(cache.get(key))
+
+ self.client.get(self.user1_url)
+
+ self.assertIsNotNone(cache.get(key))
+
class ViewsTests(TestCase):
diff --git a/accounts/views.py b/accounts/views.py
index bbdf888d..80cfffce 100644
--- a/accounts/views.py
+++ b/accounts/views.py
@@ -2,7 +2,7 @@ import hashlib
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
-from django.core.cache import caches
+from django.core.cache import cache
from django.shortcuts import get_object_or_404, redirect, render
from tracdb import stats as trac_stats
@@ -35,10 +35,9 @@ def edit_profile(request):
def get_user_stats(user):
- c = caches["default"]
username = user.username.encode("ascii", "ignore")
key = "user_vital_status:%s" % hashlib.md5(username).hexdigest()
- info = c.get(key)
+ info = cache.get(key)
if info is None:
info = trac_stats.get_user_stats(user.username)
# Hide any stat with a value = 0 so that we don't accidentally insult
@@ -46,5 +45,5 @@ def get_user_stats(user):
for k, v in list(info.items()):
if v.count == 0:
info.pop(k)
- c.set(key, info, 60 * 60)
+ cache.set(key, info, 60 * 60)
return info