1 15 16 package com.jdon.controller.cache; 17 18 import java.util.Collection ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 import java.util.concurrent.ConcurrentHashMap ; 22 23 import com.jdon.container.pico.Startable; 24 import com.jdon.util.Debug; 25 26 39 public class CacheManager implements Startable { 40 public static String module = CacheManager.class.getName(); 41 42 43 private Cache cache; 44 45 50 private Map cacheKeyMap; 51 52 53 public CacheManager(Cache cache) { 54 this.cache = cache; 55 cacheKeyMap = new ConcurrentHashMap (); } 58 59 public void start() { 60 Debug.logVerbose("[JdonFramework]CacheFactory start .....", module); 61 clear(); 62 } 63 64 public void stop() { 65 clear(); 66 cache = null; 67 cacheKeyMap = null; 68 } 69 70 public void clear() { 71 if (cache != null) 72 cache.clear(); 73 if (cacheKeyMap != null) 74 cacheKeyMap.clear(); 75 } 76 77 78 83 public Object fetchObject(StringKey key) { 84 Debug.logVerbose("[JdonFramework]<-cache->try to get cache: " + key, module); 85 Object o = cache.get(key.toString()); 86 if (o != null) 87 Debug.logVerbose("[JdonFramework]<-cache->got it, hashcode=" + o.hashCode(), module); 88 return o; 89 } 90 91 96 public void putObect(StringKey key, Object value) { 97 if (key == null) return; 98 if (!cache.contain(key.toString())){ 99 cache.put(key.toString(), value); 100 Debug.logVerbose("[JdonFramework]<-cache->save cache: " + key + ", cache size:" + cache.size(), 101 module); 102 } 103 } 104 105 109 public synchronized void removeObect(StringKey key) { 110 cache.remove(key.toString()); 111 Debug.logVerbose("[JdonFramework]<-cache->remove the object of " + key + " from cache", 112 module); 113 } 114 115 123 public synchronized void removeCache(Object dataKey, CacheKeyFactory cacheKeyFactory) { 124 if (dataKey == null) 125 return; 126 127 String cacheKeyStr = null; 128 129 Collection dels = cacheKeyFactory.getAllCacheKey(dataKey); 130 Iterator iter = dels.iterator(); 131 while (iter.hasNext()) { 132 cacheKeyStr = (String ) iter.next(); 133 Debug.logVerbose("[JdonFramework]<-cache->remove cache:" + cacheKeyStr, module); 134 removeObject(cacheKeyStr, cacheKeyFactory); 135 } 136 } 137 138 private synchronized void removeObject(String cacheKeyStr, CacheKeyFactory cacheKeyFactory) { 139 try { 140 cache.remove(cacheKeyStr); 141 cacheKeyFactory.removeCacheKey(cacheKeyStr); 142 } catch (Exception e) { 143 Debug.logError("[JdonFramework] remove cache error:" + e + " cacheKey=" + cacheKeyStr, 144 module); 145 } 146 } 147 148 public Map getCacheKeyMap() { 149 return cacheKeyMap; 150 } 151 152 public void setCacheKeyMap(Map cacheKeyMap) { 153 this.cacheKeyMap = cacheKeyMap; 154 } 155 156 public Cache getCache() { 157 return cache; 158 } 159 160 public void setCache(Cache cache) { 161 this.cache = cache; 162 } 163 164 } 165 | Popular Tags |