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

Source Code for Module Products.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 self._p_changed = 1
59
60 - def manage_clearCache(self, REQUEST=None):
61 self.cleanCache(force=1) 62 if REQUEST: 63 return self.editCache(self, REQUEST, "cleared cache")
64 65
66 - def cleanCache(self, force=0):
67 """clean the cache if nessesary""" 68 cleared = 0 69 if self.cache: 70 self.clearcount -= 1 71 if force or self.clearcount < self.clearthresh: 72 for key, value in self.cache.items(): 73 if not value.checkTime(): 74 cleared = 1 75 del self.cache[key] 76 self._p_changed = 1 77 self.clearcount = self.clearthresh 78 return cleared
79 80
81 - def getCache(self):
82 return self.cache
83 84 85 security.declareProtected('View','getCacheTimeout')
86 - def getCacheTimeout(self):
87 """return cache timeout""" 88 return self.timeout
89 90 91 security.declareProtected('View','getCacheClearthresh')
92 - def getCacheClearthresh(self):
93 """return cache clearthresh""" 94 return self.clearthresh
95 96
97 -class CacheObj:
98
99 - def __init__(self, obj, timeout):
100 self._obj = obj 101 self._timeout = timeout 102 self._time = time.time()
103
104 - def checkTime(self):
105 if self._time + self._timeout < time.time(): 106 return 0 107 else: 108 return 1
109
110 - def getObj(self):
111 return self._obj
112
113 - def getTime(self):
114 """Return the time at which this cache object was created""" 115 return self._time
116