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)
| 262 | |
| 263 | |
| 264 | def 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')]: |
nothing calls this directly
no test coverage detected