1 16 package com.jdon.model.query.cache; 17 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.concurrent.CopyOnWriteArrayList ; 21 22 import org.apache.log4j.Logger; 23 24 import com.jdon.controller.cache.CacheKey; 25 import com.jdon.controller.cache.CacheKeyFactory; 26 import com.jdon.controller.cache.CacheManager; 27 28 40 public class BlockCacheManager extends CacheKeyFactory { 41 private final static Logger logger = Logger.getLogger(BlockCacheManager.class); 42 43 public final static String CACHE_TYPE_BLOCK = "BLOCK"; 44 45 private CacheManager cacheManager; 46 47 private List <CacheKey> cacheKeys; 48 49 public BlockCacheManager(CacheManager cacheManager) { 50 super(cacheManager); 51 52 this.cacheKeys = new CopyOnWriteArrayList <CacheKey>(); 53 this.cacheManager = cacheManager; 54 55 } 56 57 public List getBlockKeysFromCache(QueryConditonDatakey qckey) { 58 CacheKey cacheKey = getCacheKey(qckey); 59 return (List ) cacheManager.fetchObject(cacheKey); 60 61 } 62 63 public CacheKey getCacheKey(QueryConditonDatakey qckey) { 64 return createCacheKey(qckey, Integer.toString(qckey.getBlockStart())); 65 } 66 67 public void saveBlockKeys(QueryConditonDatakey qckey, List keys) { 68 CacheKey cacheKey = getCacheKey(qckey); 69 cacheManager.putObect(cacheKey, keys); 70 cacheKeys.add(cacheKey); 71 } 72 73 public Integer getAllCountsFromCache(QueryConditonDatakey qckey) { 74 CacheKey cacheKey = getCacheKey(qckey); 75 return (Integer ) cacheManager.fetchObject(cacheKey); 76 } 77 78 public void saveAllCounts(QueryConditonDatakey qckey, Integer allCount) { 79 CacheKey cacheKey = getCacheKey(qckey); 80 cacheManager.putObect(cacheKey, allCount); 81 cacheKeys.add(cacheKey); 82 } 83 84 public CacheKey createCacheKeyImp(Object dataKey, String typeName) { 85 return new CacheKey(CACHE_TYPE_BLOCK, dataKey, typeName); 86 } 87 88 public synchronized void clearCache() { 89 try { 90 Iterator iter = cacheKeys.iterator(); 91 while (iter.hasNext()) { 92 CacheKey cacheKey = (CacheKey) iter.next(); 93 cacheManager.removeObect(cacheKey); 94 } 95 } catch (Exception e) { 96 logger.error(e); 97 } finally { 98 cacheKeys.clear(); 99 } 100 } 101 } 102 | Popular Tags |