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

# Source Code for Module Products.ZenUtils.ObjectCache

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