Changeset 1291
- Timestamp:
- 08/28/06 15:34:49
- Files:
-
- trunk/cherrypy/_cptools.py (modified) (1 diff)
- trunk/cherrypy/lib/caching.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/_cptools.py
r1280 r1291 253 253 254 254 255 class CachingTool :255 class CachingTool(Tool): 256 256 """Caching Tool for CherryPy.""" 257 257 258 258 def __init__(self): 259 self._setup = _caching._setup 260 self.__call__ = _caching.enable 259 self._point = 'before_main' 260 self.callable = _caching.get 261 self._name = 'caching' 262 self.__doc__ = self.callable.__doc__ 263 setargs(self, self.callable) 264 265 def _wrapper(self): 266 request = cherrypy.request 267 if _caching.get(): 268 request.handler = None 269 else: 270 # Note the devious technique here of adding hooks on the fly 271 request.hooks.attach('before_finalize', _caching.tee_output) 272 273 def _setup(self): 274 """Hook caching into cherrypy.request using the given conf.""" 275 conf = self._merged_args() 276 cherrypy.request.hooks.attach('before_main', self._wrapper, **conf) 277 261 278 262 279 trunk/cherrypy/lib/caching.py
r1288 r1291 85 85 86 86 87 def init(cache_class=None): 88 if cache_class is None: 89 cache_class = MemoryCache 90 cherrypy._cache = cache_class() 91 92 def get(): 87 def get(invalid_methods=("POST", "PUT", "DELETE"), cache_class=MemoryCache): 88 """Try to obtain cached output. If fresh enough, raise HTTPError(304).""" 89 if not hasattr(cherrypy, "_cache"): 90 cherrypy._cache = cache_class() 91 93 92 request = cherrypy.request 93 94 94 # Ignore POST, PUT, DELETE. 95 95 # See http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.10. 96 invalid = request.config.get("tools.caching.invalid_methods", 97 ("POST", "PUT", "DELETE")) 98 if request.method in invalid: 96 if request.method in invalid_methods: 99 97 request.cached = c = False 100 98 else: … … 121 119 return c 122 120 121 123 122 def tee_output(): 124 123 if cherrypy.request.cached: … … 141 140 response.body = tee(response.body) 142 141 143 144 # CherryPy interfaces. Pick one.145 146 def enable(**kwargs):147 """Compile-time decorator (turn on the tool in config)."""148 def wrapper(f):149 if not hasattr(f, "_cp_config"):150 f._cp_config = {}151 f._cp_config["tools.caching.on"] = True152 for k, v in kwargs.iteritems():153 f._cp_config["tools.caching." + k] = v154 return f155 return wrapper156 157 def _wrapper():158 if get():159 cherrypy.request.handler = None160 else:161 # Note the devious technique here of adding hooks on the fly162 cherrypy.request.hooks.attach('before_finalize', tee_output)163 164 def _setup():165 """Hook caching into cherrypy.request using the given conf."""166 conf = cherrypy.request.toolmap.get("caching", {})167 if not getattr(cherrypy, "_cache", None):168 init(conf.get("class", None))169 cherrypy.request.hooks.attach('before_main', _wrapper)170 142 171 143 def expires(secs=0, force=False):

