1 14 15 package com.jdon.model.cache; 16 17 import com.jdon.container.pico.Startable; 18 import com.jdon.controller.cache.CacheKey; 19 import com.jdon.controller.cache.CacheKeyFactory; 20 import com.jdon.controller.cache.CacheManager; 21 import com.jdon.controller.model.ModelIF; 22 import com.jdon.model.config.ModelMapping; 23 import com.jdon.model.factory.ModelHandlerClassBuilder; 24 import com.jdon.util.Debug; 25 26 public class ModelCacheManager implements Startable { 27 28 private final static String module = ModelCacheManager.class.getName(); 29 30 protected CacheManager cacheManager; 31 32 private CacheKeyFactory cacheKeyFactory; 33 34 private ModelHandlerClassBuilder modelFactory; 35 36 public ModelCacheManager(ModelHandlerClassBuilder modelFactory, CacheManager cacheManager) { 37 this.cacheManager = cacheManager; 38 this.modelFactory = modelFactory; 39 this.cacheKeyFactory = new ModelCacheKeyFactory(cacheManager); 40 } 41 42 public void start() { 43 Debug.logVerbose("[JdonFramework]ModelCacheManager start ...", module); 44 } 45 46 public void stop() { 47 48 } 49 50 59 public void setCache(Object dataKey, String formName, ModelIF model) { 60 CacheKey cachKey = getCacheKey(dataKey, formName); 61 if (cachKey != null) 62 saveToCache(cachKey, model); 63 } 64 65 private void saveToCache(CacheKey cachKey, ModelIF model) { 66 if ((model != null) && (model.isCacheable())) { Debug.logVerbose("[JdonFramework]save cache: " + cachKey + " hashCode:" + model.hashCode(), module); 68 cacheManager.putObect(cachKey, model); 69 } 70 } 71 72 79 public void setCache2(Object dataKey, String modelClassName, ModelIF model) { 80 CacheKey cachKey = getCacheKey2(dataKey, modelClassName); 81 if (cachKey != null) 82 saveToCache(cachKey, model); 83 } 84 85 94 public ModelIF getCache(Object dataKey, String formName) { 95 CacheKey cachKey = getCacheKey(dataKey, formName); 96 if (cachKey != null) 97 return getModelFromCache(cachKey); 98 else 99 return null; 100 } 101 102 private ModelIF getModelFromCache(CacheKey cachKey) { 103 104 ModelIF model = (ModelIF) cacheManager.fetchObject(cachKey); 105 if (model != null){ 106 Debug.logVerbose("[JdonFramework]found cache: " + cachKey + " hashCode:" + model.hashCode(), module); 107 if ( model.isModified()){ 108 Debug.logVerbose("[JdonFramework]<-cache-> model is isModified():" + cachKey, module); 109 model = null; 110 } 111 } 112 return model; 113 } 114 115 122 public ModelIF getCache2(Object dataKey, String modelClassName) { 123 CacheKey cachKey = getCacheKey2(dataKey, modelClassName); 124 if (cachKey != null) 125 return getModelFromCache(cachKey); 126 else 127 return null; 128 } 129 130 136 public void removeCache(Object dataKey) { 137 cacheManager.removeCache(dataKey, cacheKeyFactory); 138 } 139 140 147 public void removeCache2(Object dataKey, String modelClassName) { 148 CacheKey cachKey = getCacheKey2(dataKey, modelClassName); 149 if (cachKey != null) 150 cacheManager.removeObect(cachKey); 151 } 152 153 160 public CacheKey getCacheKey(Object dataKey, String formName) { 161 ModelMapping modelMapping = this.modelFactory.getModelMapping(formName); 162 return cacheKeyFactory.createCacheKey(dataKey, modelMapping.getClassName()); 163 } 164 165 public CacheKey getCacheKey2(Object dataKey, String modelClassName) { 166 return cacheKeyFactory.createCacheKey(dataKey, modelClassName); 167 } 168 169 public void clearCache() { 170 cacheManager.clear(); 171 } 172 173 } 174 | Popular Tags |