Package ZenUtils :: Module PObjectCache
[hide private]
[frames] | no frames]

Source Code for Module ZenUtils.PObjectCache

  1  ########################################################################### 
  2  # 
  3  # This program is part of Zenoss Core, an open source monitoring platform. 
  4  # Copyright (C) 2007, Zenoss Inc. 
  5  # 
  6  # This program is free software; you can redistribute it and/or modify it 
  7  # under the terms of the GNU General Public License version 2 as published by 
  8  # the Free Software Foundation. 
  9  # 
 10  # For complete information please visit: http://www.zenoss.com/oss/ 
 11  # 
 12  ########################################################################### 
 13   
 14  """PObjectCache 
 15   
 16  Persistent object cache that should be placed in a temp_folder 
 17   
 18  $Id: PObjectCache.py,v 1.2 2003/04/11 15:50:18 edahl Exp $""" 
 19   
 20  __version__ = "$Revision: 1.2 $"[11:-2] 
 21   
 22  import time 
 23   
 24  from Globals import DTMLFile 
 25  from AccessControl import ClassSecurityInfo 
 26   
 27  from OFS.SimpleItem import SimpleItem 
 28   
29 -class PObjectCache(SimpleItem):
30 editCache = DTMLFile('dtml/editCache', globals()) 31 manage_options = ({'label':'Cache','action':'editCache'},) 32 33 security = ClassSecurityInfo() 34
35 - def __init__(self, id, timeout=20, clearthresh=20):
36 self.id = id 37 self.timeout = timeout 38 self.clearcount = self.clearthresh = clearthresh 39 self.cache = {}
40 41
42 - def checkCache(self, key):
43 """check to see if key is in cache return None if not""" 44 if self.cache.has_key(key): 45 cobj = self.cache[key] 46 if cobj.checkTime(): 47 return cobj.getObj() 48 else: 49 del self.cache[key] 50 self._p_changed = 1 51 return None
52 53
54 - def addToCache(self, key, obj):
55 """add an object to the cache""" 56 cobj = CacheObj(obj, self.timeout) 57 self.cache[key] = cobj
58
59 - def manage_clearCache(self, REQUEST=None):
60 self.cleanCache(force=1) 61 if REQUEST: 62 return self.editCache(self, REQUEST, "cleared cache")
63 64
65 - def cleanCache(self, force=0):
66 """clean the cache if nessesary""" 67 cleared = 0 68 if self.cache: 69 self.clearcount -= 1 70 if force or self.clearcount < self.clearthresh: 71 for key, value in self.cache.items(): 72 if not value.checkTime(): 73 cleared = 1 74 del self.cache[key] 75 self.clearcount = self.clearthresh 76 return cleared
77 78
79 - def getCache(self):
80 return self.cache
81 82 83 security.declareProtected('View','getCacheTimeout')
84 - def getCacheTimeout(self):
85 """return cache timeout""" 86 return self.timeout
87 88 89 security.declareProtected('View','getCacheClearthresh')
90 - def getCacheClearthresh(self):
91 """return cache clearthresh""" 92 return self.clearthresh
93 94
95 -class CacheObj:
96
97 - def __init__(self, obj, timeout):
98 self._obj = obj 99 self._timeout = timeout 100 self._time = time.time()
101
102 - def checkTime(self):
103 if self._time + self._timeout < time.time(): 104 return 0 105 else: 106 return 1
107
108 - def getObj(self):
109 return self._obj
110
111 - def getTime(self):
112 """Return the time at which this cache object was created""" 113 return self._time
114