Skip to content
This repository was archived by the owner on Mar 15, 2018. It is now read-only.

Commit f0813d5

Browse files
Allen Shortwraithan
authored andcommitted
replace redis with memcache for Spam/GroupedRating
1 parent f52b6fa commit f0813d5

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

apps/reviews/models.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
from django.conf import settings
66
from django.db import models
7+
from django.core.cache import cache
78

89
import bleach
9-
import redisutils
1010
from celeryutils import task
1111
from 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

178173
class 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

apps/reviews/tests/test_models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ def test_get_none(self):
4242
def test_set(self):
4343
eq_(GroupedRating.get(1865), None)
4444
GroupedRating.set(1865)
45-
eq_(GroupedRating.get(1865), [[1, 0], [2, 0], [3, 0], [4, 1], [5, 0]])
45+
eq_(GroupedRating.get(1865), [(1, 0), (2, 0), (3, 0), (4, 1), (5, 0)])
4646

4747
def test_cron(self):
4848
eq_(GroupedRating.get(1865), None)
4949
tasks.addon_grouped_rating(1865)
50-
eq_(GroupedRating.get(1865), [[1, 0], [2, 0], [3, 0], [4, 1], [5, 0]])
50+
eq_(GroupedRating.get(1865), [(1, 0), (2, 0), (3, 0), (4, 1), (5, 0)])
5151

5252

5353
class TestSpamTest(amo.tests.TestCase):

0 commit comments

Comments
 (0)