-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmemcache_decorator.py
More file actions
26 lines (24 loc) · 844 Bytes
/
memcache_decorator.py
File metadata and controls
26 lines (24 loc) · 844 Bytes
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
import hashlib
import memcache
import functools
memcache_servers = ['localhost:11211','localhost:11212','localhost:11213','localhost:11214']
def use_memcache(func):
cache = memcache.Client(memcache_servers)
func.get_stats = cache.get_stats
@functools.wraps(func)
def wrapped(*a, **kw):
h = hashlib.sha1()
h.update(func.__name__)
h.update(repr(a))
# go throught kw in an orderly manner to ensure
# that the hash stays the same for every call
for k in sorted(kw.iterkeys()):
h.update(k)
h.update(repr(kw[k])) # of course, if this is a dict, our clever scheme may not work
key = h.hexdigest()
ret = cache.get(key)
if ret is None:
ret = func(*a, **kw)
cache.set(key, ret)
return ret
return wrapped