-
Notifications
You must be signed in to change notification settings - Fork 4
/
tyrant_cache.py
66 lines (55 loc) · 2.08 KB
/
tyrant_cache.py
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
64
65
66
try:
import cPickle as pickle
except ImportError:
import pickle
import pytyrant
import time
from django.core.cache.backends.base import BaseCache
from django.utils.encoding import smart_unicode, smart_str
class CacheClass(BaseCache):
def __init__(self, server, params):
"Connect to Tokyo Tyrant, and set up cache backend."
BaseCache.__init__(self, params)
host, port = server.split(':')
self._cache = pytyrant.Tyrant.open(host, int(port))
def _prepare_key(self, raw_key):
return smart_str(raw_key)
def add(self, key, value, timeout=0):
"Add a value to the cache. Returns ``True`` if the object was added, ``False`` if not."
try:
value = pickle.dumps(value)
self._cache.putkeep(self._prepare_key(key), value)
except pytyrant.TyrantError:
return False
return True
def get(self, key, default=None):
"Retrieve a value from the cache. Returns unpicked value if key is found, 'default' if not. "
try:
value = self._cache.get(self._prepare_key(key))
except pytyrant.TyrantError:
return default
value = pickle.loads(value)
if isinstance(value, basestring):
return smart_unicode(value)
else:
return value
def set(self, key, value, timeout=0):
"Persist a value to the cache."
value = pickle.dumps(value)
self._cache.put(self._prepare_key(key), value)
return True
def delete(self, key):
"Remove a key from the cache."
try:
self._cache.out(self._prepare_key(key))
except pytyrant.TyrantError: #Should not raise error if key doesn't exist
return None
def get_many(self, keys):
"Retrieve many keys."
many = self._cache.mget(keys)
return [{k:pickle.loads(v)} for k,v in many]
def flush(self, all_dbs=False):
self._cache.vanish()
def incr(self, key, delta=1):
"Atomically increment ``key`` by ``delta``."
return self._cache.addint(self._prepare_key(key), delta)