1
2
3
4
5
6
7
8
9
10
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
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):
40
41
43 """check to see if key is in cache return None if not"""
44 if self.cache.has_key(key):
45 cobj = self.cache[key]
46 if cobj.checkTime():
47 return cobj.getObj()
48 else:
49 del self.cache[key]
50 self._p_changed = 1
51 return None
52
53
55 """add an object to the cache"""
56 cobj = CacheObj(obj, self.timeout)
57 self.cache[key] = cobj
58 self._p_changed = 1
59
64
65
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._p_changed = 1
77 self.clearcount = self.clearthresh
78 return cleared
79
80
83
84
85 security.declareProtected('View','getCacheTimeout')
87 """return cache timeout"""
88 return self.timeout
89
90
91 security.declareProtected('View','getCacheClearthresh')
93 """return cache clearthresh"""
94 return self.clearthresh
95
96
98
103
105 if self._time + self._timeout < time.time():
106 return 0
107 else:
108 return 1
109
112
114 """Return the time at which this cache object was created"""
115 return self._time
116