-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
61 lines (48 loc) · 1.63 KB
/
util.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
import os
import functools
import gzip
import pickle
import StringIO
import webapp2
import jinja2
from google.appengine.api import memcache
jinja_environment = jinja2.Environment(extensions=['jinja2.ext.i18n', 'jinja2.ext.loopcontrols'], loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
class AppRequestHandler(webapp2.RequestHandler):
def render(self, page, values):
jinja_env = jinja_environment.get_template(page)
self.response.out.write(jinja_env.render(values))
def _gzip_pickle(data):
stream = StringIO.StringIO()
gzipper = gzip.GzipFile(mode='wb', fileobj=stream)
p = pickle.Pickler(gzipper)
p.dump(data)
gzipper.close()
return stream.getvalue()
def _gunzip_pickle(data):
stream = StringIO.StringIO(data)
gzipper = gzip.GzipFile(mode='rb', fileobj=stream)
p = pickle.Unpickler(gzipper)
data = p.load()
return data
def cached(time=1200):
"""
Source: https://gist.github.com/abahgat/1395810
Decorator that caches the result of a method for the specified time in seconds.
Use it as:
@cached(time=1200)
def functionToCache(arguments):
...
"""
def decorator(function):
@functools.wraps(function)
def wrapper(*args, **kwargs):
key = '%s%s%s' % (function.__name__, str(args), str(kwargs))
value = memcache.get(key)
if not value:
value = function(*args, **kwargs)
memcache.set(key, _gzip_pickle(value), time=time)
else:
value = _gunzip_pickle(value)
return value
return wrapper
return decorator