1 package org.shiftone.cache.decorator.sync; 2 3 4 5 import org.shiftone.cache.Cache; 6 import org.shiftone.cache.util.reaper.ReapableCache; 7 8 9 13 public class SyncCache implements Cache, ReapableCache 14 { 15 16 private final ReapableCache reapableCache; 17 private final Cache cache; 18 19 public SyncCache(Cache cache) 20 { 21 22 this.cache = cache; 23 24 if (cache instanceof ReapableCache) 25 { 26 reapableCache = (ReapableCache) cache; 27 } 28 else 29 { 30 reapableCache = null; 31 } 32 } 33 34 35 public void addObject(Object userKey, Object cacheObject) 36 { 37 38 synchronized (cache) 39 { 40 cache.addObject(userKey, cacheObject); 41 } 42 } 43 44 45 public Object getObject(Object key) 46 { 47 48 synchronized (cache) 49 { 50 return cache.getObject(key); 51 } 52 } 53 54 55 public int size() 56 { 57 return cache.size(); 58 } 59 60 61 public void remove(Object key) 62 { 63 64 synchronized (cache) 65 { 66 cache.remove(key); 67 } 68 } 69 70 71 public void clear() 72 { 73 74 synchronized (cache) 75 { 76 cache.clear(); 77 } 78 } 79 80 81 public void removeExpiredElements() 82 { 83 84 if (reapableCache != null) 85 { 86 synchronized (cache) 87 { 88 reapableCache.removeExpiredElements(); 89 } 90 } 91 } 92 93 94 public String toString() 95 { 96 return "SycnCache->" + cache; 97 } 98 } 99 | Popular Tags |