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

Changeset 1667

Show
Ignore:
Timestamp:
06/16/07 18:09:43
Author:
fumanchu
Message:

Final fix for #662 (error on configuration directive tools.caching.delay/maxobjects/maxsize). Trunk (3.1) now forces most caching config to apply site-wide. Also changed MemoryCache?.key to MemoryCache?.key().

Files:

Legend:

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

    r1659 r1667  
    426426    for the current request path (minus the querystring) by passing no args. 
    427427    If you call url(qs=cherrypy.request.query_string), you should get the 
    428     original browser URL (assuming no Internal redirections). 
     428    original browser URL (assuming no internal redirections). 
    429429     
    430430    If relative is False (the default), the output will be an absolute URL 
  • trunk/cherrypy/_cptools.py

    r1663 r1667  
    338338    """Caching Tool for CherryPy.""" 
    339339     
    340     def _wrapper(self, **kwargs): 
     340    def _wrapper(self, invalid_methods=("POST", "PUT", "DELETE"), **kwargs): 
    341341        request = cherrypy.request 
    342         if _caching.get(**kwargs): 
     342         
     343        if not hasattr(cherrypy, "_cache"): 
     344            # Make a process-wide Cache object. 
     345            cherrypy._cache = kwargs.pop("cache_class", _caching.MemoryCache)() 
     346             
     347            # Take all remaining kwargs and set them on the Cache object. 
     348            for k, v in kwargs.iteritems(): 
     349                setattr(cherrypy._cache, k, v) 
     350         
     351        if _caching.get(invalid_methods=invalid_methods): 
    343352            request.handler = None 
    344353        else: 
  • trunk/cherrypy/lib/caching.py

    r1594 r1667  
    88 
    99class MemoryCache: 
     10     
     11    maxobjects = 1000 
     12    maxobj_size = 100000 
     13    maxsize = 10000000 
     14    delay = 600 
    1015     
    1116    def __init__(self): 
     
    2732        self.cursize = 0 
    2833     
    29     def _key(self): 
    30         request = cherrypy.request 
    31         return request.config.get("tools.caching.key", cherrypy.url(qs=request.query_string)) 
    32     key = property(_key) 
     34    def key(self): 
     35        return cherrypy.url(qs=cherrypy.request.query_string) 
    3336     
    3437    def expire_cache(self): 
     
    5558        """Return the object if in the cache, else None.""" 
    5659        self.tot_gets += 1 
    57         cache_item = self.cache.get(self.key, None) 
     60        cache_item = self.cache.get(self.key(), None) 
    5861        if cache_item: 
    5962            self.tot_hist += 1 
     
    6366     
    6467    def put(self, obj): 
    65         conf = cherrypy.request.config.get 
    66          
    67         if len(self.cache) < conf("tools.caching.maxobjects", 1000): 
     68        if len(self.cache) < self.maxobjects: 
    6869            # Size check no longer includes header length 
    6970            obj_size = len(obj[2]) 
    70             maxobj_size = conf("tools.caching.maxobj_size", 100000) 
    71              
    7271            total_size = self.cursize + obj_size 
    73             maxsize = conf("tools.caching.maxsize", 10000000) 
    7472             
    7573            # checks if there's space for the object 
    76             if (obj_size < maxobj_size and total_size < maxsize): 
     74            if (obj_size < self.maxobj_size and total_size < self.maxsize): 
    7775                # add to the expirations list and cache 
    78                 expiration_time = cherrypy.response.time + conf("tools.caching.delay", 600) 
    79                 obj_key = self.key 
     76                expiration_time = cherrypy.response.time + self.delay 
     77                obj_key = self.key() 
    8078                bucket = self.expirations.setdefault(expiration_time, []) 
    8179                bucket.append((obj_size, obj_key)) 
     
    8583     
    8684    def delete(self): 
    87         self.cache.pop(self.key
    88  
    89  
    90 def get(invalid_methods=("POST", "PUT", "DELETE"), cache_class=MemoryCache): 
     85        self.cache.pop(self.key()
     86 
     87 
     88def get(invalid_methods=("POST", "PUT", "DELETE")): 
    9189    """Try to obtain cached output. If fresh enough, raise HTTPError(304). 
    9290     
     
    111109        * returns False 
    112110    """ 
    113     if not hasattr(cherrypy, "_cache"): 
    114         cherrypy._cache = cache_class() 
    115      
    116111    request = cherrypy.request 
    117112     
     
    151146 
    152147def tee_output(): 
    153     response = cherrypy.response 
    154     output = [] 
    155148    def tee(body): 
    156149        """Tee response.body into a list.""" 
     150        output = [] 
    157151        for chunk in body: 
    158152            output.append(chunk) 
    159153            yield chunk 
     154         
    160155        # Might as well do this here; why cache if the body isn't consumed? 
    161156        if response.headers.get('Pragma', None) != 'no-cache': 
    162157            # save the cache data 
    163             body = ''.join([chunk for chunk in output]
     158            body = ''.join(output
    164159            cherrypy._cache.put((response.status, response.headers or {}, 
    165160                                 body, response.time)) 
     161     
     162    response = cherrypy.response 
    166163    response.body = tee(response.body) 
    167164 

Hosted by WebFaction

Log in as guest/cpguest to create tickets