1
2
3
4
5
6
7
8
9
10
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
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):
36
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
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
61
62
64 """Clear the cache.
65 """
66 if key is not None:
67 try:
68 del self._v_cache[key]
69 except KeyError:
70 pass
71 else:
72 self._v_cache = {}
73
74
76 """clean the cache if nessesary"""
77 self.initCache()
78 cleared = 0
79 if self._v_cache:
80 self._v_clearcount -= 1
81 if force or self._v_clearcount < self.clearthresh:
82 for key, value in self._v_cache.items():
83 if not value.checkTime():
84 cleared = 1
85 del self._v_cache[key]
86 self._v_clearcount = self.clearthresh
87 return cleared
88
92
93 security.declareProtected('View','getCacheTimeout')
95 """return cache timeout"""
96 return self.timeout
97
98 security.declareProtected('View','getCacheClearthresh')
100 """return cache clearthresh"""
101 return self.clearthresh
102
104
106 self._obj = obj
107 self._timeout = timeout
108 self._time = time.time()
109
111 if self._time + self._timeout < time.time():
112 return 0
113 else:
114 return 1
115
118
120 """Return the time at which this cache object was created"""
121 return self._time
122