1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
import redis
import pickle
import sys
# load some django settings here some day
class StorageError(Exception):
pass
def storage_error(typ):
return type('Storage%sError' % typ.__name__, (typ, StorageError), {})
def raise_storage_error():
typ, value, trace = sys.exc_info()
raise storage_error(typ), value, trace
class Storage:
key_prefix = 'sublab_monitor.'
def __init__(self, app_prefix):
self.key_prefix += '%s.' % app_prefix
try:
self.redis = redis.StrictRedis()
except redis.RedisError:
raise_storage_error()
def key(self, key):
return '%s%s' % (self.key_prefix, key)
def get(self, key):
lookup_key = self.key(key)
try:
entry = self.redis.get(lookup_key)
except redis.RedisError:
raise_storage_error()
if entry is None:
return None
try:
return pickle.loads(entry)
except pickle.PickleError:
raise_storage_error()
def set(self, key, value):
lookup_key = self.key(key)
try:
entry = pickle.dumps(value)
except pickle.PickleError:
raise_storage_error()
try:
self.redis.set(lookup_key, entry)
except redis.RedisError:
raise_storage_error()
|