[Zope-CVS] CVS: Products/FileCacheManager - FileCacheManager.py:1.8
Jens Vagelpohl
jens at dataflake.org
Fri Aug 13 12:58:29 EDT 2004
Update of /cvs-repository/Products/FileCacheManager
In directory cvs.zope.org:/tmp/cvs-serv3948
Modified Files:
FileCacheManager.py
Log Message:
- couple docstring changes
- less stupid check for writability of a chosen cache directory
- some variable name changes
- some added whitespace
=== Products/FileCacheManager/FileCacheManager.py 1.7 => 1.8 ===
--- Products/FileCacheManager/FileCacheManager.py:1.7 Fri Aug 13 08:53:50 2004
+++ Products/FileCacheManager/FileCacheManager.py Fri Aug 13 12:58:29 2004
@@ -34,10 +34,10 @@
caches = {}
class FileCache(RAMCache):
- """A cache that caches to the filesystem """
+ """ A cache that caches rendered content to the filesystem """
def __init__(self, path='/tmp'):
- # cache maps physical paths to files.
+ # self.cache maps physical ZODB paths to disk files.
self.cache=OOBTree.OOBTree()
self.writelock = allocate_lock()
self.next_cleanup = 0
@@ -49,8 +49,9 @@
human-readable path, but otherwise makes life much easier """
phys_path = '/'.join(ob.getPhysicalPath())[1:]
hashed = md5.new(phys_path).hexdigest()
- fn = os.path.join(self._dir, hashed[:2], hashed)
- return fn
+ fname = os.path.join(self._dir, hashed[:2], hashed)
+
+ return fname
def getDir(self):
""" Retrieve the filesystem directory used for caching """
@@ -62,21 +63,27 @@
self._makeDirs()
def ZCache_invalidate(self, ob):
- """ Invalidates cache entries that apply to ob, from FSCacheManager """
+ """ Invalidates cache entries that apply to ob """
# XXX ADD LOCKS HERE
- fn = self._fileName(ob)
+ fname = self._fileName(ob)
+
try:
- if os.path.exists(fn): # XXX race?
- os.remove(fn)
+ if os.path.exists(fname): # XXX race?
+ os.remove(fname)
except IOError, msg:
- LOG('DiskCacheManger', ERROR, 'IOError removing file', error=msg)
+ zLOG.LOG( 'FileCacheManager'
+ , zLOG.ERROR
+ , 'IOError removing file'
+ , error=msg
+ )
def ZCache_get(self, ob, view_name='', keywords=None,
mtime_func=None, default=None):
- """ Gets a cache entry """
- f_path = self._fileName(ob)
+ """ Gets a cache entry and return a filestream_iterator """
+ fname = self._fileName(ob)
+
try:
- f = filestream_iterator(f_path, 'rb')
+ fiter = filestream_iterator(fname, 'rb')
except IOError:
# couldn't get the actual cache
zLOG.LOG('FileCacheManager', zLOG.INFO,
@@ -84,22 +91,23 @@
'/'.join(ob.getPhysicalPath())
)
return default
- return f
+
+ return fiter
def ZCache_set(self, ob, data=None, view_name='', keywords=None,
mtime_func=None):
- """ Sets a cache entry. Code swiped from FSCacheManager. """
+ """ Sets a cache entry. """
# XXX locks?
- fnId = self._fileName(ob)
+ fname = self._fileName(ob)
if data is None:
# maybe it's a File or an Image, grab the data
data = ob.data
try:
- if fnId:
+ if fname:
# use a temp. file for writing.
- fd, tempname = tempfile.mkstemp() #open(fnId, 'wb')
+ fd, tempname = tempfile.mkstemp()
# isinstance won't work on extension class
if type(data) == type(''):
os.write(fd, data)
@@ -111,24 +119,26 @@
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:
- os.rename(tempname, fnId)
+ os.rename(tempname, fname)
except OSError:
- # windows fails if fnId exists.
+ # windows fails if fname exists.
# if this doesn't work, tough noogies
- os.unlink(fnId)
- os.rename(tempname, fnId)
+ os.unlink(fname)
+ os.rename(tempname, fname)
except IOError, msg:
- LOG('DiskCacheManger', ERROR, 'IOError writing file', error=msg)
+ LOG('FileCacheManager', ERROR, 'IOError writing file', error=msg)
def _makeDirs(self):
""" Make sure we have somewhere to put files. """
chars = '0123456789abcdef'
+
for char in chars:
for char2 in chars:
dirpath = os.path.join(self._dir, '%s%s' % (char, char2))
@@ -202,14 +212,11 @@
def manage_addFileCacheManager(self, id, path='/tmp', title='', REQUEST=None):
""" Adds a FileCacheManager to the folder. """
- test_path = os.path.join(path, 'test.txt')
- try:
- fd = open(test_path, 'w')
+ # Test to see that we can write into the place we got as the cache dir
+ if os.access(path, os.W_OK):
self._setObject(id, FileCacheManager(id, path, title))
msg = 'FileCacheManager created.'
- fd.close()
- os.unlink(test_path)
- except IOError:
+ else:
msg = 'Invalid directory path "%s"' % path
if REQUEST is None:
raise(IOError, msg)
More information about the Zope-CVS
mailing list