[Zope-CVS] CVS: Products/FileCacheManager - FileCache.py:1.2

Jens Vagelpohl jens at dataflake.org
Thu Aug 19 14:39:01 EDT 2004


Update of /cvs-repository/Products/FileCacheManager
In directory cvs.zope.org:/tmp/cvs-serv19862

Modified Files:
	FileCache.py 
Log Message:
- Factor a representation of the filesystem cache entry into its own class
  to have a good place for ZODB transaction awareness later. Looks neater, too.


=== Products/FileCacheManager/FileCache.py 1.1 => 1.2 ===
--- Products/FileCacheManager/FileCache.py:1.1	Thu Aug 19 14:16:29 2004
+++ Products/FileCacheManager/FileCache.py	Thu Aug 19 14:38:58 2004
@@ -34,7 +34,6 @@
     def __init__(self, path='/tmp'):
         # self.cache maps physical ZODB paths to disk files.
         self.cache=OOBTree.OOBTree()
-        self.writelock = allocate_lock()
         self.next_cleanup = 0
         self.setDir(path)
         self._makeDirs()
@@ -92,23 +91,12 @@
     def ZCache_invalidate(self, ob):
         """ Invalidate cache entries that apply to ob """
         fname = self._fileName(ob)
+        cache_entry = FileCacheEntry(fname)
+        cache_entry.remove()
 
-        try:
-            try:
-                self.writelock.acquire()
-                if os.path.exists(fname): # XXX race?
-                    os.remove(fname)
-            except IOError, msg:
-                 zLOG.LOG( 'FileCacheManager'
-                         , zLOG.ERROR
-                         , 'IOError removing file'
-                         , error=msg
-                         )
-        finally:
-            self.writelock.release()
-
-    def ZCache_get(self, ob, view_name='', keywords=None,
-                   mtime_func=None, default=None):
+    def ZCache_get( self, ob, view_name='', keywords=None
+                  , mtime_func=None, default=None
+                  ):
         """ Gets a cache entry and return a filestream_iterator """
         fname = self._fileName(ob)
 
@@ -124,8 +112,9 @@
 
         return fiter
 
-    def ZCache_set(self, ob, data=None, view_name='', keywords=None,
-                   mtime_func=None):
+    def ZCache_set( self, ob, data=None, view_name=''
+                  , keywords=None, mtime_func=None
+                  ):
         """ Sets a cache entry. """
         fname = self._fileName(ob)
 
@@ -133,40 +122,9 @@
             # maybe it's a File or an Image, grab the data
             data = ob.data
 
-        try:
-            if fname:
-                # use a temp. file for writing.
-                fd, tempname = tempfile.mkstemp()
-                # isinstance won't work on extension class
-                if type(data) == type(''):
-                    os.write(fd, data)
-                else:
-                    # Image pdata objects?
-                    # there's no interface to check
-                    # and I don't want to test meta_type, so
-                    # assume that's what we have here.
-                    while data is not None and hasattr(data, 'next'):
-                        os.write(fd, data.data)
-                        data = data.next
-
-                os.fsync(fd)
-                os.close(fd)
-                # rename that sucker.
-                # This may fail if they are not on the same filesystem.
-                try:
-                    self.writelock.acquire()
-                    try:
-                        os.rename(tempname, fname)
-                    except OSError:
-                        # windows fails if fname exists.
-                        # if this doesn't work, tough noogies
-                        os.unlink(fname)
-                        os.rename(tempname, fname)
-                finally:
-                    self.writelock.release()
+        cache_entry = FileCacheEntry(fname)
+        cache_entry.write(data)
 
-        except IOError, msg:
-            LOG('FileCacheManager', ERROR, 'IOError writing file', error=msg)
 
     def _makeDirs(self):
         """ Make sure we have somewhere to put files. """
@@ -180,4 +138,74 @@
                 except OSError:
                     zLOG.LOG('FileCacheManager', zLOG.PROBLEM,
                              'unable to create directory %s' % dirpath)
+
+
+class FileCacheEntry:
+    """ Class representing a cache file """
+
+    def __init__(self, path):
+        """ Instantiate a new instance """
+        self.path = path
+        self.writelock = allocate_lock()
+
+
+    def remove(self):
+        """ Delete the cache file from disk """
+        try:
+            try:
+                self.writelock.acquire()
+                if os.path.exists(self.path): # XXX race?
+                    os.remove(self.path)
+            except IOError, msg:
+                 zLOG.LOG( 'FileCacheManager'
+                         , zLOG.ERROR
+                         , 'IOError removing file'
+                         , error=msg
+                         )
+        finally:
+            self.writelock.release()
+
+
+    def write(self, data):
+        """ Write a cache file to disk """
+        try:
+            # Open a tempfile to safely dump data into
+            fd, tempname = tempfile.mkstemp()
+
+            # isinstance won't work on extension class
+            if type(data) == type(''):
+                os.write(fd, data)
+            else:
+                # Image pdata objects?
+                # there's no interface to check
+                # and I don't want to test meta_type, so
+                # assume that's what we have here.
+                while data is not None and hasattr(data, 'next'):
+                    os.write(fd, data.data)
+                    data = data.next
+
+            os.fsync(fd)
+            os.close(fd)
+
+            # Now rename the tempfile to reflect the desired name.
+            # This may fail if they are not on the same filesystem.
+            try:
+                self.writelock.acquire()
+                try:
+                    os.rename(tempname, self.path)
+                except OSError:
+                    # Windows fails if fname exists.
+                    # If this doesn't work, tough noogies
+                    os.unlink(self.path)
+                    os.rename(tempname, self.path)
+            finally:
+                self.writelock.release()
+
+        except IOError, msg:
+            zLOG.LOG( 'FileCacheManager'
+                    , zLOG.ERROR
+                    , 'IOError writing file'
+                    , error=msg
+                    )
+
 



More information about the Zope-CVS mailing list