Package cherrypy :: Package lib :: Module caching
[hide private]
[frames] | no frames]

Module caching

source code

CherryPy implements a simple caching system as a pluggable Tool. This tool tries to be an (in-process) HTTP/1.1-compliant cache. It's not quite there yet, but it's probably good enough for most sites.

In general, GET responses are cached (along with selecting headers) and, if another request arrives for the same resource, the caching Tool will return 304 Not Modified if possible, or serve the cached response otherwise. It also sets request.cached to True if serving a cached representation, and sets request.cacheable to False (so it doesn't get cached again).

If POST, PUT, or DELETE requests are made for a cached resource, they invalidate (delete) any cached response.

Usage

Configuration file example:

   [/]
   tools.caching.on = True
   tools.caching.delay = 3600

You may use a class other than the default :class:`MemoryCache<cherrypy.lib.caching.MemoryCache>` by supplying the config entry ``cache_class``; supply the full dotted name of the replacement class as the config value. It must implement the basic methods ``get``, ``put``, ``delete``, and ``clear``.

You may set any attribute, including overriding methods, on the cache instance by providing them in config. The above sets the :attr:`delay<cherrypy.lib.caching.MemoryCache.delay>` attribute, for example.

Classes [hide private]
  Cache
Base class for Cache implementations.
  AntiStampedeCache
A storage system for cached items which reduces stampede collisions.
  MemoryCache
An in-memory cache for varying response content.
Functions [hide private]
 
get(invalid_methods=('POST', 'PUT', 'DELETE'), debug=False, **kwargs)
Try to obtain cached output.
source code
 
tee_output()
Tee response output to cache storage.
source code
 
expires(secs=0, force=False, debug=False)
Tool for influencing cache mechanisms using the 'Expires' header.
source code
Variables [hide private]
  __package__ = 'cherrypy.lib'
Function Details [hide private]

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

source code 
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 request.cached = True
    * sets request.cacheable = False
    * sets response.headers to the cached values
    * checks the cached Last-Modified response header against the
      current If-(Un)Modified-Since request headers; raises 304
      if necessary.
    * sets response.status and response.body to the cached values
    * returns True

otherwise:
    * sets request.cached = False
    * sets request.cacheable = True
    * returns False

tee_output()

source code 

Tee response output to cache storage. Internal.

expires(secs=0, force=False, debug=False)

source code 
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 'Expires' header is set one year in the past, and
    the following "cache prevention" headers are also set:

        * Pragma: no-cache
        * Cache-Control': no-cache, must-revalidate

force
    If False, the following headers are checked:

        * Etag
        * Last-Modified
        * Age
        * Expires

    If any are already present, none of the above response headers are set.