Andrew Godwin avatar Andrew Godwin committed 415353b

More caching of methods (GET, REPORT), a --memcache option for quick testing, and better cache invalidation.

Comments (0)

Files changed (4)

heechee/webdav/__init__.py

     
     @cached_property
     def raw_body(self):
-        return self.input_stream.read(self.content_length)
+        if self.content_length:
+            return self.input_stream.read(self.content_length)
+        else:
+            return ""
 
     @cached_property
     def xml_body(self):
         """
         Uses the request and the repo to make a cache key.
         """
-        return "%s:%s:%s:%s" % (request.method, md5(self.repo.path).hexdigest(), str(self.repo.mtime()), md5(request.path + ":" + request.raw_body).hexdigest())
+        return "%s:%s:%s:%s" % (request.method, md5(self.repo.path).hexdigest(), str(self.repo.mtime()), md5(request.path + request.raw_body + str(request.headers)).hexdigest())
     
     def access_check(self, request, accesss_type):
         # Get their status
         help="Display debug output (warning: there's lots)")
     parser.add_option("--cache", action='store_true', default=False,
         help="Turns on simple disk caching.")
+    parser.add_option("--memcache", action='store_true', default=False,
+        help="Turns on simple memcache plugin.")
 
     options, args = parser.parse_args()
     if len(args) < 1:
         datefmt="%Y-%m-%d %H:%M:%S",
     )
     
-    if options.cache:
+    if options.memcache:
+        from heechee.webdav.caching.memcached import MemcachedCache
+        cache = MemcachedCache(["127.0.0.1"])
+    elif options.cache:
         cache = DiskCache()
-        #from heechee.webdav.caching.memcached import MemcachedCache
-        #cache = MemcachedCache(["127.0.0.1"])
     else:
         cache = NoopCache()
 

heechee/webdav/caching/memcached.py

+
+import logging
 
 try:
     import cmemcache as memcache
         self.timeout = timeout
     
     def __getitem__(self, key):
-        #print "cache TRY: %r" % key
         value = self._cache.get(key)
         if value is not None:
-            #print "cache HIT"
+            logging.debug("cache HIT: %r" % key)
             return value
         else:
+            logging.debug("cache MISS: %r" % key)
             raise KeyError("No such key %r" % key)
     
     def __setitem__(self, key, value):
-        #print "cache SET: %r (%i)" % (key, len(value))
+        logging.debug("cache SET: %r (%i)" % (key, len(value)))
         self._cache.add(key, value, self.timeout)

heechee/webdav/read.py

 
 class ReaderMixin(object):
     def GET(self, request):
+        
+        # Try getting a cached copy
+        cache_key = self._cache_key(request)
+        try:
+            return Response(self.cache[cache_key], status=200)
+        except KeyError:
+            pass
+        
         if request.path.startswith("/!svn/bc/"):
             parts = request.path.strip("/").split("/")
             revision = parts[2]
             if not file_:
                 raise NotFound()
 
-            return Response(file_.contents, status=200)
+            # Cache the response
+            response = file_.contents
+            self.cache[cache_key] = response
+            return Response(response, status=200)
         else:
             raise BadRequest()
         

heechee/webdav/report.py

     """
     
     def REPORT(self, request):
-        "i.e. SVN-wants-a-massive-diff time."        
+        "i.e. SVN-wants-a-massive-diff time."   
+
+        # Try getting a cached copy
+        cache_key = self._cache_key(request)
+        try:
+            return Response(self.cache[cache_key])
+        except KeyError:
+            pass        
+        
         # Can we use gzip?
         gzip_allowed = "gzip" in request.headers['Accept-Encoding']
         
                 else:
                     raise ValueError("Unexpected item in changes queue: %s" % node)
             
-            return Response(tostring(up_rep))
+            # Cache the response
+            response = tostring(up_rep)
+            self.cache[cache_key] = response
+            
+            return Response(response)
         
         elif tree.tag == SVN_NS + "log-report":
             assert "!svn" in request.path, "Log report on non-special directory!"
                 date = SubElement(item, SVN_NS+"date")
                 date.text = info['date'].strftime("%Y-%m-%dT%H:%M:%S.00000Z")
             
-            return Response(tostring(log_rep))
+            # Cache the response
+            response = tostring(log_rep)
+            self.cache[cache_key] = response
+            
+            return Response(response)
         
         else:
             return BadRequest()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.