01: package org.shiftone.cache.decorator.sync;
02:
03: import org.shiftone.cache.Cache;
04: import org.shiftone.cache.util.reaper.ReapableCache;
05:
06: /**
07: * @version $Revision: 1.3 $
08: * @author <a href="mailto:jeff@shiftone.org">Jeff Drost</a>
09: */
10: public class SyncCache implements Cache, ReapableCache {
11:
12: private final ReapableCache reapableCache;
13: private final Cache cache;
14:
15: public SyncCache(Cache cache) {
16:
17: this .cache = cache;
18:
19: if (cache instanceof ReapableCache) {
20: reapableCache = (ReapableCache) cache;
21: } else {
22: reapableCache = null;
23: }
24: }
25:
26: public void addObject(Object userKey, Object cacheObject) {
27:
28: synchronized (cache) {
29: cache.addObject(userKey, cacheObject);
30: }
31: }
32:
33: public Object getObject(Object key) {
34:
35: synchronized (cache) {
36: return cache.getObject(key);
37: }
38: }
39:
40: public int size() {
41: return cache.size();
42: }
43:
44: public void remove(Object key) {
45:
46: synchronized (cache) {
47: cache.remove(key);
48: }
49: }
50:
51: public void clear() {
52:
53: synchronized (cache) {
54: cache.clear();
55: }
56: }
57:
58: public void removeExpiredElements() {
59:
60: if (reapableCache != null) {
61: synchronized (cache) {
62: reapableCache.removeExpiredElements();
63: }
64: }
65: }
66:
67: public String toString() {
68: return "SycnCache->" + cache;
69: }
70: }
|