1 24 25 package com.rift.coad.lib.cache; 27 28 import java.util.Date ; 30 import java.util.Map ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 34 import org.apache.log4j.Logger; 36 37 import com.rift.coad.lib.configuration.ConfigurationFactory; 39 import com.rift.coad.lib.configuration.Configuration; 40 import com.rift.coad.lib.thread.ThreadStateMonitor; 41 42 43 49 public class KeySyncCache implements Cache { 50 51 54 public static class KeySync { 55 private Date touchTime = null; 57 58 61 public KeySync() { 62 touch(); 63 } 64 65 66 73 public synchronized boolean isExpired(Date expiryDate) { 74 return (touchTime.getTime() < expiryDate.getTime()); 75 } 76 77 78 81 public synchronized void touch() { 82 touchTime = new Date (); 83 } 84 } 85 86 private final static String CACHE_EXPIRY_TIME = "key_sync_cache_expiry"; 88 private final static long CACHE_EXPIRY_TIME_DEFAULT = 30 * 60 * 1000; 89 90 protected static Logger log = 92 Logger.getLogger(KeySyncCache.class.getName()); 93 94 private ThreadStateMonitor status = new ThreadStateMonitor(); 96 private Map keySyncMap = new HashMap (); 97 private Configuration config = null; 98 private long cacheExpiryTime = 0; 99 100 101 106 public KeySyncCache() throws CacheException { 107 try { 108 Configuration config = ConfigurationFactory.getInstance(). 109 getConfig(KeySyncCache.class); 110 cacheExpiryTime = config.getLong(CACHE_EXPIRY_TIME, 111 CACHE_EXPIRY_TIME_DEFAULT); 112 } catch (Exception ex) { 113 log.error("Failed to start the KeySyncCache object because : " + 114 ex.getMessage(),ex); 115 throw new CacheException( 116 "Failed to start the KeySyncCache object because : " + 117 ex.getMessage(),ex); 118 } 119 } 120 121 122 128 public KeySync getKeySync(Object key) throws CacheException { 129 checkStatus(); 130 synchronized(keySyncMap) { 131 if (!keySyncMap.containsKey(key)) { 132 keySyncMap.put(key,new KeySync()); 133 } 134 KeySync keySync = (KeySync)keySyncMap.get(key); 135 keySync.touch(); 136 return keySync; 137 } 138 } 139 140 141 144 public void garbageCollect() { 145 Map keySyncMap = new HashMap (); 146 synchronized(this.keySyncMap) { 147 keySyncMap.putAll(this.keySyncMap); 148 } 149 Date expiryDate = new Date (new Date ().getTime() - cacheExpiryTime); 150 for (Iterator iter = keySyncMap.keySet().iterator();iter.hasNext();) { 151 Object key = iter.next(); 152 KeySync keySync = (KeySync)keySyncMap.get(key); 153 synchronized(this.keySyncMap) { 154 if (keySync.isExpired(expiryDate)) { 155 this.keySyncMap.remove(key); 156 } 157 } 158 } 159 } 160 161 162 165 public void clear() { 166 status.terminate(false); 167 synchronized(this.keySyncMap) { 168 this.keySyncMap.clear(); 169 } 170 } 171 172 173 179 public boolean contains(Object cacheEntry) { 180 synchronized (keySyncMap) { 181 return keySyncMap.containsKey(cacheEntry); 182 } 183 } 184 185 186 192 private void checkStatus() throws CacheException { 193 if (status.isTerminated()) { 194 throw new CacheException("The cache has been terminated."); 195 } 196 } 197 198 } 199 | Popular Tags |