Esempio n. 1
0
 def innerfunc(self, *args):
     self.responded = False
     data = memcache.get(self.request.url)
     if _USE_MEMCACHE_ and can_use_cached_copy(self) and data:
         data_len = data.respond(self)
         self.responded = True
         logging.debug("Responded with %d bytes of memcached data for %s" % (data_len, self.request.url))
     else:
         method_args = [self]
         method_args.extend(args)
         retval = warh_method(*method_args)
         if can_use_cached_copy(self):
             if _USE_MEMCACHE_ and retval:
                 if isinstance(retval, HeadersAndContent):
                     if retval.content is not None:
                         memcache.set(self.request.url, retval, time=_CACHE_LIFE_)
                     else:
                         logging.debug("Response for '%s' is not cachable" % (self.request.url))
                 else:
                     # Assume just the content; no headers,
                     # HTTP status, etc
                     memcache.set(self.request.url, HeadersAndContent(content=retval), time=_CACHE_LIFE_)
             else:
                 logging.warning(
                     "Memcache disabled and/or no value " "returned from handler for '%s'" % (self.request.url)
                 )
Esempio n. 2
0
    def get(self, *dontcare):
        """
        Respond to request from memcache where available

        The inheriting class should do the following in
        its get() method:
        1. After calling this, check self.responded and
           return if it is True
        2. Call cache_response() with the rendered content.
           (NB the content would still have to be returned
           to the client)
        If you use the @memcachable decorator you get this (and
        more for free), you just need to return the content -
        ideally as a HeadersAndContent, but a string will
        be accepted.

        """
        self.responded = False
        if not can_use_cached_copy(self):
            return
        if _USE_MEMCACHE_:
            data = memcache.get(self.request.url)
            if data:
                data.respond(self)
                self.responded = True
                logging.debug("Responded with memcached page for %s" % (self.request.url))
 def cache_response(self, content, time=_CACHE_LIFE_):
     if content and len(content)>0 and can_use_cached_copy(self):
         logging.debug("Caching %d bytes of content for %s" % 
                       (len(content), self.request.url))
         memcache.set(self.request.url, content, time=time)
     else:
         logging.debug("NOT caching content for %s" %
                       (self.request.url))