|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| package com.opensymphony.oscache.base.algorithm; |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| import com.opensymphony.oscache.base.CacheEntry; |
|
40 |
| import com.opensymphony.oscache.base.persistence.CachePersistenceException; |
|
41 |
| import com.opensymphony.oscache.base.persistence.PersistenceListener; |
|
42 |
| |
|
43 |
| import org.apache.commons.logging.Log; |
|
44 |
| import org.apache.commons.logging.LogFactory; |
|
45 |
| |
|
46 |
| import java.io.IOException; |
|
47 |
| import java.io.Serializable; |
|
48 |
| |
|
49 |
| import java.util.*; |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
| public abstract class AbstractConcurrentReadCache extends AbstractMap implements Map, Cloneable, Serializable { |
|
154 |
| |
|
155 |
| |
|
156 |
| |
|
157 |
| |
|
158 |
| public static int DEFAULT_INITIAL_CAPACITY = 32; |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
| |
|
165 |
| |
|
166 |
| private static final int MINIMUM_CAPACITY = 4; |
|
167 |
| |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
| |
|
174 |
| private static final int MAXIMUM_CAPACITY = 1 << 30; |
|
175 |
| |
|
176 |
| |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
| public static final float DEFAULT_LOAD_FACTOR = 0.75f; |
|
181 |
| |
|
182 |
| |
|
183 |
| protected static final String NULL = "_nul!~"; |
|
184 |
| protected static Log log = LogFactory.getLog(AbstractConcurrentReadCache.class); |
|
185 |
| |
|
186 |
| |
|
187 |
| |
|
188 |
| |
|
189 |
| |
|
190 |
| |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
| |
|
197 |
| |
|
198 |
| |
|
199 |
| |
|
200 |
| |
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
| |
|
210 |
| |
|
211 |
| |
|
212 |
| |
|
213 |
| |
|
214 |
| |
|
215 |
| protected final Boolean barrierLock = new Boolean(true); |
|
216 |
| |
|
217 |
| |
|
218 |
| |
|
219 |
| |
|
220 |
| protected transient Object lastWrite; |
|
221 |
| |
|
222 |
| |
|
223 |
| |
|
224 |
| |
|
225 |
| protected transient Entry[] table; |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
| protected transient int count; |
|
231 |
| |
|
232 |
| |
|
233 |
| |
|
234 |
| |
|
235 |
| protected PersistenceListener persistenceListener = null; |
|
236 |
| |
|
237 |
| |
|
238 |
| |
|
239 |
| |
|
240 |
| protected boolean memoryCaching = true; |
|
241 |
| |
|
242 |
| |
|
243 |
| |
|
244 |
| |
|
245 |
| protected boolean unlimitedDiskCache = false; |
|
246 |
| |
|
247 |
| |
|
248 |
| |
|
249 |
| |
|
250 |
| |
|
251 |
| |
|
252 |
| protected float loadFactor; |
|
253 |
| |
|
254 |
| |
|
255 |
| |
|
256 |
| |
|
257 |
| protected final int DEFAULT_MAX_ENTRIES = 100; |
|
258 |
| |
|
259 |
| |
|
260 |
| |
|
261 |
| |
|
262 |
| protected final int UNLIMITED = 2147483646; |
|
263 |
| protected transient Collection values = null; |
|
264 |
| |
|
265 |
| |
|
266 |
| |
|
267 |
| |
|
268 |
| |
|
269 |
| |
|
270 |
| |
|
271 |
| protected HashMap groups = new HashMap(); |
|
272 |
| protected transient Set entrySet = null; |
|
273 |
| |
|
274 |
| |
|
275 |
| protected transient Set keySet = null; |
|
276 |
| |
|
277 |
| |
|
278 |
| |
|
279 |
| |
|
280 |
| protected int maxEntries = DEFAULT_MAX_ENTRIES; |
|
281 |
| |
|
282 |
| |
|
283 |
| |
|
284 |
| |
|
285 |
| |
|
286 |
| |
|
287 |
| |
|
288 |
| protected int threshold; |
|
289 |
| |
|
290 |
| |
|
291 |
| |
|
292 |
| |
|
293 |
| private boolean overflowPersistence = false; |
|
294 |
| |
|
295 |
| |
|
296 |
| |
|
297 |
| |
|
298 |
| |
|
299 |
| |
|
300 |
| |
|
301 |
| |
|
302 |
| |
|
303 |
| |
|
304 |
| |
|
305 |
132
| public AbstractConcurrentReadCache(int initialCapacity, float loadFactor) {
|
|
306 |
132
| if (loadFactor <= 0) {
|
|
307 |
0
| throw new IllegalArgumentException("Illegal Load factor: " + loadFactor);
|
|
308 |
| } |
|
309 |
| |
|
310 |
132
| this.loadFactor = loadFactor;
|
|
311 |
| |
|
312 |
132
| int cap = p2capacity(initialCapacity);
|
|
313 |
132
| table = new Entry[cap];
|
|
314 |
132
| threshold = (int) (cap * loadFactor);
|
|
315 |
| } |
|
316 |
| |
|
317 |
| |
|
318 |
| |
|
319 |
| |
|
320 |
| |
|
321 |
| |
|
322 |
| |
|
323 |
| |
|
324 |
| |
|
325 |
| |
|
326 |
0
| public AbstractConcurrentReadCache(int initialCapacity) {
|
|
327 |
0
| this(initialCapacity, DEFAULT_LOAD_FACTOR);
|
|
328 |
| } |
|
329 |
| |
|
330 |
| |
|
331 |
| |
|
332 |
| |
|
333 |
132
| public AbstractConcurrentReadCache() {
|
|
334 |
132
| this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
|
|
335 |
| } |
|
336 |
| |
|
337 |
| |
|
338 |
| |
|
339 |
| |
|
340 |
| |
|
341 |
| |
|
342 |
0
| public AbstractConcurrentReadCache(Map t) {
|
|
343 |
0
| this(Math.max(2 * t.size(), 11), DEFAULT_LOAD_FACTOR);
|
|
344 |
0
| putAll(t);
|
|
345 |
| } |
|
346 |
| |
|
347 |
| |
|
348 |
| |
|
349 |
| |
|
350 |
| |
|
351 |
| |
|
352 |
0
| public synchronized boolean isEmpty() {
|
|
353 |
0
| return count == 0;
|
|
354 |
| } |
|
355 |
| |
|
356 |
| |
|
357 |
| |
|
358 |
| |
|
359 |
| |
|
360 |
| |
|
361 |
| |
|
362 |
| |
|
363 |
| |
|
364 |
36
| public Set getGroup(String groupName) {
|
|
365 |
36
| if (log.isDebugEnabled()) {
|
|
366 |
0
| log.debug("getGroup called (group=" + groupName + ")");
|
|
367 |
| } |
|
368 |
| |
|
369 |
36
| Set groupEntries = null;
|
|
370 |
| |
|
371 |
36
| if (memoryCaching && (groups != null)) {
|
|
372 |
27
| groupEntries = (Set) getGroupForReading(groupName);
|
|
373 |
| } |
|
374 |
| |
|
375 |
36
| if (groupEntries == null) {
|
|
376 |
| |
|
377 |
12
| groupEntries = persistRetrieveGroup(groupName);
|
|
378 |
| } |
|
379 |
| |
|
380 |
36
| return groupEntries;
|
|
381 |
| } |
|
382 |
| |
|
383 |
| |
|
384 |
| |
|
385 |
| |
|
386 |
80
| public void setMaxEntries(int newLimit) {
|
|
387 |
80
| if (newLimit > 0) {
|
|
388 |
64
| maxEntries = newLimit;
|
|
389 |
| |
|
390 |
64
| synchronized (this) {
|
|
391 |
| |
|
392 |
64
| while (size() > maxEntries) {
|
|
393 |
16
| remove(removeItem(), false);
|
|
394 |
| } |
|
395 |
| } |
|
396 |
| } else { |
|
397 |
| |
|
398 |
16
| throw new IllegalArgumentException("Cache maximum number of entries must be at least 1");
|
|
399 |
| } |
|
400 |
| } |
|
401 |
| |
|
402 |
| |
|
403 |
| |
|
404 |
| |
|
405 |
32
| public int getMaxEntries() {
|
|
406 |
32
| return maxEntries;
|
|
407 |
| } |
|
408 |
| |
|
409 |
| |
|
410 |
| |
|
411 |
| |
|
412 |
132
| public void setMemoryCaching(boolean memoryCaching) {
|
|
413 |
132
| this.memoryCaching = memoryCaching;
|
|
414 |
| } |
|
415 |
| |
|
416 |
| |
|
417 |
| |
|
418 |
| |
|
419 |
24
| public boolean isMemoryCaching() {
|
|
420 |
24
| return memoryCaching;
|
|
421 |
| } |
|
422 |
| |
|
423 |
| |
|
424 |
| |
|
425 |
| |
|
426 |
102
| public void setPersistenceListener(PersistenceListener listener) {
|
|
427 |
102
| this.persistenceListener = listener;
|
|
428 |
| } |
|
429 |
| |
|
430 |
| |
|
431 |
| |
|
432 |
| |
|
433 |
64
| public PersistenceListener getPersistenceListener() {
|
|
434 |
64
| return persistenceListener;
|
|
435 |
| } |
|
436 |
| |
|
437 |
| |
|
438 |
| |
|
439 |
| |
|
440 |
108
| public void setUnlimitedDiskCache(boolean unlimitedDiskCache) {
|
|
441 |
108
| this.unlimitedDiskCache = unlimitedDiskCache;
|
|
442 |
| } |
|
443 |
| |
|
444 |
| |
|
445 |
| |
|
446 |
| |
|
447 |
0
| public boolean isUnlimitedDiskCache() {
|
|
448 |
0
| return unlimitedDiskCache;
|
|
449 |
| } |
|
450 |
| |
|
451 |
| |
|
452 |
| |
|
453 |
| |
|
454 |
| |
|
455 |
| |
|
456 |
16
| public boolean isOverflowPersistence() {
|
|
457 |
16
| return this.overflowPersistence;
|
|
458 |
| } |
|
459 |
| |
|
460 |
| |
|
461 |
| |
|
462 |
| |
|
463 |
| |
|
464 |
| |
|
465 |
132
| public void setOverflowPersistence(boolean overflowPersistence) {
|
|
466 |
132
| this.overflowPersistence = overflowPersistence;
|
|
467 |
| } |
|
468 |
| |
|
469 |
| |
|
470 |
| |
|
471 |
| |
|
472 |
0
| public synchronized int capacity() {
|
|
473 |
0
| return table.length;
|
|
474 |
| } |
|
475 |
| |
|
476 |
| |
|
477 |
| |
|
478 |
| |
|
479 |
204
| public synchronized void clear() {
|
|
480 |
204
| Entry[] tab = table;
|
|
481 |
| |
|
482 |
204
| for (int i = 0; i < tab.length; ++i) {
|
|
483 |
| |
|
484 |
6528
| for (Entry e = tab[i]; e != null; e = e.next) {
|
|
485 |
204
| e.value = null;
|
|
486 |
| |
|
487 |
| |
|
488 |
204
| itemRemoved(e.key);
|
|
489 |
| |
|
490 |
| |
|
491 |
| } |
|
492 |
| |
|
493 |
6528
| tab[i] = null;
|
|
494 |
| } |
|
495 |
| |
|
496 |
| |
|
497 |
204
| persistClear();
|
|
498 |
| |
|
499 |
204
| count = 0;
|
|
500 |
|