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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
import datetime
import threading
import time
import cherrypy
from cherrypy.lib import cptools, http
class MemoryCache:
def __init__(self):
self.clear()
t = threading.Thread(target=self.expireCache, name='expireCache')
self.expirationThread = t
t.setDaemon(True)
t.start()
def clear(self):
"""Reset the cache to its initial, empty state."""
self.cache = {}
self.expirations = {}
self.totPuts = 0
self.totGets = 0
self.totHits = 0
self.totExpires = 0
self.totNonModified = 0
self.cursize = 0
def _key(self):
return cherrypy.request.config.get("tools.caching.key", cherrypy.request.browser_url)
key = property(_key)
def expireCache(self):
# expireCache runs in a separate thread which the servers are
# not aware of. It's possible that "time" will be set to None
# arbitrarily, so we check "while time" to avoid exceptions.
# See tickets #99 and #180 for more information.
while time:
now = time.time()
for expirationTime, objects in self.expirations.items():
if expirationTime <= now:
for objSize, objKey in objects:
try:
del self.cache[objKey]
self.totExpires += 1
self.cursize -= objSize
except KeyError:
# the key may have been deleted elsewhere
pass
del self.expirations[expirationTime]
time.sleep(0.1)
def get(self):
"""Return the object if in the cache, else None."""
self.totGets += 1
cacheItem = self.cache.get(self.key, None)
if cacheItem:
self.totHits += 1
return cacheItem
else:
return None
def put(self, obj):
conf = cherrypy.request.config.get
if len(self.cache) < conf("tools.caching.maxobjects", 1000):
# Size check no longer includes header length
objSize = len(obj[2])
maxobjsize = conf("tools.caching.maxobjsize", 100000)
totalSize = self.cursize + objSize
maxsize = conf("tools.caching.maxsize", 10000000)
# checks if there's space for the object
if (objSize < maxobjsize and totalSize < maxsize):
# add to the expirations list and cache
expirationTime = time.time() + conf("tools.caching.delay", 600)
objKey = self.key
bucket = self.expirations.setdefault(expirationTime, [])
bucket.append((objSize, objKey))
self.cache[objKey] = obj
self.totPuts += 1
self.cursize = totalSize
def init(cache_class=None):
if cache_class is None:
cache_class = MemoryCache
cherrypy._cache = cache_class()
def get():
# Ignore POST, PUT, DELETE.
# See http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.10.
invalid = cherrypy.config.get("tools.caching.invalid_methods",
("POST", "PUT", "DELETE"))
if cherrypy.request.method in invalid:
cherrypy.request.cached = c = False
else:
cacheData = cherrypy._cache.get()
cherrypy.request.cached = c = bool(cacheData)
if c:
response = cherrypy.response
s, response.headers, b, create_time = cacheData
# Add the required Age header
response.headers["Age"] = str(int(time.time() - create_time))
try:
cptools.validate_since()
except cherrypy.HTTPError, x:
if x.status == 304:
cherrypy._cache.totNonModified += 1
raise
# serve it & get out from the request
response.status = s
response.body = b
return c
def tee_output():
if cherrypy.request.cached:
return
response = cherrypy.response
output = []
def tee(body):
"""Tee response.body into a list."""
for chunk in body:
output.append(chunk)
yield chunk
# Might as well do this here; why cache if the body isn't consumed?
if response.headers.get('Pragma', None) != 'no-cache':
# save the cache data
body = ''.join([chunk for chunk in output])
create_time = time.time()
cherrypy._cache.put((response.status, response.headers or {},
body, create_time))
response.body = tee(response.body)
# CherryPy interfaces. Pick one.
def enable(**kwargs):
"""Compile-time decorator (turn on the tool in config)."""
def wrapper(f):
if not hasattr(f, "_cp_config"):
f._cp_config = {}
f._cp_config["tools.caching.on"] = True
for k, v in kwargs.iteritems():
f._cp_config["tools.caching." + k] = v
return f
return wrapper
def _wrapper():
if get():
cherrypy.request.handler = None
else:
# Note the devious technique here of adding hooks on the fly
cherrypy.request.hooks.attach('before_finalize', tee_output)
def _setup():
"""Hook caching into cherrypy.request using the given conf."""
conf = cherrypy.request.toolmap.get("caching", {})
if not getattr(cherrypy, "_cache", None):
init(conf.get("class", None))
cherrypy.request.hooks.attach('before_main', _wrapper)
def expires(secs=0, force=False):
"""Tool for influencing cache mechanisms using the 'Expires' header.
'secs' must be either an int or a datetime.timedelta, and indicates the
number of seconds between response.time and when the response should
expire. The 'Expires' header will be set to (response.time + secs).
If 'secs' is zero, the following "cache prevention" headers are also set:
'Pragma': 'no-cache'
'Cache-Control': 'no-cache'
If 'force' is False (the default), the following headers are checked:
'Etag', 'Last-Modified', 'Age', 'Expires'. If any are already present,
none of the "cache prevention" headers are set.
"""
if isinstance(secs, datetime.timedelta):
secs = (86400 * secs.days) + secs.seconds
expiry = http.HTTPDate(cherrypy.response.time + secs)
cptools.response_headers([("Expires", expiry)], force)
if secs == 0:
cacheable = False
if not force:
# some header names that indicate that the response can be cached
for indicator in ('Etag', 'Last-Modified', 'Age', 'Expires'):
if indicator in cherrypy.response.headers:
cacheable = True
break
if not cacheable:
cptools.response_headers([("Pragma", "no-cache")], force)
if cherrypy.request.version >= (1, 1):
cptools.response_headers([("Cache-Control", "no-cache")], force)
|