MCPcopy
hub / github.com/cherrypy/cherrypy / get

Function get

cherrypy/lib/caching.py:264–377  ·  view source on GitHub ↗

Try to obtain cached output. If fresh enough, raise HTTPError(304). If POST, PUT, or DELETE: * invalidates (deletes) any cached response for this resource * sets request.cached = False * sets request.cacheable = False else if a cached copy exists: * sets req

(invalid_methods=('POST', 'PUT', 'DELETE'), debug=False, **kwargs)

Source from the content-addressed store, hash-verified

262
263
264def get(invalid_methods=('POST', 'PUT', 'DELETE'), debug=False, **kwargs):
265 """Try to obtain cached output. If fresh enough, raise HTTPError(304).
266
267 If POST, PUT, or DELETE:
268 * invalidates (deletes) any cached response for this resource
269 * sets request.cached = False
270 * sets request.cacheable = False
271
272 else if a cached copy exists:
273 * sets request.cached = True
274 * sets request.cacheable = False
275 * sets response.headers to the cached values
276 * checks the cached Last-Modified response header against the
277 current If-(Un)Modified-Since request headers; raises 304
278 if necessary.
279 * sets response.status and response.body to the cached values
280 * returns True
281
282 otherwise:
283 * sets request.cached = False
284 * sets request.cacheable = True
285 * returns False
286 """
287 request = cherrypy.serving.request
288 response = cherrypy.serving.response
289
290 if not hasattr(cherrypy, '_cache'):
291 # Make a process-wide Cache object.
292 cherrypy._cache = kwargs.pop('cache_class', MemoryCache)()
293
294 # Take all remaining kwargs and set them on the Cache object.
295 for k, v in kwargs.items():
296 setattr(cherrypy._cache, k, v)
297 cherrypy._cache.debug = debug
298
299 # POST, PUT, DELETE should invalidate (delete) the cached copy.
300 # See http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.10.
301 if request.method in invalid_methods:
302 if debug:
303 cherrypy.log('request.method %r in invalid_methods %r' %
304 (request.method, invalid_methods), 'TOOLS.CACHING')
305 cherrypy._cache.delete()
306 request.cached = False
307 request.cacheable = False
308 return False
309
310 if 'no-cache' in [e.value for e in request.headers.elements('Pragma')]:
311 request.cached = False
312 request.cacheable = True
313 return False
314
315 cache_data = cherrypy._cache.get()
316 request.cached = bool(cache_data)
317 request.cacheable = not request.cached
318 if request.cached:
319 # Serve the cached copy.
320 max_age = cherrypy._cache.delay
321 for v in [e.value for e in request.headers.elements('Cache-Control')]:

Callers

nothing calls this directly

Calls 8

popMethod · 0.80
itemsMethod · 0.80
logMethod · 0.80
elementsMethod · 0.80
deleteMethod · 0.45
getMethod · 0.45
__setitem__Method · 0.45
__getitem__Method · 0.45

Tested by

no test coverage detected