-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
65 lines (48 loc) · 1.44 KB
/
utils.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
from google.appengine.api import memcache
import functools
import logging
import md5
def memoize(keyformat, time=60):
"""Decorator to memoize functions using memcache."""
def decorator(fxn):
def wrapper(*args, **kwargs):
key = (keyformat % args[0:keyformat.count('%')]) + str(kwargs)
#logging.info("MEMO KEY %s" % key)
m = md5.new()
m.update(key)
key_digest = m.hexdigest()
data = memcache.get(key_digest)
if data is not None:
#logging.info("Cache Hit for key %s" % key_digest)
return data
#logging.info("Cache Miss for key %s" % key_digest)
data = fxn(*args, **kwargs)
memcache.set(key_digest, data, time)
return data
return wrapper
return decorator
def selfmemoize(keyformat, time=60):
"""Decorator to memoize functions using memcache."""
def decorator(fxn):
def wrapper(self, *args, **kwargs):
ord = keyformat.count('%')
key = ""
if ord > 0:
key = keyformat % args[0:ord]
else:
key = keyformat
key = key + str(kwargs)
#logging.info("SELFMEMO %s" % key)
m = md5.new()
m.update(key)
key_digest = m.hexdigest()
data = memcache.get(key_digest)
if data is not None:
#logging.info("Cache Hit for key %s" % key_digest)
return data
#logging.info("Cache Miss for key %s" % key_digest)
data = fxn(self, *args, **kwargs)
memcache.set(key_digest, data, time)
return data
return wrapper
return decorator