Skip to content

Commit 6f64346

Browse files
committed
Make redis an instance variable
This means connections will be restablished between requests
1 parent a4ca263 commit 6f64346

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

experiments/counters.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,27 @@
88
REDIS_PASSWORD = getattr(settings, 'EXPERIMENTS_REDIS_PASSWORD', None)
99
REDIS_EXPERIMENTS_DB = getattr(settings, 'EXPERIMENTS_REDIS_DB', 0)
1010

11-
r = redis.Redis(host=REDIS_HOST, port=REDIS_PORT, password=REDIS_PASSWORD, db=REDIS_EXPERIMENTS_DB)
12-
1311
COUNTER_CACHE_KEY = 'experiments:participants:%s'
1412
COUNTER_FREQ_CACHE_KEY = 'experiments:freq:%s'
1513

1614

1715
class Counters(object):
16+
def __init__(self):
17+
self.redis = redis.Redis(host=REDIS_HOST, port=REDIS_PORT, password=REDIS_PASSWORD, db=REDIS_EXPERIMENTS_DB)
18+
1819
def increment(self, key, participant_identifier, count=1):
1920
if count == 0:
2021
return
2122

2223
try:
2324
cache_key = COUNTER_CACHE_KEY % key
2425
freq_cache_key = COUNTER_FREQ_CACHE_KEY % key
25-
new_value = r.hincrby(cache_key, participant_identifier, count)
26+
new_value = self.redis.hincrby(cache_key, participant_identifier, count)
2627

2728
# Maintain histogram of per-user counts
2829
if new_value > count:
29-
r.hincrby(freq_cache_key, new_value - count, -1)
30-
r.hincrby(freq_cache_key, new_value, 1)
30+
self.redis.hincrby(freq_cache_key, new_value - count, -1)
31+
self.redis.hincrby(freq_cache_key, new_value, 1)
3132
except (ConnectionError, ResponseError):
3233
# Handle Redis failures gracefully
3334
pass
@@ -36,28 +37,28 @@ def clear(self, key, participant_identifier):
3637
try:
3738
# Remove the direct entry
3839
cache_key = COUNTER_CACHE_KEY % key
39-
pipe = r.pipeline()
40+
pipe = self.redis.pipeline()
4041
freq, _ = pipe.hget(cache_key, participant_identifier).hdel(cache_key, participant_identifier).execute()
4142

4243
# Remove from the histogram
4344
freq_cache_key = COUNTER_FREQ_CACHE_KEY % key
44-
r.hincrby(freq_cache_key, freq, -1)
45+
self.redis.hincrby(freq_cache_key, freq, -1)
4546
except (ConnectionError, ResponseError):
4647
# Handle Redis failures gracefully
4748
pass
4849

4950
def get(self, key):
5051
try:
5152
cache_key = COUNTER_CACHE_KEY % key
52-
return r.hlen(cache_key)
53+
return self.redis.hlen(cache_key)
5354
except (ConnectionError, ResponseError):
5455
# Handle Redis failures gracefully
5556
return 0
5657

5758
def get_frequency(self, key, participant_identifier):
5859
try:
5960
cache_key = COUNTER_CACHE_KEY % key
60-
freq = r.hget(cache_key, participant_identifier)
61+
freq = self.redis.hget(cache_key, participant_identifier)
6162
return int(freq) if freq else 0
6263
except (ConnectionError, ResponseError):
6364
# Handle Redis failures gracefully
@@ -70,17 +71,17 @@ def get_frequencies(self, key):
7071
# briefly be a negative result for some frequency count. We discard these
7172
# as they shouldn't really affect the result, and they are about to become
7273
# zero anyway.
73-
return dict((int(k), int(v)) for (k, v) in r.hgetall(freq_cache_key).items() if int(v) > 0)
74+
return dict((int(k), int(v)) for (k, v) in self.redis.hgetall(freq_cache_key).items() if int(v) > 0)
7475
except (ConnectionError, ResponseError):
7576
# Handle Redis failures gracefully
7677
return tuple()
7778

7879
def reset(self, key):
7980
try:
8081
cache_key = COUNTER_CACHE_KEY % key
81-
r.delete(cache_key)
82+
self.redis.delete(cache_key)
8283
freq_cache_key = COUNTER_FREQ_CACHE_KEY % key
83-
r.delete(freq_cache_key)
84+
self.redis.delete(freq_cache_key)
8485
return True
8586
except (ConnectionError, ResponseError):
8687
# Handle Redis failures gracefully
@@ -90,11 +91,11 @@ def reset_pattern(self, pattern_key):
9091
#similar to above, but can pass pattern as arg instead
9192
try:
9293
cache_key = COUNTER_CACHE_KEY % pattern_key
93-
for key in r.keys(cache_key):
94-
r.delete(key)
94+
for key in self.redis.keys(cache_key):
95+
self.redis.delete(key)
9596
freq_cache_key = COUNTER_FREQ_CACHE_KEY % pattern_key
96-
for key in r.keys(freq_cache_key):
97-
r.delete(key)
97+
for key in self.redis.keys(freq_cache_key):
98+
self.redis.delete(key)
9899
return True
99100
except (ConnectionError, ResponseError):
100101
# Handle Redis failures gracefully

0 commit comments

Comments
 (0)