Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 1443

Show
Ignore:
Timestamp:
11/22/06 13:36:08
Author:
fumanchu
Message:

Tweaks to caching, mostly docs. Removed independent time.time() calculations in favor of response.time.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/lib/caching.py

    r1350 r1443  
    7676            if (obj_size < maxobj_size and total_size < maxsize): 
    7777                # add to the expirations list and cache 
    78                 expiration_time = time.time() + conf("tools.caching.delay", 600) 
     78                expiration_time = cherrypy.response.time + conf("tools.caching.delay", 600) 
    7979                obj_key = self.key 
    8080                bucket = self.expirations.setdefault(expiration_time, []) 
     
    8686 
    8787def get(invalid_methods=("POST", "PUT", "DELETE"), cache_class=MemoryCache): 
    88     """Try to obtain cached output. If fresh enough, raise HTTPError(304).""" 
     88    """Try to obtain cached output. If fresh enough, raise HTTPError(304). 
     89     
     90    If a cached copy exists: 
     91        * sets request.cached = True 
     92        * sets response.headers to the cached values 
     93        * checks the cached Last-Modified response header against the 
     94            current If-(Un)Modified-Since request headers; raises 304 
     95            if necessary. 
     96        * sets response.status and response.body to the cached values 
     97        * returns True 
     98     
     99    otherwise: 
     100        * sets request.cached = False 
     101        * returns False 
     102    """ 
    89103    if not hasattr(cherrypy, "_cache"): 
    90104        cherrypy._cache = cache_class() 
     
    95109    # See http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.10. 
    96110    if request.method in invalid_methods: 
    97         request.cached = c = False 
    98     else: 
    99         cache_data = cherrypy._cache.get() 
    100         request.cached = c = bool(cache_data) 
     111        request.cached = False 
     112        return False 
    101113     
     114    cache_data = cherrypy._cache.get() 
     115    request.cached = c = bool(cache_data) 
    102116    if c: 
    103117        response = cherrypy.response 
     
    105119         
    106120        # Add the required Age header 
    107         response.headers["Age"] = str(int(time.time() - create_time)) 
     121        response.headers["Age"] = str(int(response.time - create_time)) 
    108122         
    109123        try: 
     124            # Note that validate_since depends on a Last-Modified header; 
     125            # this was put into the cached copy, and should have been 
     126            # resurrected just above (response.headers = cache_data[1]). 
    110127            cptools.validate_since() 
    111128        except cherrypy.HTTPError, x: 
     
    135152            # save the cache data 
    136153            body = ''.join([chunk for chunk in output]) 
    137             create_time = time.time() 
    138154            cherrypy._cache.put((response.status, response.headers or {}, 
    139                                  body, create_time)) 
     155                                 body, response.time)) 
    140156    response.body = tee(response.body) 
    141157 

Hosted by WebFaction

Log in as guest/cpguest to create tickets