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 """
44 Any page that contains a lot of graphs is practically guaranteed to
45 cause read conflict errors on PObjectCache. Fortunately it's really
46 easy to do good conflict resolution on this simple object.
47 """
48 mergedstate = oldstate
49 mergedstate['cache'].update(savedstate['cache'])
50 mergedstate['cache'].update(newstate['cache'])
51 return mergedstate
52
53
55 """check to see if key is in cache return None if not"""
56 if key in self.cache:
57 cobj = self.cache[key]
58 if cobj.checkTime():
59 return cobj.getObj()
60 else:
61 del self.cache[key]
62 self._p_changed = 1
63 return None
64
65
67 """add an object to the cache"""
68 cobj = CacheObj(obj, self.timeout)
69 self.cache[key] = cobj
70 self._p_changed = 1
71
76
77
79 """clean the cache if nessesary"""
80 cleared = 0
81 if self.cache:
82 self.clearcount -= 1
83 if force or self.clearcount < self.clearthresh:
84 for key, value in self.cache.items():
85 if not value.checkTime():
86 cleared = 1
87 del self.cache[key]
88 self._p_changed = 1
89 self.clearcount = self.clearthresh
90 return cleared
91
92
95
96
97 security.declareProtected('View','getCacheTimeout')
99 """return cache timeout"""
100 return self.timeout
101
102
103 security.declareProtected('View','getCacheClearthresh')
105 """return cache clearthresh"""
106 return self.clearthresh
107
108
110
115
117 if self._time + self._timeout < time.time():
118 return 0
119 else:
120 return 1
121
124
126 """Return the time at which this cache object was created"""
127 return self._time
128