[Checkins] SVN: lovely.viewcache/trunk/src/lovely/viewcache/ Allow to make a cache entry depend on the current principal.

Jürgen Kartnaller juergen at kartnaller.at
Tue Feb 20 03:56:38 EST 2007


Log message for revision 72700:
  Allow to make a cache entry depend on the current principal.
  

Changed:
  U   lovely.viewcache/trunk/src/lovely/viewcache/README.txt
  U   lovely.viewcache/trunk/src/lovely/viewcache/ftesting.zcml
  U   lovely.viewcache/trunk/src/lovely/viewcache/view.py

-=-
Modified: lovely.viewcache/trunk/src/lovely/viewcache/README.txt
===================================================================
--- lovely.viewcache/trunk/src/lovely/viewcache/README.txt	2007-02-20 06:58:36 UTC (rev 72699)
+++ lovely.viewcache/trunk/src/lovely/viewcache/README.txt	2007-02-20 08:56:36 UTC (rev 72700)
@@ -212,6 +212,30 @@
   u'"content 1" is rendered 4 time(s)'
 
 
+Using dependOnPrincipal
+-----------------------
+
+It is possible to tell a cached view that it is has to be cached depending on
+the current principal.
+
+  >>> PrincipalCachedView = cachedView(View,
+  ...                                  dependencies = ('content',),
+  ...                                  dependOnPrincipal=True)
+  >>> view = PrincipalCachedView(content, request)
+  >>> view.__name__ = 'principalCachedView'
+  >>> view.__parent__ = content
+
+We must provide a principal for the reqeust.
+
+  >>> class DummyPrincipal(object):
+  ...     id = 'members.jukart'
+  >>> request.setPrincipal(DummyPrincipal())
+  >>> view()
+  u'"content 1" is rendered 5 time(s)'
+  >>> view()
+  u'"content 1" is rendered 5 time(s)'
+
+
 Cached Viewlets
 ---------------
 

Modified: lovely.viewcache/trunk/src/lovely/viewcache/ftesting.zcml
===================================================================
--- lovely.viewcache/trunk/src/lovely/viewcache/ftesting.zcml	2007-02-20 06:58:36 UTC (rev 72699)
+++ lovely.viewcache/trunk/src/lovely/viewcache/ftesting.zcml	2007-02-20 08:56:36 UTC (rev 72700)
@@ -3,7 +3,7 @@
            xmlns:meta="http://namespaces.zope.org/meta"
            i18n_domain="zope">
 
-  <include package="zope.app" />
+  <include package="zope.app.zcmlfiles" />
   <include package="zope.app.cache" />
   <include package="lovely.viewcache" />
  

Modified: lovely.viewcache/trunk/src/lovely/viewcache/view.py
===================================================================
--- lovely.viewcache/trunk/src/lovely/viewcache/view.py	2007-02-20 06:58:36 UTC (rev 72699)
+++ lovely.viewcache/trunk/src/lovely/viewcache/view.py	2007-02-20 08:56:36 UTC (rev 72700)
@@ -68,7 +68,10 @@
 
     def _getCachePath(self):
         url = absoluteURL(self, self.request)
-        return '/'.join(url.split('/')[3:])
+        result = '/'.join(url.split('/')[3:])
+        if self.dependOnPrincipal:
+            result += '/'+self.request.principal.id
+        return result
 
     def _getCachedResult(self):
         self.__cachedValue__ = None
@@ -111,12 +114,14 @@
         return self.__cachedValue__
 
 
-def cachedView(ViewClass, dependencies=(), minAge=0, maxAge=None):
+def cachedView(ViewClass, dependencies=(), minAge=0, maxAge=None,
+               dependOnPrincipal=False):
     """A factory to provide a view which is possibly in the view cache."""
     klass = ViewClass
     if ICacheableView not in interface.implementedBy(klass):
         attrs = dict(_staticCachingDeps=dependencies,
                      lifetime = (minAge, maxAge),
+                     dependOnPrincipal=dependOnPrincipal,
                      __name__=None,
                     )
         klass = type('<ViewCache for %s>'% ViewClass.__name__,
@@ -146,13 +151,15 @@
         return self.__cachedValue__
 
 
-def cachedViewlet(ViewClass, dependencies=(), minAge=0, maxAge=None):
+def cachedViewlet(ViewClass, dependencies=(), minAge=0, maxAge=None,
+                  dependOnPrincipal=False):
     """A factory to provide a viewlet which is possibly in the view cache."""
     klass = ViewClass
     if ICacheableView not in interface.implementedBy(klass):
         # our class is not cached, so make it a cached class
         attrs = dict(_staticCachingDeps=dependencies,
                      lifetime = (minAge, maxAge),
+                     dependOnPrincipal=dependOnPrincipal,
                      __name__=None,
                     )
         klass = type('<ViewletCache for %s>'% ViewClass.__name__,



More information about the Checkins mailing list