[Zope-Checkins] CVS: Zope/lib/python/Products/StandardCacheManagers
- RAMCacheManager.py:1.10.68.2.2.1
Tres Seaver
tseaver at zope.com
Wed May 19 18:53:50 EDT 2004
Update of /cvs-repository/Zope/lib/python/Products/StandardCacheManagers
In directory cvs.zope.org:/tmp/cvs-serv21096/lib/python/Products/StandardCacheManagers
Modified Files:
Tag: tseaver-collector_911-branch
RAMCacheManager.py
Log Message:
- Initial pass at merging leper's changes from Collector #911.
=== Zope/lib/python/Products/StandardCacheManagers/RAMCacheManager.py 1.10.68.2 => 1.10.68.2.2.1 ===
--- Zope/lib/python/Products/StandardCacheManagers/RAMCacheManager.py:1.10.68.2 Thu Apr 29 16:08:00 2004
+++ Zope/lib/python/Products/StandardCacheManagers/RAMCacheManager.py Wed May 19 18:53:49 2004
@@ -10,41 +10,36 @@
# FOR A PARTICULAR PURPOSE
#
##############################################################################
-'''
-RAM cache manager --
- Caches the results of method calls in RAM.
+""" RAM cache manager -- Caches the results of method calls in RAM.
$Id$
-'''
-
-
-from OFS.Cache import Cache, CacheManager
-from OFS.SimpleItem import SimpleItem
+"""
+import time
from thread import allocate_lock
from cgi import escape
-import time
-import Globals
-from Globals import DTMLFile
-try: from cPickle import Pickler
-except: from pickle import Pickler
+from AccessControl import ClassSecurityInfo
+from AccessControl.Permissions import view as View
+from Globals import DTMLFile, InitializeClass
+from OFS.Cache import Cache, CacheManager, CacheException
+from OFS.Cache import ViewManagementScreens, ChangeCacheSettings
+from OFS.SimpleItem import SimpleItem
-try: from cStringIO import dumps
-except: from pickle import dumps
+try:
+ from cPickle import PicklingError, dumps
+except ImportError:
+ from pickle import PicklingError, dumps
_marker = [] # Create a new marker object.
-class CacheException (Exception):
- '''
- A cache-related exception.
- '''
-
+class CacheEntryException(CacheException):
+ """A cache-related exception.
+ """
class CacheEntry:
- '''
- Represents a cached value.
- '''
+ """Represents a cached value.
+ """
def __init__(self, index, data, view_name):
try:
@@ -53,8 +48,9 @@
# leaks. It's also convenient for determining the
# approximate memory usage of the cache entry.
self.size = len(dumps(index)) + len(dumps(data))
- except:
- raise CacheException('The data for the cache is not pickleable.')
+ except PicklingError:
+ raise CacheEntryException(
+ 'The data for the cache is not pickleable.')
self.created = time.time()
self.data = data
self.view_name = view_name
@@ -62,9 +58,9 @@
class ObjectCacheEntries:
- '''
+ """
Represents the cache for one Zope object.
- '''
+ """
hits = 0
misses = 0
@@ -75,12 +71,12 @@
self.entries = {}
def aggregateIndex(self, view_name, req, req_names, local_keys):
- '''
+ """
Returns the index to be used when looking for or inserting
a cache entry.
view_name is a string.
local_keys is a mapping or None.
- '''
+ """
req_index = []
# Note: req_names is already sorted.
for key in req_names:
@@ -111,8 +107,10 @@
self.entries[index] = CacheEntry(index, data, view_name)
def delEntry(self, index):
- try: del self.entries[index]
- except KeyError: pass
+ try:
+ del self.entries[index]
+ except KeyError:
+ pass
class RAMCache (Cache):
@@ -148,19 +146,19 @@
return oc
def countAllEntries(self):
- '''
+ """
Returns the count of all cache entries.
- '''
+ """
count = 0
for oc in self.cache.values():
count = count + len(oc.entries)
return count
def countAccesses(self):
- '''
+ """
Returns a mapping of
(n) -> number of entries accessed (n) times
- '''
+ """
counters = {}
for oc in self.cache.values():
for entry in oc.entries.values():
@@ -170,9 +168,9 @@
return counters
def clearAccessCounters(self):
- '''
+ """
Clears access_count for each cache entry.
- '''
+ """
for oc in self.cache.values():
for entry in oc.entries.values():
entry.access_count = 0
@@ -210,9 +208,9 @@
self.writelock.release()
def cleanup(self):
- '''
+ """
Removes cache entries.
- '''
+ """
self.deleteStaleEntries()
new_count = self.countAllEntries()
if new_count > self.threshold:
@@ -258,9 +256,9 @@
return rval
def ZCache_invalidate(self, ob):
- '''
+ """
Invalidates the cache entries that apply to ob.
- '''
+ """
path = ob.getPhysicalPath()
# Invalidates all subobjects as well.
self.writelock.acquire()
@@ -274,9 +272,9 @@
def ZCache_get(self, ob, view_name='', keywords=None,
mtime_func=None, default=None):
- '''
+ """
Gets a cache entry or returns default.
- '''
+ """
oc = self.getObjectCacheEntries(ob)
if oc is None:
return default
@@ -300,9 +298,9 @@
def ZCache_set(self, ob, data, view_name='', keywords=None,
mtime_func=None):
- '''
+ """
Sets a cache entry.
- '''
+ """
now = time.time()
if self.next_cleanup <= now:
self.cleanup()
@@ -339,15 +337,8 @@
AcceleratedHTTPCacheManager and/or downstream
caching.
"""
-
- __ac_permissions__ = (
- ('View management screens', ('getSettings',
- 'manage_main',
- 'manage_stats',
- 'getCacheReport',
- 'sort_link',)),
- ('Change cache managers', ('manage_editProps',), ('Manager',)),
- )
+ security = ClassSecurityInfo()
+ security.setPermissionDefault(ChangeCacheSettings, ('Manager',))
manage_options = (
{'label':'Properties', 'action':'manage_main',
@@ -369,11 +360,13 @@
}
self.__cacheid = '%s_%f' % (id(self), time.time())
+ security.declareProtected(View, 'getId')
def getId(self):
- ' '
+ """
+ """
return self.id
- ZCacheManager_getCache__roles__ = ()
+ security.declarePrivate( 'ZCacheManager_getCache' )
def ZCacheManager_getCache(self):
cacheid = self.__cacheid
try:
@@ -384,6 +377,7 @@
caches[cacheid] = cache
return cache
+ security.declareProtected(ViewManagementScreens, 'getSettings')
def getSettings(self):
'Returns the current cache settings.'
res = self._settings.copy()
@@ -391,8 +385,10 @@
res['max_age'] = 0
return res
+ security.declareProtected(ViewManagementScreens, 'manage_main')
manage_main = DTMLFile('dtml/propsRCM', globals())
+ security.declareProtected(ChangeCacheSettings, 'manage_editProps')
def manage_editProps(self, title, settings=None, REQUEST=None):
'Changes the cache settings.'
if settings is None:
@@ -412,8 +408,10 @@
return self.manage_main(
self, REQUEST, manage_tabs_message='Properties changed.')
+ security.declareProtected(ViewManagementScreens, 'manage_stats')
manage_stats = DTMLFile('dtml/statsRCM', globals())
+ security.declarePrivate( '_getSortInfo' )
def _getSortInfo(self):
"""
Returns the value of sort_by and sort_reverse.
@@ -424,6 +422,7 @@
sort_reverse = int(req.get('sort_reverse', 1))
return sort_by, sort_reverse
+ security.declareProtected(ViewManagementScreens, 'getCacheReport')
def getCacheReport(self):
"""
Returns the list of objects in the cache, sorted according to
@@ -439,6 +438,7 @@
rval.reverse()
return rval
+ security.declareProtected(ViewManagementScreens, 'sort_link')
def sort_link(self, name, id):
"""
Utility for generating a sort link.
@@ -451,7 +451,7 @@
url = url + '&sort_reverse=' + (newsr and '1' or '0')
return '<a href="%s">%s</a>' % (escape(url, 1), escape(name))
-Globals.default__class_init__(RAMCacheManager)
+InitializeClass(RAMCacheManager)
manage_addRAMCacheManagerForm = DTMLFile('dtml/addRCM', globals())
More information about the Zope-Checkins
mailing list