[Checkins] SVN: z3c.image/trunk/src/z3c/image/proc/ invalidate cache on modification of images

Bernd Dorn bernd.dorn at lovelysystems.com
Thu Nov 16 03:25:54 EST 2006


Log message for revision 71144:
  invalidate cache on modification of images

Changed:
  U   z3c.image/trunk/src/z3c/image/proc/README.txt
  U   z3c.image/trunk/src/z3c/image/proc/adapter.py
  U   z3c.image/trunk/src/z3c/image/proc/configure.zcml
  U   z3c.image/trunk/src/z3c/image/proc/tests.py

-=-
Modified: z3c.image/trunk/src/z3c/image/proc/README.txt
===================================================================
--- z3c.image/trunk/src/z3c/image/proc/README.txt	2006-11-16 01:42:49 UTC (rev 71143)
+++ z3c.image/trunk/src/z3c/image/proc/README.txt	2006-11-16 08:25:53 UTC (rev 71144)
@@ -78,4 +78,65 @@
   >>> res.getImageSize()
   (80, 80)
 
+Cache Handling
+==============
 
+The adapter uses a RamCache instance to cache results.
+
+  >>> from z3c.image.proc.adapter import imgCache
+
+We have to take private methods to get to the objects, because the
+getStatistics implementation does not allow to get the size of
+unpickleable objects.
+
+  >>> sorted(imgCache._getStorage()._data.keys())
+  [<zope.app.file.image.Image object at ...>,
+   <zope.app.file.image.Image object at ...>,
+   <zope.app.file.image.Image object at ...>]
+
+To demonstrate caching we now invalidate first and process another.
+
+  >>> imgCache.invalidateAll()
+  >>> sorted(imgCache._getStorage()._data.keys())
+  []
+  >>> image = testing.getTestImage('hiring.gif')
+  >>> pimg = IProcessableImage(image)
+  >>> pimg.rotate(90)
+  >>> res = pimg.process()
+  >>> res.getImageSize()
+  (183, 199)
+
+Now let us change the data of the image.
+
+  >>> import os
+  >>> path = os.path.join(testing.dataDir,'flower.jpg')
+  >>> image.data = file(path,'rb').read()
+  >>> image.getImageSize()
+  (103, 118)
+
+And now we do the same processing again
+
+  >>> pimg = IProcessableImage(image)
+  >>> pimg.rotate(90)
+  >>> res = pimg.process()
+  >>> res.getImageSize()
+  (183, 199)
+
+The cache is invalidated when a modified event is fired. To
+demonstrate this we need to set up event handling
+
+  >>> from zope.component import eventtesting
+  >>> eventtesting.setUp()
+  >>> from zope.lifecycleevent import ObjectModifiedEvent
+
+When we now fire the event, the result changes.
+
+  >>> from zope.event import notify
+  >>> notify(ObjectModifiedEvent(image))
+  >>> pimg = IProcessableImage(image)
+  >>> pimg.rotate(90)
+  >>> res = pimg.process()
+  >>> res.getImageSize()
+  (118, 103)
+
+

Modified: z3c.image/trunk/src/z3c/image/proc/adapter.py
===================================================================
--- z3c.image/trunk/src/z3c/image/proc/adapter.py	2006-11-16 01:42:49 UTC (rev 71143)
+++ z3c.image/trunk/src/z3c/image/proc/adapter.py	2006-11-16 08:25:53 UTC (rev 71144)
@@ -26,6 +26,10 @@
     maxEntries, ulimit))
 imgCache.maxEntries = maxEntries
 
+def invalidateCache(object, event):
+    imgCache.invalidate(object)
+    
+
 class ProcessableImage(object):
 
     component.adapts(IFile)

Modified: z3c.image/trunk/src/z3c/image/proc/configure.zcml
===================================================================
--- z3c.image/trunk/src/z3c/image/proc/configure.zcml	2006-11-16 01:42:49 UTC (rev 71143)
+++ z3c.image/trunk/src/z3c/image/proc/configure.zcml	2006-11-16 08:25:53 UTC (rev 71144)
@@ -18,5 +18,11 @@
   permission="zope.Public"
   class=".browser.ImageProcessorView"
   />
- 
+
+ <subscriber
+     for="zope.app.file.interfaces.IImage
+          zope.lifecycleevent.interfaces.IObjectModifiedEvent"
+     handler=".adapter.invalidateCache"
+     />
+
 </configure>

Modified: z3c.image/trunk/src/z3c/image/proc/tests.py
===================================================================
--- z3c.image/trunk/src/z3c/image/proc/tests.py	2006-11-16 01:42:49 UTC (rev 71143)
+++ z3c.image/trunk/src/z3c/image/proc/tests.py	2006-11-16 08:25:53 UTC (rev 71144)
@@ -3,20 +3,24 @@
 from zope.testing.doctestunit import DocFileSuite, DocTestSuite
 from zope.app.testing import setup
 from zope import component
+import adapter
+from zope.app.file.interfaces import IImage
+from zope.lifecycleevent.interfaces import IObjectModifiedEvent
 
 def setUp(test):
-    setup.placefulSetUp()
+    component.provideHandler(adapter.invalidateCache,
+                             adapts=(
+        IImage, IObjectModifiedEvent))
 
-
 def tearDown(test):
-    setup.placefulTearDown()
+    pass
 
-
 def test_suite():
     
     return unittest.TestSuite(
         (
         DocFileSuite('README.txt',
+                     setUp=setUp,
                      optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS,
                      ),
         DocTestSuite('z3c.image.proc.browser',



More information about the Checkins mailing list