1
2
3
4
5
6
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
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):
33
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
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
58
59
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
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
90
91 security.declareProtected('View','getCacheTimeout')
93 """return cache timeout"""
94 return self.timeout
95
96 security.declareProtected('View','getCacheClearthresh')
98 """return cache clearthresh"""
99 return self.clearthresh
100
102
107
109 if self._time + self._timeout < time.time():
110 return 0
111 else:
112 return 1
113
116
118 """Return the time at which this cache object was created"""
119 return self._time
120