[Zope-Checkins] CVS: Zope2 - RAMCacheManager.py:1.6 version.txt:1.3
shane@digicool.com
shane@digicool.com
Tue, 24 Apr 2001 09:08:47 -0400 (EDT)
Update of /cvs-repository/Zope2/lib/python/Products/StandardCacheManagers
In directory korak:/tmp/cvs-serv7168
Modified Files:
RAMCacheManager.py version.txt
Log Message:
Added "max_age" parameter to RAM cache manager, making it easier to
configure for certain kinds of setups.
--- Updated File RAMCacheManager.py in package Zope2 --
--- RAMCacheManager.py 2001/03/06 21:14:57 1.5
+++ RAMCacheManager.py 2001/04/24 13:08:46 1.6
@@ -127,6 +127,7 @@
self.size = len(dumps(index)) + len(dumps(data))
except:
raise CacheException('The data for the cache is not pickleable.')
+ self.created = time.time()
self.data = data
self.view_name = view_name
self.access_count = 0
@@ -142,7 +143,7 @@
def __init__(self, path):
self.physical_path = path
- self.lastmod = 0
+ self.lastmod = 0 # Mod time of the object, class, etc.
self.entries = {}
def aggregateIndex(self, view_name, req, req_names, local_keys):
@@ -181,7 +182,11 @@
self.lastmod = lastmod
self.entries[index] = CacheEntry(index, data, view_name)
+ def delEntry(self, index):
+ try: del self.entries[index]
+ except KeyError: pass
+
class RAMCache (Cache):
# Note the need to take thread safety into account.
# Also note that objects of this class are not persistent,
@@ -258,10 +263,27 @@
finally:
self.writelock.release()
+ def deleteStaleEntries(self):
+ """
+ Deletes entries that have expired.
+ """
+ self.writelock.acquire()
+ try:
+ min_created = time.time() - self.max_age
+ for p, oc in self.cache.items():
+ for agindex, entry in oc.entries.items():
+ if entry.created < min_created:
+ del oc.entries[agindex]
+ if len(oc.entries) < 1:
+ del self.cache[p]
+ finally:
+ self.writelock.release()
+
def cleanup(self):
'''
Removes cache entries.
'''
+ self.deleteStaleEntries()
new_count = self.countAllEntries()
if new_count > self.threshold:
counters = self.countAccesses()
@@ -334,6 +356,14 @@
entry = oc.getEntry(lastmod, index)
if entry is _marker:
return default
+ if entry.created < time.time() - self.max_age:
+ # Expired.
+ self.writelock.acquire()
+ try:
+ oc.delEntry(index)
+ finally:
+ self.writelock.release()
+ return default
oc.hits = oc.hits + 1
entry.access_count = entry.access_count + 1
return entry.data
@@ -389,7 +419,9 @@
self._settings = {
'threshold': 1000,
'cleanup_interval': 300,
- 'request_vars': ('AUTHENTICATED_USER',)}
+ 'request_vars': ('AUTHENTICATED_USER',),
+ 'max_age': 3600,
+ }
self.__cacheid = '%s_%f' % (id(self), time.time())
def getId(self):
@@ -423,7 +455,9 @@
self._settings = {
'threshold': int(settings['threshold']),
'cleanup_interval': int(settings['cleanup_interval']),
- 'request_vars': tuple(request_vars)}
+ 'request_vars': tuple(request_vars),
+ 'max_age': int(settings['max_age']),
+ }
cache = self.ZCacheManager_getCache()
cache.initSettings(self._settings)
if REQUEST is not None:
--- Updated File version.txt in package Zope2 --
--- version.txt 2001/01/09 22:25:10 1.2
+++ version.txt 2001/04/24 13:08:46 1.3
@@ -1 +1 @@
-StandardCacheManagers-1-0-0
+StandardCacheManagers-1-1-0