Browse files

Refactor the etag annotation lookup into a wrapper function in case s…

…omeone wants to get an etag value without polluting the request annotations (not sure if there is a usecase for that but it seemed polite). Also added the same annotation dance to the lastmodified lookup (just like to avoid doing the same calculation more than once in a request).

svn path=/plone.app.caching/trunk/; revision=33140
  • Loading branch information...
1 parent c389b73 commit e7c3115196fa6ce564f9053a4ba0d642be0ed944 newbery committed Jan 16, 2010
Showing with 55 additions and 26 deletions.
  1. +17 −17 plone/app/caching/operations/default.py
  2. +38 −9 plone/app/caching/operations/utils.py
View
34 plone/app/caching/operations/default.py
@@ -19,9 +19,9 @@
from plone.app.caching.operations.utils import cachedResponse
from plone.app.caching.operations.utils import notModified
-from plone.app.caching.operations.utils import getETag
+from plone.app.caching.operations.utils import getETagAnnotation
from plone.app.caching.operations.utils import getContext
-from plone.app.caching.operations.utils import getLastModified
+from plone.app.caching.operations.utils import getLastModifiedAnnotation
from plone.app.caching.operations.utils import fetchFromRAMCache
from plone.app.caching.operations.utils import isModified
@@ -61,7 +61,7 @@ def __init__(self, published, request):
def interceptResponse(self, rulename, response):
options = lookupOptions(CompositeViews, rulename)
- etag = getETag(self.published, self.request, options['etags'] or self.etags)
+ etag = getETagAnnotation(self.published, self.request, options['etags'] or self.etags)
if not isModified(self.request, etag=etag):
return notModified(self.published, self.request, response, etag=etag)
@@ -78,7 +78,7 @@ def interceptResponse(self, rulename, response):
def modifyResponse(self, rulename, response):
options = lookupOptions(CompositeViews, rulename)
- etag = getETag(self.published, self.request, options['etags'] or self.etags)
+ etag = getETagAnnotation(self.published, self.request, options['etags'] or self.etags)
cacheInBrowser(self.published, self.request, response, etag=etag)
@@ -112,7 +112,7 @@ def __init__(self, published, request):
def interceptResponse(self, rulename, response):
options = lookupOptions(ContentFeeds, rulename)
- etag = getETag(self.published, self.request, options['etags'] or self.etags)
+ etag = getETagAnnotation(self.published, self.request, options['etags'] or self.etags)
if not isModified(self.request, etag=etag):
return notModified(self.published, self.request, response, etag=etag)
@@ -129,7 +129,7 @@ def interceptResponse(self, rulename, response):
def modifyResponse(self, rulename, response):
options = lookupOptions(ContentFeeds, rulename)
- etag = getETag(self.published, self.request, options['etags'] or self.etags)
+ etag = getETagAnnotation(self.published, self.request, options['etags'] or self.etags)
cacheInBrowser(self.published, self.request, response, etag=etag)
@@ -167,7 +167,7 @@ def __init__(self, published, request):
def interceptResponse(self, rulename, response):
options = lookupOptions(ContentFeedsWithProxy, rulename)
- etag = getETag(self.published, self.request, options['etags'] or self.etags)
+ etag = getETagAnnotation(self.published, self.request, options['etags'] or self.etags)
if not isModified(self.request, etag=etag):
return notModified(self.published, self.request, response, etag=etag)
@@ -184,7 +184,7 @@ def interceptResponse(self, rulename, response):
def modifyResponse(self, rulename, response):
options = lookupOptions(ContentFeedsWithProxy, rulename)
- etag = getETag(self.published, self.request, options['etags'] or self.etags)
+ etag = getETagAnnotation(self.published, self.request, options['etags'] or self.etags)
smaxage = options['smaxage'] or self.smaxage
vary = options['vary'] or self.vary
@@ -217,15 +217,15 @@ def __init__(self, published, request):
def interceptResponse(self, rulename, response):
if visibleToRole(self.published, role='Anonymous'):
- lastModified = getLastModified(self.published)
+ lastModified = getLastModifiedAnnotation(self.published, self.request)
if not isModified(self.request, lastModified=lastModified):
return notModified(self.published, self.request, response, lastModified=lastModified)
return None
def modifyResponse(self, rulename, response):
if visibleToRole(self.published, role='Anonymous'):
- lastModified = getLastModified(self.published)
+ lastModified = getLastModifiedAnnotation(self.published, self.request)
cacheInBrowser(self.published, self.request, response, lastModified=lastModified)
else:
doNotCache(self.published, self.request, response)
@@ -265,7 +265,7 @@ def modifyResponse(self, rulename, response):
vary = options['vary'] or self.vary
if visibleToRole(self.published, role='Anonymous'):
- lastModified = getLastModified(self.published)
+ lastModified = getLastModifiedAnnotation(self.published, self.request)
cacheInProxy(self.published, self.request, response, smaxage=smaxage, lastModified=lastModified, vary=vary)
else:
doNotCache(self.published, self.request, response)
@@ -294,7 +294,7 @@ def __init__(self, published, request):
self.request = request
def interceptResponse(self, rulename, response):
- lastModified = getLastModified(self.published)
+ lastModified = getLastModifiedAnnotation(self.published, self.request)
if not isModified(self.request, lastModified=lastModified):
return notModified(self.published, self.request, response, lastModified=lastModified)
@@ -306,7 +306,7 @@ def modifyResponse(self, rulename, response):
maxage = options['maxage'] or self.maxage
vary = options['vary'] or self.vary
- lastModified = getLastModified(self.published)
+ lastModified = getLastModifiedAnnotation(self.published, self.request)
cacheInBrowserAndProxy(self.published, self.request, response, maxage=maxage, lastModified=lastModified, vary=vary)
class StableResources(object):
@@ -337,8 +337,8 @@ def __init__(self, published, request):
def interceptResponse(self, rulename, response):
options = lookupOptions(StableResources, rulename)
- lastModified = getLastModified(self.published)
- etag = getETag(self.published, self.request, options['etags'] or self.etags)
+ lastModified = getLastModifiedAnnotation(self.published, self.request)
+ etag = getETagAnnotation(self.published, self.request, options['etags'] or self.etags)
if not isModified(self.request, etag=etag, lastModified=lastModified):
return notModified(self.published, self.request, response, etag=etag, lastModified=lastModified)
@@ -349,10 +349,10 @@ def modifyResponse(self, rulename, response):
options = lookupOptions(StableResources, rulename)
maxage = options['maxage'] or self.maxage
- etag = getETag(self.published, self.request, options['etags'] or self.etags)
+ etag = getETagAnnotation(self.published, self.request, options['etags'] or self.etags)
vary = options['vary'] or self.vary
- lastModified = getLastModified(self.published)
+ lastModified = getLastModifiedAnnotation(self.published, self.request)
cacheInBrowserAndProxy(self.published, self.request, response,
maxage=maxage, etag=etag, lastModified=lastModified, vary=vary)
View
47 plone/app/caching/operations/utils.py
@@ -28,6 +28,8 @@
PAGE_CACHE_KEY = 'plone.app.caching.operations.ramcache'
PAGE_CACHE_ANNOTATION_KEY = 'plone.app.caching.operations.ramcache.key'
ETAG_ANNOTATION_KEY = 'plone.app.caching.operations.etag'
+LASTMODIFIED_ANNOTATION_KEY = 'plone.app.caching.operations.lastmodified'
+_marker = object()
logger = logging.getLogger('plone.app.caching')
@@ -346,6 +348,24 @@ def parseDateTime(str):
return dt
+def getLastModifiedAnnotation(published, request):
+ """Try to get the last modified date from a request annotation if available,
+ otherwise try to get it from published object
+ """
+
+ annotations = IAnnotations(request, None)
+ if annotations is not None:
+ dt = annotations.get(LASTMODIFIED_ANNOTATION_KEY, _marker)
+ if dt is not _marker:
+ return dt
+
+ dt = getLastModified(published)
+
+ if annotations is not None:
+ annotations[LASTMODIFIED_ANNOTATION_KEY] = dt
+
+ return dt
+
def getLastModified(published):
"""Get a last modified date or None.
@@ -381,6 +401,24 @@ def getExpiration(maxage):
else:
return now - datetime.timedelta(days=3650)
+def getETagAnnotation(published, request, keys=(), extraTokens=()):
+ """Try to get the ETag from a request annotation if available,
+ otherwise try to get it from published object
+ """
+
+ annotations = IAnnotations(request, None)
+ if annotations is not None:
+ etag = annotations.get(ETAG_ANNOTATION_KEY, _marker)
+ if etag is not _marker:
+ return etag
+
+ etag = getETag(published, request, keys, extraTokens)
+
+ if annotations is not None:
+ annotations[ETAG_ANNOTATION_KEY] = etag
+
+ return etag
+
def getETag(published, request, keys=(), extraTokens=()):
"""Calculate an ETag.
@@ -394,12 +432,6 @@ def getETag(published, request, keys=(), extraTokens=()):
All tokens will be concatenated into an ETag string, separated by pipes.
"""
- annotations = IAnnotations(request, None)
- if annotations is not None:
- etag = annotations.get(ETAG_ANNOTATION_KEY)
- if etag is not None:
- return etag
-
tokens = []
for key in keys:
component = queryMultiAdapter((published, request), IETagValue, name=key)
@@ -416,9 +448,6 @@ def getETag(published, request, keys=(), extraTokens=()):
etag = '|' + '|'.join(tokens)
etag = etag.replace(',', ';') # commas are bad in etags
- if annotations is not None:
- annotations[ETAG_ANNOTATION_KEY] = etag
-
return etag
def parseETags(text, allowWeak=True, _result=None):

0 comments on commit e7c3115

Please sign in to comment.