[Zope-Checkins] CVS: Products/StandardCacheManagers - RAMCacheManager.py:1.10.68.4

Andreas Jung andreas at andreas-jung.com
Mon Nov 29 06:12:13 EST 2004


Update of /cvs-repository/Products/StandardCacheManagers
In directory cvs.zope.org:/tmp/cvs-serv21427/lib/python/Products/StandardCacheManagers

Modified Files:
      Tag: Zope-2_7-branch
	RAMCacheManager.py 
Log Message:
      - RAMCacheManager: opimized performance by using cPickle instead
        of pickle and by using the highest pickle protocol available
        instead of using ASCII pickles (patch by Dieter Maurer)


=== Products/StandardCacheManagers/RAMCacheManager.py 1.10.68.3 => 1.10.68.4 ===
--- Products/StandardCacheManagers/RAMCacheManager.py:1.10.68.3	Mon Nov  8 04:49:59 2004
+++ Products/StandardCacheManagers/RAMCacheManager.py	Mon Nov 29 06:12:13 2004
@@ -26,11 +26,8 @@
 import Globals
 from Globals import DTMLFile
 
-try: from cPickle import Pickler
-except: from pickle import Pickler
-
-try: from cStringIO import dumps
-except: from pickle import dumps
+try: from cPickle import Pickler, HIGHEST_PROTOCOL
+except: from pickle import Pickler, HIGHEST_PROTOCOL
 
 _marker = []  # Create a new marker object.
 
@@ -52,7 +49,17 @@
             # us from caching something that might result in memory
             # leaks.  It's also convenient for determining the
             # approximate memory usage of the cache entry.
-            self.size = len(dumps(index)) + len(dumps(data))
+            # DM 2004-11-29: this code causes excessive time.
+            #   Note also that it does not prevent us from
+            #   caching objects with references to persistent objects
+            #   When we do, nasty persistency errors are likely
+            #   to occur ("shouldn't load data while connection is closed").
+            #self.size = len(dumps(index)) + len(dumps(data))
+            sizer = _ByteCounter()
+            pickler = Pickler(sizer, HIGHEST_PROTOCOL)
+            pickler.dump(index)
+            pickler.dump(data)
+            self.size = sizer.getCount()
         except:
             raise CacheException('The data for the cache is not pickleable.')
         self.created = time.time()
@@ -467,6 +474,16 @@
 
 Globals.default__class_init__(RAMCacheManager)
 
+
+class _ByteCounter:
+    '''auxiliary file like class which just counts the bytes written.'''
+    _count = 0
+
+    def write(self, bytes):
+        self._count += len(bytes)
+
+    def getCount(self):
+        return self._count
 
 manage_addRAMCacheManagerForm = DTMLFile('dtml/addRCM', globals())
 



More information about the Zope-Checkins mailing list