Changeset 1667
- Timestamp:
- 06/16/07 18:09:43
- Files:
-
- trunk/cherrypy/__init__.py (modified) (1 diff)
- trunk/cherrypy/_cptools.py (modified) (1 diff)
- trunk/cherrypy/lib/caching.py (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/__init__.py
r1659 r1667 426 426 for the current request path (minus the querystring) by passing no args. 427 427 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). 429 429 430 430 If relative is False (the default), the output will be an absolute URL trunk/cherrypy/_cptools.py
r1663 r1667 338 338 """Caching Tool for CherryPy.""" 339 339 340 def _wrapper(self, **kwargs):340 def _wrapper(self, invalid_methods=("POST", "PUT", "DELETE"), **kwargs): 341 341 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): 343 352 request.handler = None 344 353 else: trunk/cherrypy/lib/caching.py
r1594 r1667 8 8 9 9 class MemoryCache: 10 11 maxobjects = 1000 12 maxobj_size = 100000 13 maxsize = 10000000 14 delay = 600 10 15 11 16 def __init__(self): … … 27 32 self.cursize = 0 28 33 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) 33 36 34 37 def expire_cache(self): … … 55 58 """Return the object if in the cache, else None.""" 56 59 self.tot_gets += 1 57 cache_item = self.cache.get(self.key , None)60 cache_item = self.cache.get(self.key(), None) 58 61 if cache_item: 59 62 self.tot_hist += 1 … … 63 66 64 67 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: 68 69 # Size check no longer includes header length 69 70 obj_size = len(obj[2]) 70 maxobj_size = conf("tools.caching.maxobj_size", 100000)71 72 71 total_size = self.cursize + obj_size 73 maxsize = conf("tools.caching.maxsize", 10000000)74 72 75 73 # 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): 77 75 # 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() 80 78 bucket = self.expirations.setdefault(expiration_time, []) 81 79 bucket.append((obj_size, obj_key)) … … 85 83 86 84 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 88 def get(invalid_methods=("POST", "PUT", "DELETE")): 91 89 """Try to obtain cached output. If fresh enough, raise HTTPError(304). 92 90 … … 111 109 * returns False 112 110 """ 113 if not hasattr(cherrypy, "_cache"):114 cherrypy._cache = cache_class()115 116 111 request = cherrypy.request 117 112 … … 151 146 152 147 def tee_output(): 153 response = cherrypy.response154 output = []155 148 def tee(body): 156 149 """Tee response.body into a list.""" 150 output = [] 157 151 for chunk in body: 158 152 output.append(chunk) 159 153 yield chunk 154 160 155 # Might as well do this here; why cache if the body isn't consumed? 161 156 if response.headers.get('Pragma', None) != 'no-cache': 162 157 # save the cache data 163 body = ''.join( [chunk for chunk in output])158 body = ''.join(output) 164 159 cherrypy._cache.put((response.status, response.headers or {}, 165 160 body, response.time)) 161 162 response = cherrypy.response 166 163 response.body = tee(response.body) 167 164

