|
1 <!DOCTYPE html> |
|
2 <html><head> |
|
3 <title>eric7.EricUtilities.EricCache</title> |
|
4 <meta charset="UTF-8"> |
|
5 <link rel="stylesheet" href="styles.css"> |
|
6 </head> |
|
7 <body> |
|
8 <a NAME="top" ID="top"></a> |
|
9 <h1>eric7.EricUtilities.EricCache</h1> |
|
10 |
|
11 <p> |
|
12 Module implementing classes used for caching objects. |
|
13 </p> |
|
14 <h3>Global Attributes</h3> |
|
15 |
|
16 <table> |
|
17 <tr><td>None</td></tr> |
|
18 </table> |
|
19 <h3>Classes</h3> |
|
20 |
|
21 <table> |
|
22 |
|
23 <tr> |
|
24 <td><a href="#EricCache">EricCache</a></td> |
|
25 <td>Class implementing a LRU cache of a specific size.</td> |
|
26 </tr> |
|
27 </table> |
|
28 <h3>Functions</h3> |
|
29 |
|
30 <table> |
|
31 <tr><td>None</td></tr> |
|
32 </table> |
|
33 <hr /> |
|
34 <hr /> |
|
35 <a NAME="EricCache" ID="EricCache"></a> |
|
36 <h2>EricCache</h2> |
|
37 |
|
38 <p> |
|
39 Class implementing a LRU cache of a specific size. |
|
40 </p> |
|
41 <p> |
|
42 If the maximum number of entries is exceeded, the least recently used item |
|
43 is removed from the cache. A cache hit moves the entry to the front of the |
|
44 cache. |
|
45 </p> |
|
46 <h3>Derived from</h3> |
|
47 None |
|
48 <h3>Class Attributes</h3> |
|
49 |
|
50 <table> |
|
51 <tr><td>None</td></tr> |
|
52 </table> |
|
53 <h3>Class Methods</h3> |
|
54 |
|
55 <table> |
|
56 <tr><td>None</td></tr> |
|
57 </table> |
|
58 <h3>Methods</h3> |
|
59 |
|
60 <table> |
|
61 |
|
62 <tr> |
|
63 <td><a href="#EricCache.__init__">EricCache</a></td> |
|
64 <td>Constructor</td> |
|
65 </tr> |
|
66 <tr> |
|
67 <td><a href="#EricCache.__adjustToSize">__adjustToSize</a></td> |
|
68 <td>Private method to adjust the cache to its size.</td> |
|
69 </tr> |
|
70 <tr> |
|
71 <td><a href="#EricCache.__moveLast">__moveLast</a></td> |
|
72 <td>Private method to move a cached item to the MRU position.</td> |
|
73 </tr> |
|
74 <tr> |
|
75 <td><a href="#EricCache.__pruneCache">__pruneCache</a></td> |
|
76 <td>Private slot to prune outdated cache entries and restart the timer.</td> |
|
77 </tr> |
|
78 <tr> |
|
79 <td><a href="#EricCache.add">add</a></td> |
|
80 <td>Public method to add an item to the cache.</td> |
|
81 </tr> |
|
82 <tr> |
|
83 <td><a href="#EricCache.clear">clear</a></td> |
|
84 <td>Public method to clear the cache.</td> |
|
85 </tr> |
|
86 <tr> |
|
87 <td><a href="#EricCache.get">get</a></td> |
|
88 <td>Public method to get an entry from the cache given its key.</td> |
|
89 </tr> |
|
90 <tr> |
|
91 <td><a href="#EricCache.getMaximumCacheTime">getMaximumCacheTime</a></td> |
|
92 <td>Public method to get the maximum time entries may exist in the cache.</td> |
|
93 </tr> |
|
94 <tr> |
|
95 <td><a href="#EricCache.getSize">getSize</a></td> |
|
96 <td>Public method to get the maximum size of the cache.</td> |
|
97 </tr> |
|
98 <tr> |
|
99 <td><a href="#EricCache.info">info</a></td> |
|
100 <td>Public method to get some information about the cache.</td> |
|
101 </tr> |
|
102 <tr> |
|
103 <td><a href="#EricCache.length">length</a></td> |
|
104 <td>Public method to get the current length of the cache.</td> |
|
105 </tr> |
|
106 <tr> |
|
107 <td><a href="#EricCache.remove">remove</a></td> |
|
108 <td>Public method to remove an item from the cache.</td> |
|
109 </tr> |
|
110 <tr> |
|
111 <td><a href="#EricCache.reset">reset</a></td> |
|
112 <td>Public method to reset the cache.</td> |
|
113 </tr> |
|
114 <tr> |
|
115 <td><a href="#EricCache.setMaximumCacheTime">setMaximumCacheTime</a></td> |
|
116 <td>Public method to set the maximum time entries may exist in the cache.</td> |
|
117 </tr> |
|
118 <tr> |
|
119 <td><a href="#EricCache.setSize">setSize</a></td> |
|
120 <td>Public method to change the maximum size of the cache.</td> |
|
121 </tr> |
|
122 </table> |
|
123 <h3>Static Methods</h3> |
|
124 |
|
125 <table> |
|
126 <tr><td>None</td></tr> |
|
127 </table> |
|
128 |
|
129 <a NAME="EricCache.__init__" ID="EricCache.__init__"></a> |
|
130 <h4>EricCache (Constructor)</h4> |
|
131 <b>EricCache</b>(<i>size=100</i>) |
|
132 |
|
133 <p> |
|
134 Constructor |
|
135 </p> |
|
136 <dl> |
|
137 |
|
138 <dt><i>size</i> (int)</dt> |
|
139 <dd> |
|
140 maximum number of entries that may be stored in the cache |
|
141 </dd> |
|
142 </dl> |
|
143 <dl> |
|
144 |
|
145 <dt>Raises <b>ValueError</b>:</dt> |
|
146 <dd> |
|
147 raised to indicate an illegal 'size' parameter |
|
148 </dd> |
|
149 </dl> |
|
150 <a NAME="EricCache.__adjustToSize" ID="EricCache.__adjustToSize"></a> |
|
151 <h4>EricCache.__adjustToSize</h4> |
|
152 <b>__adjustToSize</b>(<i></i>) |
|
153 |
|
154 <p> |
|
155 Private method to adjust the cache to its size. |
|
156 </p> |
|
157 <a NAME="EricCache.__moveLast" ID="EricCache.__moveLast"></a> |
|
158 <h4>EricCache.__moveLast</h4> |
|
159 <b>__moveLast</b>(<i>key</i>) |
|
160 |
|
161 <p> |
|
162 Private method to move a cached item to the MRU position. |
|
163 </p> |
|
164 <dl> |
|
165 |
|
166 <dt><i>key</i> (any hashable type that can be used as a dict key)</dt> |
|
167 <dd> |
|
168 key of the item to be retrieved |
|
169 </dd> |
|
170 </dl> |
|
171 <a NAME="EricCache.__pruneCache" ID="EricCache.__pruneCache"></a> |
|
172 <h4>EricCache.__pruneCache</h4> |
|
173 <b>__pruneCache</b>(<i></i>) |
|
174 |
|
175 <p> |
|
176 Private slot to prune outdated cache entries and restart the timer. |
|
177 </p> |
|
178 <a NAME="EricCache.add" ID="EricCache.add"></a> |
|
179 <h4>EricCache.add</h4> |
|
180 <b>add</b>(<i>key, item</i>) |
|
181 |
|
182 <p> |
|
183 Public method to add an item to the cache. |
|
184 </p> |
|
185 <p> |
|
186 If the key is already in use, the cached item is replaced by the new |
|
187 one given and is moved to the MRU position |
|
188 </p> |
|
189 <dl> |
|
190 |
|
191 <dt><i>key</i> (any hashable type that can be used as a dict key)</dt> |
|
192 <dd> |
|
193 key of the item to be retrieved |
|
194 </dd> |
|
195 <dt><i>item</i> (object)</dt> |
|
196 <dd> |
|
197 item to be cached under the given key |
|
198 </dd> |
|
199 </dl> |
|
200 <a NAME="EricCache.clear" ID="EricCache.clear"></a> |
|
201 <h4>EricCache.clear</h4> |
|
202 <b>clear</b>(<i></i>) |
|
203 |
|
204 <p> |
|
205 Public method to clear the cache. |
|
206 </p> |
|
207 <a NAME="EricCache.get" ID="EricCache.get"></a> |
|
208 <h4>EricCache.get</h4> |
|
209 <b>get</b>(<i>key</i>) |
|
210 |
|
211 <p> |
|
212 Public method to get an entry from the cache given its key. |
|
213 </p> |
|
214 <p> |
|
215 If the key is present in the cache, it is moved to the MRU position. |
|
216 </p> |
|
217 <dl> |
|
218 |
|
219 <dt><i>key</i> (any hashable type that can be used as a dict key)</dt> |
|
220 <dd> |
|
221 key of the item to be retrieved |
|
222 </dd> |
|
223 </dl> |
|
224 <dl> |
|
225 <dt>Return:</dt> |
|
226 <dd> |
|
227 cached item for the given key or None, if the key is not |
|
228 present |
|
229 </dd> |
|
230 </dl> |
|
231 <dl> |
|
232 <dt>Return Type:</dt> |
|
233 <dd> |
|
234 object or None |
|
235 </dd> |
|
236 </dl> |
|
237 <a NAME="EricCache.getMaximumCacheTime" ID="EricCache.getMaximumCacheTime"></a> |
|
238 <h4>EricCache.getMaximumCacheTime</h4> |
|
239 <b>getMaximumCacheTime</b>(<i></i>) |
|
240 |
|
241 <p> |
|
242 Public method to get the maximum time entries may exist in the cache. |
|
243 </p> |
|
244 <dl> |
|
245 <dt>Return:</dt> |
|
246 <dd> |
|
247 maximum cache time in seconds |
|
248 </dd> |
|
249 </dl> |
|
250 <dl> |
|
251 <dt>Return Type:</dt> |
|
252 <dd> |
|
253 int |
|
254 </dd> |
|
255 </dl> |
|
256 <a NAME="EricCache.getSize" ID="EricCache.getSize"></a> |
|
257 <h4>EricCache.getSize</h4> |
|
258 <b>getSize</b>(<i></i>) |
|
259 |
|
260 <p> |
|
261 Public method to get the maximum size of the cache. |
|
262 </p> |
|
263 <dl> |
|
264 <dt>Return:</dt> |
|
265 <dd> |
|
266 maximum number of entries of the cache |
|
267 </dd> |
|
268 </dl> |
|
269 <dl> |
|
270 <dt>Return Type:</dt> |
|
271 <dd> |
|
272 int |
|
273 </dd> |
|
274 </dl> |
|
275 <a NAME="EricCache.info" ID="EricCache.info"></a> |
|
276 <h4>EricCache.info</h4> |
|
277 <b>info</b>(<i></i>) |
|
278 |
|
279 <p> |
|
280 Public method to get some information about the cache. |
|
281 </p> |
|
282 <dl> |
|
283 <dt>Return:</dt> |
|
284 <dd> |
|
285 dictionary containing the cache info |
|
286 </dd> |
|
287 </dl> |
|
288 <dl> |
|
289 <dt>Return Type:</dt> |
|
290 <dd> |
|
291 dict (with keys "hits", "misses", "maxsize", "currsize") |
|
292 </dd> |
|
293 </dl> |
|
294 <a NAME="EricCache.length" ID="EricCache.length"></a> |
|
295 <h4>EricCache.length</h4> |
|
296 <b>length</b>(<i></i>) |
|
297 |
|
298 <p> |
|
299 Public method to get the current length of the cache. |
|
300 </p> |
|
301 <dl> |
|
302 <dt>Return:</dt> |
|
303 <dd> |
|
304 current length of the cache |
|
305 </dd> |
|
306 </dl> |
|
307 <dl> |
|
308 <dt>Return Type:</dt> |
|
309 <dd> |
|
310 int |
|
311 </dd> |
|
312 </dl> |
|
313 <a NAME="EricCache.remove" ID="EricCache.remove"></a> |
|
314 <h4>EricCache.remove</h4> |
|
315 <b>remove</b>(<i>key</i>) |
|
316 |
|
317 <p> |
|
318 Public method to remove an item from the cache. |
|
319 </p> |
|
320 <dl> |
|
321 |
|
322 <dt><i>key</i> (any hashable type that can be used as a dict key)</dt> |
|
323 <dd> |
|
324 key of the item to be retrieved |
|
325 </dd> |
|
326 </dl> |
|
327 <a NAME="EricCache.reset" ID="EricCache.reset"></a> |
|
328 <h4>EricCache.reset</h4> |
|
329 <b>reset</b>(<i></i>) |
|
330 |
|
331 <p> |
|
332 Public method to reset the cache. |
|
333 </p> |
|
334 <p> |
|
335 This is like clear() but sets the various counters to their initial |
|
336 value as well. |
|
337 </p> |
|
338 <a NAME="EricCache.setMaximumCacheTime" ID="EricCache.setMaximumCacheTime"></a> |
|
339 <h4>EricCache.setMaximumCacheTime</h4> |
|
340 <b>setMaximumCacheTime</b>(<i>time</i>) |
|
341 |
|
342 <p> |
|
343 Public method to set the maximum time entries may exist in the cache. |
|
344 </p> |
|
345 <dl> |
|
346 |
|
347 <dt><i>time</i> (int)</dt> |
|
348 <dd> |
|
349 maximum cache time in seconds |
|
350 </dd> |
|
351 </dl> |
|
352 <a NAME="EricCache.setSize" ID="EricCache.setSize"></a> |
|
353 <h4>EricCache.setSize</h4> |
|
354 <b>setSize</b>(<i>newSize</i>) |
|
355 |
|
356 <p> |
|
357 Public method to change the maximum size of the cache. |
|
358 </p> |
|
359 <dl> |
|
360 |
|
361 <dt><i>newSize</i> (int)</dt> |
|
362 <dd> |
|
363 maximum number of entries that may be stored in the |
|
364 cache |
|
365 </dd> |
|
366 </dl> |
|
367 <div align="right"><a href="#top">Up</a></div> |
|
368 <hr /> |
|
369 </body></html> |