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

Source Code for Module ZenUtils.ObjectCache

  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  """NcoProduct 
 15   
 16  Data connector for Micromuse Omnibus 
 17   
 18  $Id: ObjectCache.py,v 1.7 2003/04/14 21:08:25 edahl Exp $""" 
 19   
 20  __version__ = "$Revision: 1.7 $"[11:-2] 
 21   
 22  from Globals import Persistent 
 23  from Globals import DTMLFile 
 24  from AccessControl import ClassSecurityInfo 
 25  import time 
 26   
27 -class ObjectCache(Persistent):
28 editCache = DTMLFile('dtml/editCache', globals()) 29 manage_options = ({'label':'Cache','action':'editCache'},) 30 31 security = ClassSecurityInfo() 32
33 - def __init__(self, timeout=20, clearthresh=20):
34 self.timeout = timeout 35 self.clearthresh = clearthresh
36
37 - def initCache(self):
38 """make sure that volitile attributes exist""" 39 if not hasattr(self, '_v_cache'): 40 self._v_cache = {} 41 if not hasattr(self, '_v_clearcount'): 42 self._v_clearcount = self.clearthresh
43
44 - def checkCache(self, key):
45 """check to see if key is in cache return None if not""" 46 self.initCache() 47 if self._v_cache.has_key(key): 48 cobj = self._v_cache[key] 49 if cobj.checkTime(): 50 return cobj.getObj() 51 else: 52 del self._v_cache[key] 53 return None
54 55
56 - def addToCache(self, key, obj):
57 """add an object to the cache""" 58 self.initCache() 59 cobj = CacheObj(obj, self.timeout) 60 self._v_cache[key] = cobj
61 62
63 - def clearCache(self, key=None):
64 """Clear the cache. 65 """ 66 if key is not None: 67 try: 68 del self._v_cache[key] 69 except KeyError: 70 pass 71 else: 72 self._v_cache = {}
73 74
75 - def cleanCache(self, force=0):
76 """clean the cache if nessesary""" 77 self.initCache() 78 cleared = 0 79 if self._v_cache: 80 self._v_clearcount -= 1 81 if force or self._v_clearcount < self.clearthresh: 82 for key, value in self._v_cache.items(): 83 if not value.checkTime(): 84 cleared = 1 85 del self._v_cache[key] 86 self._v_clearcount = self.clearthresh 87 return cleared
88
89 - def getCache(self):
90 self.initCache() 91 return self._v_cache
92 93 security.declareProtected('View','getCacheTimeout')
94 - def getCacheTimeout(self):
95 """return cache timeout""" 96 return self.timeout
97 98 security.declareProtected('View','getCacheClearthresh')
99 - def getCacheClearthresh(self):
100 """return cache clearthresh""" 101 return self.clearthresh
102
103 -class CacheObj:
104
105 - def __init__(self, obj, timeout):
106 self._obj = obj 107 self._timeout = timeout 108 self._time = time.time()
109
110 - def checkTime(self):
111 if self._time + self._timeout < time.time(): 112 return 0 113 else: 114 return 1
115
116 - def getObj(self):
117 return self._obj
118
119 - def getTime(self):
120 """Return the time at which this cache object was created""" 121 return self._time
122