[CalendarServer-changes] [2457] CalendarServer/branches/unified-cache/twistedcaldav

source_changes at macosforge.org source_changes at macosforge.org
Fri May 23 09:58:37 PDT 2008


Revision: 2457
          http://trac.macosforge.org/projects/calendarserver/changeset/2457
Author:   dreid at apple.com
Date:     2008-05-23 09:58:36 -0700 (Fri, 23 May 2008)

Log Message:
-----------
Trap KeyError in _resourceNotInCache, use dicts instead of Headers instances in the memcache pickle, refactor key hashing.

Modified Paths:
--------------
    CalendarServer/branches/unified-cache/twistedcaldav/cache.py
    CalendarServer/branches/unified-cache/twistedcaldav/root.py
    CalendarServer/branches/unified-cache/twistedcaldav/test/test_cache.py

Modified: CalendarServer/branches/unified-cache/twistedcaldav/cache.py
===================================================================
--- CalendarServer/branches/unified-cache/twistedcaldav/cache.py	2008-05-23 16:56:31 UTC (rev 2456)
+++ CalendarServer/branches/unified-cache/twistedcaldav/cache.py	2008-05-23 16:58:36 UTC (rev 2457)
@@ -31,6 +31,7 @@
 from twisted.web2.dav import davxml
 from twisted.web2.dav.util import allDataFromStream
 from twisted.web2.http import HTTPError, Response
+from twisted.web2.http_headers import Headers
 from twisted.web2.stream import MemoryStream
 
 from twisted.web2.dav.xattrprops import xattrPropertyStore
@@ -75,9 +76,6 @@
 
 
     def _requestKey(self, request):
-        if hasattr(request, 'cacheKey'):
-            return succeed(request.cacheKey)
-
         def _getKey(requestBody):
             if requestBody is not None:
                 request.stream = MemoryStream(requestBody)
@@ -289,7 +287,12 @@
             response.stream = MemoryStream(responseBody)
             return response
 
-        d = self._requestKey(request)
+        if hasattr(request, 'cacheKey'):
+            request.cacheKey
+            d = succeed(request.cacheKey)
+        else:
+            d = self._requestKey(request)
+
         d.addCallback(self._getResponseBody, response)
         d.addCallback(_cacheResponse)
         return d
@@ -323,14 +326,46 @@
 
         return d.addCallback(_cacheProtocol)
 
+
+    def _hashedRequestKey(self, request):
+        def _hashKey(key):
+            oldkey = key
+            request.cacheKey = key = hashlib.md5(
+                ':'.join([str(t) for t in key])).hexdigest()
+            self.log_debug("hashing key for get: %r to %r" % (oldkey, key))
+            return request.cacheKey
+
+        d = self._requestKey(request)
+        d.addCallback(_hashKey)
+        return d
+
+
     def getResponseForRequest(self, request):
         def _checkTokens(curTokens, expectedTokens, (code, headers, body)):
-            if curTokens != expectedTokens:
+            if curTokens[0] != expectedTokens[0]:
+                self.log_debug(
+                    "Principal token doesn't match for %r: %r != %r" % (
+                        request.cacheKey,
+                        curTokens[0],
+                        expectedTokens[0]))
                 return None
 
-            return Response(code, headers=headers,
-                            stream=MemoryStream(body))
+            if curTokens[1] != expectedTokens[1]:
+                self.log_debug(
+                    "URI token doesn't match for %r: %r != %r" % (
+                        request.cacheKey,
+                        curTokens[1],
+                        expectedTokens[1]))
+                return None
 
+            r = Response(code,
+                         stream=MemoryStream(body))
+
+            for key, value in headers.iteritems():
+                r.headers.setRawHeaders(key, value)
+
+            return r
+
         def _unpickleResponse((flags, value), key):
             if value is None:
                 self.log_debug("Not in cache: %r" % (key,))
@@ -354,12 +389,9 @@
             return d1.addCallback(_unpickleResponse, key)
 
         def _getProtocol(key):
-            request.cacheKey = key = hashlib.md5(':'.join(
-                    [str(t) for t in key])).hexdigest()
-
             return self._getMemcacheProtocol().addCallback(_getCache, key)
 
-        d = self._requestKey(request)
+        d = self._hashedRequestKey(request)
         d.addCallback(_getProtocol)
         return d
 
@@ -371,8 +403,6 @@
                 lambda _: response)
 
         def _cacheResponse((key, responseBody)):
-            key = hashlib.md5(':'.join([str(t) for t in key])).hexdigest()
-
             principalURI = self._principalURI(request.authnUser)
 
             response.headers.removeHeader('date')
@@ -382,7 +412,7 @@
                 (self._tokenForURI(principalURI),
                  self._tokenForURI(request.uri),
                  (response.code,
-                  response.headers,
+                  dict(list(response.headers.getAllRawHeaders())),
                   responseBody)))
 
             d1 = self._getMemcacheProtocol()
@@ -390,8 +420,11 @@
 
             return d1
 
+        if hasattr(request, 'cacheKey'):
+            d = succeed(request.cacheKey)
+        else:
+            d = self._hashedRequestKey(request)
 
-        d = self._requestKey(request)
         d.addCallback(self._getResponseBody, response)
         d.addCallback(_cacheResponse)
         return d

Modified: CalendarServer/branches/unified-cache/twistedcaldav/root.py
===================================================================
--- CalendarServer/branches/unified-cache/twistedcaldav/root.py	2008-05-23 16:56:31 UTC (rev 2456)
+++ CalendarServer/branches/unified-cache/twistedcaldav/root.py	2008-05-23 16:58:36 UTC (rev 2457)
@@ -156,6 +156,7 @@
             return _CachedResponseResource(response), []
 
         def _resourceNotInCacheEb(failure):
+            failure.trap(KeyError)
             return super(RootResource, self).locateChild(request,segments)
 
         if request.method == 'PROPFIND' and not getattr(

Modified: CalendarServer/branches/unified-cache/twistedcaldav/test/test_cache.py
===================================================================
--- CalendarServer/branches/unified-cache/twistedcaldav/test/test_cache.py	2008-05-23 16:56:31 UTC (rev 2456)
+++ CalendarServer/branches/unified-cache/twistedcaldav/test/test_cache.py	2008-05-23 16:58:36 UTC (rev 2457)
@@ -350,7 +350,13 @@
                 hash('foobar'),
                 )])).hexdigest()
 
-        memcacheStub._cache[expected_key] = (0, cPickle.dumps((
-            'principalToken0', 'uriToken0', self.expected_response)))
+        memcacheStub._cache[expected_key] = (
+            0, #flags
+            cPickle.dumps((
+            'principalToken0',
+            'uriToken0',
+            (self.expected_response[0],
+             dict(list(self.expected_response[1].getAllRawHeaders())),
+             self.expected_response[2]))))
 
         self.rc._memcacheProtocol = memcacheStub

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20080523/38ccc16c/attachment.htm 


More information about the calendarserver-changes mailing list