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

Changeset 1291

Show
Ignore:
Timestamp:
08/28/06 15:34:49
Author:
fumanchu
Message:

Reorganized the caching tool code.

Files:

Legend:

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

    r1280 r1291  
    253253 
    254254 
    255 class CachingTool
     255class CachingTool(Tool)
    256256    """Caching Tool for CherryPy.""" 
    257257     
    258258    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 
    261278 
    262279 
  • trunk/cherrypy/lib/caching.py

    r1288 r1291  
    8585 
    8686 
    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(): 
     87def 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     
    9392    request = cherrypy.request 
     93     
    9494    # Ignore POST, PUT, DELETE. 
    9595    # 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: 
    9997        request.cached = c = False 
    10098    else: 
     
    121119    return c 
    122120 
     121 
    123122def tee_output(): 
    124123    if cherrypy.request.cached: 
     
    141140    response.body = tee(response.body) 
    142141 
    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"] = True 
    152         for k, v in kwargs.iteritems(): 
    153             f._cp_config["tools.caching." + k] = v 
    154         return f 
    155     return wrapper 
    156  
    157 def _wrapper(): 
    158     if get(): 
    159         cherrypy.request.handler = None 
    160     else: 
    161         # Note the devious technique here of adding hooks on the fly 
    162         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) 
    170142 
    171143def expires(secs=0, force=False): 

Hosted by WebFaction

Log in as guest/cpguest to create tickets