44
55from django .conf import settings
66from django .db import models
7+ from django .core .cache import cache
78
89import bleach
9- import redisutils
1010from celeryutils import task
1111from tower import ugettext_lazy as _
1212
@@ -145,23 +145,18 @@ class GroupedRating(object):
145145
146146 SELECT rating, COUNT(rating) FROM reviews where addon=:id
147147 """
148- # Non-critical data, so we always leave it in redis . Numbers are updated
148+ # Non-critical data, so we always leave it in memcache . Numbers are updated
149149 # when a new review comes in.
150150 prefix = 'addons:grouped:rating'
151151
152- @classmethod
153- def redis (cls ):
154- return redisutils .connections ['master' ]
155-
156152 @classmethod
157153 def key (cls , addon ):
158154 return '%s:%s' % (cls .prefix , addon )
159155
160156 @classmethod
161157 def get (cls , addon ):
162158 try :
163- d = cls .redis ().get (cls .key (addon ))
164- return json .loads (d ) if d else None
159+ return cache .get (cls .key (addon ))
165160 except Exception :
166161 # Don't worry about failures, especially timeouts.
167162 return
@@ -172,21 +167,29 @@ def set(cls, addon, using=None):
172167 .values_list ('rating' ).annotate (models .Count ('rating' )))
173168 counts = dict (q )
174169 ratings = [(rating , counts .get (rating , 0 )) for rating in range (1 , 6 )]
175- cls . redis (). set (cls .key (addon ), json . dumps ( ratings ) )
170+ cache . set (cls .key (addon ), ratings )
176171
177172
178173class Spam (object ):
179174
180- def __init__ (self ):
181- self .redis = redisutils .connections ['master' ]
182-
183175 def add (self , review , reason ):
184176 reason = 'amo:review:spam:%s' % reason
185- self .redis .sadd (reason , review .id )
186- self .redis .sadd ('amo:review:spam:reasons' , reason )
177+ try :
178+ reasonset = cache .get ('amo:review:spam:reasons' )
179+ except KeyError :
180+ reasonset = set ()
181+ try :
182+ idset = cache .get (reason )
183+ except KeyError :
184+ idset = set ()
185+ reasonset .add (reason )
186+ cache .set ('amo:review:spam:reasons' , reasonset )
187+ idset .add (review .id )
188+ cache .set (reason , idset )
189+
187190
188191 def reasons (self ):
189- return self . redis . smembers ('amo:review:spam:reasons' )
192+ return cache . get ('amo:review:spam:reasons' )
190193
191194
192195@task
0 commit comments