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 or (at your 
  8  # option) any later version as published by 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 _p_resolveConflict(self, oldstate, savedstate, newstate):
43 """ 44 Any page that contains a lot of graphs is practically guaranteed to 45 cause read conflict errors on PObjectCache. Fortunately it's really 46 easy to do good conflict resolution on this simple object. 47 """ 48 mergedstate = oldstate 49 mergedstate['cache'].update(savedstate['cache']) 50 mergedstate['cache'].update(newstate['cache']) 51 return mergedstate
52 53
54 - def checkCache(self, key):
55 """check to see if key is in cache return None if not""" 56 if key in self.cache: 57 cobj = self.cache[key] 58 if cobj.checkTime(): 59 return cobj.getObj() 60 else: 61 del self.cache[key] 62 self._p_changed = 1 63 return None
64 65
66 - def addToCache(self, key, obj):
67 """add an object to the cache""" 68 cobj = CacheObj(obj, self.timeout) 69 self.cache[key] = cobj 70 self._p_changed = 1
71
72 - def manage_clearCache(self, REQUEST=None):
73 self.cleanCache(force=1) 74 if REQUEST: 75 return self.editCache(self, REQUEST, "cleared cache")
76 77
78 - def cleanCache(self, force=0):
79 """clean the cache if nessesary""" 80 cleared = 0 81 if self.cache: 82 self.clearcount -= 1 83 if force or self.clearcount < self.clearthresh: 84 for key, value in self.cache.items(): 85 if not value.checkTime(): 86 cleared = 1 87 del self.cache[key] 88 self._p_changed = 1 89 self.clearcount = self.clearthresh 90 return cleared
91 92
93 - def getCache(self):
94 return self.cache
95 96 97 security.declareProtected('View','getCacheTimeout')
98 - def getCacheTimeout(self):
99 """return cache timeout""" 100 return self.timeout
101 102 103 security.declareProtected('View','getCacheClearthresh')
104 - def getCacheClearthresh(self):
105 """return cache clearthresh""" 106 return self.clearthresh
107 108
109 -class CacheObj:
110
111 - def __init__(self, obj, timeout):
112 self._obj = obj 113 self._timeout = timeout 114 self._time = time.time()
115
116 - def checkTime(self):
117 if self._time + self._timeout < time.time(): 118 return 0 119 else: 120 return 1
121
122 - def getObj(self):
123 return self._obj
124
125 - def getTime(self):
126 """Return the time at which this cache object was created""" 127 return self._time
128