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 key in self._v_cache:
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 self.initCache()
67 if key is not None:
68 try:
69 del self._v_cache[key]
70 except KeyError:
71 pass
72 else:
73 self._v_cache = {}
74
75
77 """clean the cache if nessesary"""
78 self.initCache()
79 cleared = 0
80 if self._v_cache:
81 self._v_clearcount -= 1
82 if force or self._v_clearcount < self.clearthresh:
83 for key, value in self._v_cache.items():
84 if not value.checkTime():
85 cleared = 1
86 del self._v_cache[key]
87 self._v_clearcount = self.clearthresh
88 return cleared
89
93
94 security.declareProtected('View','getCacheTimeout')
96 """return cache timeout"""
97 return self.timeout
98
99 security.declareProtected('View','getCacheClearthresh')
101 """return cache clearthresh"""
102 return self.clearthresh
103
105
110
112 if self._time + self._timeout < time.time():
113 return 0
114 else:
115 return 1
116
119
121 """Return the time at which this cache object was created"""
122 return self._time
123