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 Persistent 
 25  from Globals import DTMLFile 
 26  from AccessControl import ClassSecurityInfo 
 27   
 28  from OFS.SimpleItem import SimpleItem 
 29   
30 -class PObjectCache(SimpleItem):
31 editCache = DTMLFile('dtml/editCache', globals()) 32 manage_options = ({'label':'Cache','action':'editCache'},) 33 34 security = ClassSecurityInfo() 35
36 - def __init__(self, id, timeout=20, clearthresh=20):
37 self.id = id 38 self.timeout = timeout 39 self.clearcount = self.clearthresh = clearthresh 40 self.cache = {}
41 42
43 - def checkCache(self, key):
44 """check to see if key is in cache return None if not""" 45 if self.cache.has_key(key): 46 cobj = self.cache[key] 47 if cobj.checkTime(): 48 return cobj.getObj() 49 else: 50 del self.cache[key] 51 self._p_changed = 1 52 return None
53 54
55 - def addToCache(self, key, obj):
56 """add an object to the cache""" 57 cobj = CacheObj(obj, self.timeout) 58 self.cache[key] = cobj
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.clearcount = self.clearthresh 77 return cleared
78 79
80 - def getCache(self):
81 return self.cache
82 83 84 security.declareProtected('View','getCacheTimeout')
85 - def getCacheTimeout(self):
86 """return cache timeout""" 87 return self.timeout
88 89 90 security.declareProtected('View','getCacheClearthresh')
91 - def getCacheClearthresh(self):
92 """return cache clearthresh""" 93 return self.clearthresh
94 95
96 -class CacheObj:
97
98 - def __init__(self, obj, timeout):
99 self._obj = obj 100 self._timeout = timeout 101 self._time = time.time()
102
103 - def checkTime(self):
104 if self._time + self._timeout < time.time(): 105 return 0 106 else: 107 return 1
108
109 - def getObj(self):
110 return self._obj
111
112 - def getTime(self):
113 """Return the time at which this cache object was created""" 114 return self._time
115