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

Changeset 1495

Show
Ignore:
Timestamp:
12/09/06 16:48:50
Author:
fumanchu
Message:

Fixed bug in caching; invalid methods did not delete cached resource. See #509.

Files:

Legend:

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

    r1471 r1495  
    260260            request.handler = None 
    261261        else: 
    262             # Note the devious technique here of adding hooks on the fly 
    263             request.hooks.attach('before_finalize', _caching.tee_output, 
    264                                  priority = 90) 
     262            if request.cacheable: 
     263                # Note the devious technique here of adding hooks on the fly 
     264                request.hooks.attach('before_finalize', _caching.tee_output, 
     265                                     priority = 90) 
    265266    _wrapper.priority = 20 
    266267     
  • trunk/cherrypy/lib/caching.py

    r1443 r1495  
    8383                self.tot_puts += 1 
    8484                self.cursize = total_size 
     85     
     86    def delete(self): 
     87        self.cache.pop(self.key) 
    8588 
    8689 
     
    8891    """Try to obtain cached output. If fresh enough, raise HTTPError(304). 
    8992     
    90     If a cached copy exists: 
     93    If POST, PUT, or DELETE: 
     94        * invalidates (deletes) any cached response for this resource 
     95        * sets request.cached = False 
     96        * sets request.cacheable = False 
     97     
     98    else if a cached copy exists: 
    9199        * sets request.cached = True 
     100        * sets request.cacheable = False 
    92101        * sets response.headers to the cached values 
    93102        * checks the cached Last-Modified response header against the 
     
    99108    otherwise: 
    100109        * sets request.cached = False 
     110        * sets request.cacheable = True 
    101111        * returns False 
    102112    """ 
     
    106116    request = cherrypy.request 
    107117     
    108     # Ignore POST, PUT, DELETE
     118    # POST, PUT, DELETE should invalidate (delete) the cached copy
    109119    # See http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.10. 
    110120    if request.method in invalid_methods: 
     121        cherrypy._cache.delete() 
    111122        request.cached = False 
     123        request.cacheable = False 
    112124        return False 
    113125     
    114126    cache_data = cherrypy._cache.get() 
    115127    request.cached = c = bool(cache_data) 
     128    request.cacheable = not c 
    116129    if c: 
    117130        response = cherrypy.response 
     
    138151 
    139152def tee_output(): 
    140     if cherrypy.request.cached: 
    141         return 
    142      
    143153    response = cherrypy.response 
    144154    output = [] 
  • trunk/cherrypy/test/test_caching.py

    r1278 r1495  
    8181        self.getPage("/", method="POST") 
    8282        self.assertBody('visit #2') 
     83        # The previous request should have invalidated the cache, 
     84        # so this request will recalc the response. 
    8385        self.getPage("/", method="GET") 
    84         self.assertBody('visit #2') 
     86        self.assertBody('visit #3') 
     87        # ...but this request should get the cached copy. 
     88        self.getPage("/", method="GET") 
     89        self.assertBody('visit #3') 
    8590        self.getPage("/", method="DELETE") 
    86         self.assertBody('visit #3') 
    87  
     91        self.assertBody('visit #4') 
     92        # The previous request should have invalidated the cache, 
     93        # so this request will recalc the response. 
     94        self.getPage("/", method="GET") 
     95        self.assertBody('visit #5') 
     96     
    8897    def testExpiresTool(self): 
    8998         

Hosted by WebFaction

Log in as guest/cpguest to create tickets