Commits

Gael Pasgrimaud committed 857e30c

add some debug infos

Comments (0)

Files changed (1)

 # -*- coding: utf-8 -*-
 from beaker.cache import CacheManager
 from webob import Request, Response, exc
-from fnmatch import fnmatch
+import fnmatch
+import logging
 import re
 
-
+log = logging.getLogger(__name__)
 
 class Cache(object):
 
-    def __init__(self, app, paths, debug=False, **kwargs):
+    def __init__(self, app, paths, cache_qs=False, debug=False, **kwargs):
         self.application = app
         self.manager = CacheManager(**kwargs)
         self.paths = paths
+        log.debug('cache_paths: %s', paths)
+        self.cache_qs = cache_qs
         self.debug = debug
 
     def cache_key(self, req):
         """return the cache key. You may subclass it to take care of cookies or whatever
         """
-        return req.path_info
+        if self.cache_qs and req.query_string:
+            return '%s?%s' % (req.path_info, req.query_string)
+        else:
+            return req.path_info
 
     def must_cache(self, req):
         """return (namespace, cache_option) if the request should be cached
         """
         cache = False
         path_info = req.path_info
-        for path, opts in self.paths:
-            if isinstance(path, tuple):
-                if path[1].match(path_info):
-                    return path[0], opts
-            elif fnmatch(path_info, path):
-                return (path, opts)
+        for (namespace, match, _re), opts in self.paths:
+            result = _re.match(path_info) is not None
+            if result is match:
+                return namespace, opts
 
     def update_response(self, req, resp, opts):
         """update expire/cache headers
         try:
             keys = cache.namespace.keys()
         except Exception, e:
+            log.debug('purge("%s", **%s) -> OK', namespace, opts)
             cache.clear()
             values.append(namespace)
         else:
                 path_info = req.path_info
                 for k in keys:
                     if k.startswith(path_info):
+                        log.debug('purge("%s-%s", **%s) -> OK', namespace, k, opts)
                         del cache[k]
                         values.append(k)
             else:
                 self.update_response(req, resp, opts)
             if status in (200, 301, 302, 404):
                 # cache response
-                dbtype = self.manager.kwargs.get('type', opts.get('type'))
-                if dbtype and dbtype not in ('memory',):
-                    value = resp.headers, resp.body
-                else:
-                    value = resp
+                value = resp.headers, resp.body
                 try:
                     cache.set_value(key, value)
-                except TypeError:
-                    pass
+                except TypeError, e:
+                    log.debug('set_value("%s-%s", **%s) -> %r', namespace, key, opts, e)
+                else:
+                    log.debug('set_value("%s-%s", **%s) -> OK', namespace, key, opts)
             else:
                 return resp
+        else:
+            log.debug('get(%s-%s, **%s) -> OK', namespace, key, opts)
 
         if isinstance(resp, Response):
             return resp.conditional_response_app
         req = Request(environ)
         method = req.method
 
+        key = self.cache_key(req)
         must_cache = self.must_cache(req)
         if not must_cache:
             return self.application(environ, start_response)
 
-        key = self.cache_key(req)
         if not key:
             return self.application(environ, start_response)
 
 
 def deserialize_path(line):
     values = line.split()
-    path = values.pop(0)
+    namespace = path = values.pop(0)
+    if path.startswith('!'):
+        path = path[1:]
+        match = False
+    else:
+        match = True
     if path.startswith('^/'):
-        path = (path, re.compile(path))
+        path = (namespace, match, re.compile(path))
+    else:
+        path = (namespace, match, re.compile(fnmatch.translate(path)))
     cache_opts = {}
     for v in values:
-        k, v = v.split(':')
+        k, v = v.split(':', 1)
         if k == 'expire':
             v = int(v)
         cache_opts[k] = v