File tree Expand file tree Collapse file tree 2 files changed +23
-4
lines changed Expand file tree Collapse file tree 2 files changed +23
-4
lines changed Original file line number Diff line number Diff line change
1
+ import hashlib
2
+
1
3
from django .contrib .auth .models import User
4
+ from django .core .cache import cache
2
5
from django .test import TestCase , override_settings
3
6
from django_hosts .resolvers import reverse
4
7
@@ -152,6 +155,23 @@ def test_stat_tickets_triaged_unaccepted_not_counted(self):
152
155
response = self .client .get (self .user1_url )
153
156
self .assertContains (response , "New tickets triaged: 1." )
154
157
158
+ @override_settings (
159
+ CACHES = {
160
+ "default" : {
161
+ "BACKEND" : "django.core.cache.backends.locmem.LocMemCache" ,
162
+ "LOCATION" : "unique-snowflake" ,
163
+ }
164
+ }
165
+ )
166
+ def test_caches_trac_stats (self ):
167
+ key = "user_vital_status:%s" % hashlib .md5 (b"user1" ).hexdigest ()
168
+
169
+ self .assertIsNone (cache .get (key ))
170
+
171
+ self .client .get (self .user1_url )
172
+
173
+ self .assertIsNotNone (cache .get (key ))
174
+
155
175
156
176
class ViewsTests (TestCase ):
157
177
Original file line number Diff line number Diff line change 2
2
3
3
from django .contrib .auth .decorators import login_required
4
4
from django .contrib .auth .models import User
5
- from django .core .cache import caches
5
+ from django .core .cache import cache
6
6
from django .shortcuts import get_object_or_404 , redirect , render
7
7
8
8
from tracdb import stats as trac_stats
@@ -35,16 +35,15 @@ def edit_profile(request):
35
35
36
36
37
37
def get_user_stats (user ):
38
- c = caches ["default" ]
39
38
username = user .username .encode ("ascii" , "ignore" )
40
39
key = "user_vital_status:%s" % hashlib .md5 (username ).hexdigest ()
41
- info = c .get (key )
40
+ info = cache .get (key )
42
41
if info is None :
43
42
info = trac_stats .get_user_stats (user .username )
44
43
# Hide any stat with a value = 0 so that we don't accidentally insult
45
44
# non-contributors.
46
45
for k , v in list (info .items ()):
47
46
if v .count == 0 :
48
47
info .pop (k )
49
- c .set (key , info , 60 * 60 )
48
+ cache .set (key , info , 60 * 60 )
50
49
return info
You can’t perform that action at this time.
0 commit comments