[Zope-CVS] CVS: Products/FileCacheManager - FileCache.py:1.7
FileCacheManager.py:1.17
Chris McDonough
chrism at plope.com
Sun Aug 29 16:51:00 EDT 2004
Update of /cvs-repository/Products/FileCacheManager
In directory cvs.zope.org:/tmp/cvs-serv20167
Modified Files:
FileCache.py FileCacheManager.py
Log Message:
Add simple statistics tracking.
=== Products/FileCacheManager/FileCache.py 1.6 => 1.7 ===
--- Products/FileCacheManager/FileCache.py:1.6 Sun Aug 29 15:03:06 2004
+++ Products/FileCacheManager/FileCache.py Sun Aug 29 16:50:29 2004
@@ -44,6 +44,7 @@
self._makeDirs()
self._naming_expr = None
self._tempfile_path = ''
+ self.entries = {}
def _fileName(self, ob):
""" Compute a filename based on an MD5 hash: doesn't preserve
@@ -104,13 +105,14 @@
if _debug:
print "ZCache_invalidate called"
fname = self._fileName(ob)
- cache_entry = FileCacheEntry(fname, self._tempfile_path)
+ cache_entry = FileCacheEntry(self, ob, fname, self._tempfile_path)
cache_entry.remove()
def ZCache_get( self, ob, view_name='', keywords=None
, mtime_func=None, default=None
):
""" Gets a cache entry and return a filestream_iterator """
+ pp = '/'.join(ob.getPhysicalPath())
fname = self._fileName(ob)
try:
@@ -118,13 +120,20 @@
except IOError:
# couldn't get the actual cached file
msg = ('Failed to retrieve cached file for "%s" using "%s" '
- 'as a filename' % ('/'.join(ob.getPhysicalPath()), fname))
+ 'as a filename' % (pp, fname))
zLOG.LOG('FileCacheManager', zLOG.INFO, msg, error=sys.exc_info())
return default
if _debug:
- print ("returning filestream iterator for %s" %
- '/'.join(ob.getPhysicalPath()))
+ print "returning filestream iterator for %s" % pp
+
+ try:
+ entry = self.entries[pp]
+ except KeyError:
+ entry = {'path':pp, 'fpath':fname, 'size':0, 'hits':0}
+ entry['hits'] += 1
+ self.entries[pp] = entry
+
return fiter
def ZCache_set( self, ob, data=None, view_name=''
@@ -139,9 +148,26 @@
# maybe it's a File or an Image, grab the data
data = ob.data
- cache_entry = FileCacheEntry(fname, self._tempfile_path)
+ cache_entry = FileCacheEntry(self, ob, fname, self._tempfile_path)
cache_entry.write(data)
+ def record(self, state):
+ pp = '/'.join(state.ob.getPhysicalPath())
+ if state.op_remove:
+ try:
+ del self.entries[pp]
+ except KeyError:
+ pass
+ elif state.op_write:
+ empty = {'path':pp, 'fpath':state.path, 'size':0, 'hits':0}
+ entry = self.entries.setdefault(pp, empty)
+ entry['path'] = pp
+ entry['size'] = state.size
+ entry['fpath'] = state.path
+
+ def values(self):
+ return self.entries.values()
+
def _makeDirs(self):
""" Make sure we have somewhere to put files. """
chars = '0123456789abcdef'
@@ -166,8 +192,10 @@
makedirs = True # make directories as necessary
- def __init__(self, path, tmpdir):
+ def __init__(self, cache, ob, path, tmpdir):
""" Instantiate a new instance """
+ self.cache = cache
+ self.ob = ob
self.path = path
self.tmpdir = tmpdir
self.temppath = None
@@ -176,6 +204,7 @@
self.op_write = 0
self._transaction_done = 0
self.timestamp = time.time()
+ self.size = 0
# Register as Transaction Manager so my transaction hooks get called.
get_transaction().register(self)
@@ -199,6 +228,7 @@
# isinstance won't work on extension class
if type(data) == type(''):
os.write(fd, data)
+ self.size = len(data)
else:
# Image pdata objects?
# there's no interface to check
@@ -206,6 +236,7 @@
# assume that's what we have here.
while data is not None and hasattr(data, 'next'):
os.write(fd, data.data)
+ self.size = self.size + len(data.data)
data = data.next
os.fsync(fd)
@@ -263,6 +294,7 @@
def tpc_finish(self, transaction):
""" Called at the end of a successful transaction """
if getattr(self, '_transaction_done', 0) and self.op_remove:
+ self.cache.record(self)
try:
try:
self.writelock.acquire()
@@ -271,16 +303,17 @@
if _debug:
print "removed %s" % self.path
except IOError:
- zLOG.LOG( 'FileCacheManager'
- , zLOG.ERROR
- , 'IOError removing file "%s"' % self.path
- , error=sys.exc_info()
- )
+ zLOG.LOG( 'FileCacheManager'
+ , zLOG.ERROR
+ , 'IOError removing file "%s"' % self.path
+ , error=sys.exc_info()
+ )
finally:
self.writelock.release()
self.op_remove = 0
-
+
elif getattr(self, '_transaction_done', 0) and self.op_write:
+ self.cache.record(self)
# Now rename the tempfile to reflect the desired name.
# This may fail if they are not on the same filesystem.
try:
=== Products/FileCacheManager/FileCacheManager.py 1.16 => 1.17 ===
--- Products/FileCacheManager/FileCacheManager.py:1.16 Sun Aug 29 14:20:55 2004
+++ Products/FileCacheManager/FileCacheManager.py Sun Aug 29 16:50:30 2004
@@ -42,7 +42,9 @@
)
_tempfile_path = ''
-
+
+ manage_stats = PageTemplateFile('www/statsFCM', globals())
+
def __init__(self, ob_id, path='/tmp', tpath='', title=''):
# based on RAMCacheManager
self.id = ob_id
@@ -57,7 +59,14 @@
'max_age': 3600,
}
self.__cacheid = '%s_%f' % (id(self), time.time())
-
+
+ def getCacheReport(self):
+ """
+ Reports on the contents of the cache.
+ """
+ rval = []
+ cache = self.ZCacheManager_getCache()
+ return cache.values()
def ZCacheManager_getCache(self):
""" Cache Settings """
More information about the Zope-CVS
mailing list