1 23 24 package com.rift.coad.lib.cache; 26 27 import java.util.Iterator ; 29 import java.util.Map ; 30 import java.util.HashMap ; 31 32 import com.rift.coad.lib.thread.ThreadStateMonitor; 34 35 40 public class KeySyncCacheManager implements Cache { 41 42 private ThreadStateMonitor status = new ThreadStateMonitor(); 44 private Map keySyncCaches = new HashMap (); 45 46 49 public KeySyncCacheManager() { 50 } 51 52 53 60 public KeySyncCache getKeySyncCache(Object identifier) throws CacheException { 61 checkStatus(); 62 synchronized (keySyncCaches) { 63 if (!keySyncCaches.containsKey(identifier)) { 64 keySyncCaches.put(identifier,new KeySyncCache()); 65 } 66 return (KeySyncCache)keySyncCaches.get(identifier); 67 } 68 } 69 70 71 74 public void garbageCollect() { 75 Map keySyncCaches = new HashMap (); 76 synchronized (this.keySyncCaches) { 77 keySyncCaches.putAll(this.keySyncCaches); 78 } 79 for (Iterator iter = keySyncCaches.keySet().iterator(); iter.hasNext();) { 80 KeySyncCache keySyncCache = (KeySyncCache)keySyncCaches.get( 81 iter.next()); 82 keySyncCache.garbageCollect(); 83 } 84 } 85 86 87 90 public void clear() { 91 status.terminate(false); 92 Map keySyncCaches = new HashMap (); 93 synchronized (this.keySyncCaches) { 94 keySyncCaches.putAll(this.keySyncCaches); 95 this.keySyncCaches.clear(); 96 } 97 for (Iterator iter = keySyncCaches.keySet().iterator(); iter.hasNext();) { 98 KeySyncCache keySyncCache = (KeySyncCache)keySyncCaches.get( 99 iter.next()); 100 keySyncCache.clear(); 101 } 102 } 103 104 105 111 public boolean contains(Object cacheEntry) { 112 synchronized (keySyncCaches) { 113 return keySyncCaches.containsKey(cacheEntry); 114 } 115 } 116 117 118 123 private void checkStatus() throws CacheException { 124 if (status.isTerminated()) { 125 throw new CacheException("The cache has been terminated."); 126 } 127 } 128 } 129 | Popular Tags |