parameters: (invalid_methods=('POST', 'PUT', 'DELETE'), debug=False, **kwargs)
return_statement: return request.cached
docstring: 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
docstring_summary: Try to obtain cached output. If fresh enough, raise HTTPError(304).
docstring_tokens: Try to obtain cached output . If fresh enough raise HTTPError ( 304 ) .
function: def get(invalid_methods=('POST', 'PUT', 'DELETE'), debug=False, **kwargs):
      """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
      """
      request = cherrypy.serving.request
      response = cherrypy.serving.response
  
      if not hasattr(cherrypy, '_cache'):
          # Make a process-wide Cache object.
          cherrypy._cache = kwargs.pop('cache_class', MemoryCache)()
  
          # Take all remaining kwargs and set them on the Cache object.
          for k, v in kwargs.items():
              setattr(cherrypy._cache, k, v)
          cherrypy._cache.debug = debug
  
      # POST, PUT, DELETE should invalidate (delete) the cached copy.
      # See http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.10.
      if request.method in invalid_methods:
          if debug:
              cherrypy.log('request.method %r in invalid_methods %r' %
                           (request.method, invalid_methods), 'TOOLS.CACHING')
          cherrypy._cache.delete()
          request.cached = False
          request.cacheable = False
          return False
  
      if 'no-cache' in [e.value for e in request.headers.elements('Pragma')]:
          request.cached = False
          request.cacheable = True
          return False
  
      cache_data = cherrypy._cache.get()
      request.cached = bool(cache_data)
      request.cacheable = not request.cached
      if request.cached:
          # Serve the cached copy.
          max_age = cherrypy._cache.delay
          for v in [e.value for e in request.headers.elements('Cache-Control')]:
              atoms = v.split('=', 1)
              directive = atoms.pop(0)
              if directive == 'max-age':
                  if len(atoms) != 1 or not atoms[0].isdigit():
                      raise cherrypy.HTTPError(
                          400, 'Invalid Cache-Control header')
                  max_age = int(atoms[0])
                  break
              elif directive == 'no-cache':
                  if debug:
                      cherrypy.log(
                          'Ignoring cache due to Cache-Control: no-cache',
                          'TOOLS.CACHING')
                  request.cached = False
                  request.cacheable = True
                  return False
  
          if debug:
              cherrypy.log('Reading response from cache', 'TOOLS.CACHING')
          s, h, b, create_time = cache_data
          age = int(response.time - create_time)
          if (age > max_age):
              if debug:
                  cherrypy.log('Ignoring cache due to age > %d' % max_age,
                               'TOOLS.CACHING')
              request.cached = False
              request.cacheable = True
              return False
  
          # Copy the response headers. See
          # https://github.com/cherrypy/cherrypy/issues/721.
          response.headers = rh = httputil.HeaderMap()
          for k in h:
              dict.__setitem__(rh, k, dict.__getitem__(h, k))
  
          # Add the required Age header
          response.headers['Age'] = str(age)
  
          try:
              # Note that validate_since depends on a Last-Modified header;
              # this was put into the cached copy, and should have been
              # resurrected just above (response.headers = cache_data[1]).
              cptools.validate_since()
          except cherrypy.HTTPRedirect:
              x = sys.exc_info()[1]
              if x.status == 304:
                  cherrypy._cache.tot_non_modified += 1
              raise
  
          # serve it & get out from the request
          response.status = s
          response.body = b
      else:
          if debug:
              cherrypy.log('request is not cached', 'TOOLS.CACHING')
      return request.cached
function_tokens: def get ( invalid_methods = ( 'POST' , 'PUT' , 'DELETE' ) , debug = False , * * kwargs ) : request = cherrypy . serving . request response = cherrypy . serving . response if not hasattr ( cherrypy , '_cache' ) : # Make a process-wide Cache object. cherrypy . _cache = kwargs . pop ( 'cache_class' , MemoryCache ) ( ) # Take all remaining kwargs and set them on the Cache object. for k , v in kwargs . items ( ) : setattr ( cherrypy . _cache , k , v ) cherrypy . _cache . debug = debug # POST, PUT, DELETE should invalidate (delete) the cached copy. # See http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.10. if request . method in invalid_methods : if debug : cherrypy . log ( 'request.method %r in invalid_methods %r' % ( request . method , invalid_methods ) , 'TOOLS.CACHING' ) cherrypy . _cache . delete ( ) request . cached = False request . cacheable = False return False if 'no-cache' in [ e . value for e in request . headers . elements ( 'Pragma' ) ] : request . cached = False request . cacheable = True return False cache_data = cherrypy . _cache . get ( ) request . cached = bool ( cache_data ) request . cacheable = not request . cached if request . cached : # Serve the cached copy. max_age = cherrypy . _cache . delay for v in [ e . value for e in request . headers . elements ( 'Cache-Control' ) ] : atoms = v . split ( '=' , 1 ) directive = atoms . pop ( 0 ) if directive == 'max-age' : if len ( atoms ) != 1 or not atoms [ 0 ] . isdigit ( ) : raise cherrypy . HTTPError ( 400 , 'Invalid Cache-Control header' ) max_age = int ( atoms [ 0 ] ) break elif directive == 'no-cache' : if debug : cherrypy . log ( 'Ignoring cache due to Cache-Control: no-cache' , 'TOOLS.CACHING' ) request . cached = False request . cacheable = True return False if debug : cherrypy . log ( 'Reading response from cache' , 'TOOLS.CACHING' ) s , h , b , create_time = cache_data age = int ( response . time - create_time ) if ( age > max_age ) : if debug : cherrypy . log ( 'Ignoring cache due to age > %d' % max_age , 'TOOLS.CACHING' ) request . cached = False request . cacheable = True return False # Copy the response headers. See # https://github.com/cherrypy/cherrypy/issues/721. response . headers = rh = httputil . HeaderMap ( ) for k in h : dict . __setitem__ ( rh , k , dict . __getitem__ ( h , k ) ) # Add the required Age header response . headers [ 'Age' ] = str ( age ) try : # Note that validate_since depends on a Last-Modified header; # this was put into the cached copy, and should have been # resurrected just above (response.headers = cache_data[1]). cptools . validate_since ( ) except cherrypy . HTTPRedirect : x = sys . exc_info ( ) [ 1 ] if x . status == 304 : cherrypy . _cache . tot_non_modified += 1 raise # serve it & get out from the request response . status = s response . body = b else : if debug : cherrypy . log ( 'request is not cached' , 'TOOLS.CACHING' ) return request . cached