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
58
63
64
66 """clean the cache if nessesary"""
67 cleared = 0
68 if self.cache:
69 self.clearcount -= 1
70 if force or self.clearcount < self.clearthresh:
71 for key, value in self.cache.items():
72 if not value.checkTime():
73 cleared = 1
74 del self.cache[key]
75 self.clearcount = self.clearthresh
76 return cleared
77
78
81
82
83 security.declareProtected('View','getCacheTimeout')
85 """return cache timeout"""
86 return self.timeout
87
88
89 security.declareProtected('View','getCacheClearthresh')
91 """return cache clearthresh"""
92 return self.clearthresh
93
94
96
101
103 if self._time + self._timeout < time.time():
104 return 0
105 else:
106 return 1
107
110
112 """Return the time at which this cache object was created"""
113 return self._time
114