-
Notifications
You must be signed in to change notification settings - Fork 14
/
cache.py
executable file
·108 lines (81 loc) · 2.68 KB
/
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# coding: utf-8
# author: https://github.com/cloverstd
# source code: https://gist.github.com/cloverstd/10712505
try:
import cPickle as pickle
except ImportError:
import pickle
try:
import hashlib
sha1 = hashlib.sha1
except ImportError:
import sha
sha1 = sha.new
import functools
def cache(expires=7200):
def _func(func):
@functools.wraps(func)
def wrapper(handler, *args, **kwargs):
handler.expires = expires
return func(handler, *args, **kwargs)
return wrapper
return _func
class CacheMixin(object):
@property
def cache(self):
return self.application.cache
def prepare(self):
super(CacheMixin, self).prepare()
key = self._generate_key(self.request)
print(key)
if self.cache.exists(self._prefix(key)):
rv = pickle.loads(self.cache.get(self._prefix(key)))
self.write_cache(rv)
self.finish()
def _generate_key(self, request):
key = pickle.dumps((request.path, request.arguments))
return sha1(key).hexdigest()
def _prefix(self, key):
return "Cache:%s" % key
def write_cache(self, chunk):
super(CacheMixin, self).write(chunk)
def write(self, chunk):
pickled = pickle.dumps(chunk)
key = self._generate_key(self.request)
if hasattr(self, "expires"):
self.cache.set(self._prefix(key), pickled, self.expires)
else:
self.cache.set(self._prefix(key), pickled)
super(CacheMixin, self).write(chunk)
class CacheBackend(object):
"""
The base Cache Backend class
"""
def get(self, key):
raise NotImplementedError
def set(self, key, value, timeout):
raise NotImplementedError
def delitem(self, key):
raise NotImplementedError
def exists(self, key):
raise NotImplementedError
class RedisCacheBackend(CacheBackend):
def __init__(self, redis_connection, **options):
self.options = dict(timeout=86400)
self.options.update(options)
self.redis = redis_connection
def get(self, key):
if self.exists(key):
return self.redis.get(key)
return None
def set(self, key, value, timeout=None):
self.redis.set(key, value)
if timeout:
self.redis.expire(key, timeout)
else:
self.redis.expire(key, self.options["timeout"])
def delitem(self, key):
self.redis.delete(key)
def exists(self, key):
print(key)
return bool(self.redis.exists(key))