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

# Source Code for Module Products.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          self.initCache()
     67          if key is not None:
     68              try:
     69                  del self._v_cache[key]
     70              except KeyError:
     71                  pass
     72          else:
     73              self._v_cache = {}
     74
     75
     76 -    def cleanCache(self, force=0):
     77          """clean the cache if nessesary"""
     78          self.initCache()
     79          cleared = 0
     80          if self._v_cache:
     81              self._v_clearcount -= 1
     82              if force or self._v_clearcount < self.clearthresh:
     83                  for key, value in self._v_cache.items():
     84                      if not value.checkTime():
     85                          cleared = 1
     86                          del self._v_cache[key]
     87                  self._v_clearcount = self.clearthresh
     88          return cleared
     89
     90 -    def getCache(self):
     91          self.initCache()
     92          return self._v_cache
     93
     94      security.declareProtected('View','getCacheTimeout')
     95 -    def getCacheTimeout(self):
     96          """return cache timeout"""
     97          return self.timeout
     98
     99      security.declareProtected('View','getCacheClearthresh')
    100 -    def getCacheClearthresh(self):
    101          """return cache clearthresh"""
    102          return self.clearthresh
    103
    104 -class CacheObj:
    105
    106 -    def __init__(self, obj, timeout):
    107          self._obj = obj
    108          self._timeout = timeout
    109          self._time = time.time()
    110
    111 -    def checkTime(self):
    112          if self._time + self._timeout < time.time():
    113              return 0
    114          else:
    115              return 1
    116
    117 -    def getObj(self):
    118          return self._obj
    119
    120 -    def getTime(self):
    121          """Return the time at which this cache object was created"""
    122          return self._time
    123