1 16 17 package org.springframework.cache.ehcache; 18 19 import java.io.IOException ; 20 21 import net.sf.ehcache.Cache; 22 import net.sf.ehcache.CacheException; 23 import net.sf.ehcache.CacheManager; 24 import net.sf.ehcache.Ehcache; 25 import net.sf.ehcache.constructs.blocking.BlockingCache; 26 import net.sf.ehcache.constructs.blocking.CacheEntryFactory; 27 import net.sf.ehcache.constructs.blocking.SelfPopulatingCache; 28 import net.sf.ehcache.constructs.blocking.UpdatingCacheEntryFactory; 29 import net.sf.ehcache.constructs.blocking.UpdatingSelfPopulatingCache; 30 import net.sf.ehcache.store.MemoryStoreEvictionPolicy; 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 34 import org.springframework.beans.factory.BeanNameAware; 35 import org.springframework.beans.factory.FactoryBean; 36 import org.springframework.beans.factory.InitializingBean; 37 import org.springframework.util.Assert; 38 39 63 public class EhCacheFactoryBean implements FactoryBean, BeanNameAware, InitializingBean { 64 65 protected final Log logger = LogFactory.getLog(getClass()); 66 67 private CacheManager cacheManager; 68 69 private String cacheName; 70 71 private int maxElementsInMemory = 10000; 72 73 private MemoryStoreEvictionPolicy memoryStoreEvictionPolicy = MemoryStoreEvictionPolicy.LRU; 74 75 private boolean overflowToDisk = true; 76 77 private String diskStorePath; 78 79 private boolean eternal = false; 80 81 private int timeToLive = 120; 82 83 private int timeToIdle = 120; 84 85 private boolean diskPersistent = false; 86 87 private int diskExpiryThreadIntervalSeconds = 120; 88 89 private boolean blocking = false; 90 91 private CacheEntryFactory cacheEntryFactory; 92 93 private String beanName; 94 95 private Ehcache cache; 96 97 98 109 public void setCacheManager(CacheManager cacheManager) { 110 this.cacheManager = cacheManager; 111 } 112 113 117 public void setCacheName(String cacheName) { 118 this.cacheName = cacheName; 119 } 120 121 125 public void setMaxElementsInMemory(int maxElementsInMemory) { 126 this.maxElementsInMemory = maxElementsInMemory; 127 } 128 129 135 public void setMemoryStoreEvictionPolicy(MemoryStoreEvictionPolicy memoryStoreEvictionPolicy) { 136 Assert.notNull(memoryStoreEvictionPolicy, "memoryStoreEvictionPolicy must not be null"); 137 this.memoryStoreEvictionPolicy = memoryStoreEvictionPolicy; 138 } 139 140 144 public void setOverflowToDisk(boolean overflowToDisk) { 145 this.overflowToDisk = overflowToDisk; 146 } 147 148 152 public void setDiskStorePath(String diskStorePath) { 153 this.diskStorePath = diskStorePath; 154 } 155 156 160 public void setEternal(boolean eternal) { 161 this.eternal = eternal; 162 } 163 164 169 public void setTimeToLive(int timeToLive) { 170 this.timeToLive = timeToLive; 171 } 172 173 178 public void setTimeToIdle(int timeToIdle) { 179 this.timeToIdle = timeToIdle; 180 } 181 182 186 public void setDiskPersistent(boolean diskPersistent) { 187 this.diskPersistent = diskPersistent; 188 } 189 190 194 public void setDiskExpiryThreadIntervalSeconds(int diskExpiryThreadIntervalSeconds) { 195 this.diskExpiryThreadIntervalSeconds = diskExpiryThreadIntervalSeconds; 196 } 197 198 206 public void setBlocking(boolean blocking) { 207 this.blocking = blocking; 208 } 209 210 224 public void setCacheEntryFactory(CacheEntryFactory cacheEntryFactory) { 225 this.cacheEntryFactory = cacheEntryFactory; 226 } 227 228 public void setBeanName(String name) { 229 this.beanName = name; 230 } 231 232 233 public void afterPropertiesSet() throws CacheException, IOException { 234 if (this.cacheManager == null) { 236 if (logger.isDebugEnabled()) { 237 logger.debug("Using default EHCache CacheManager for cache region '" + this.cacheName + "'"); 238 } 239 this.cacheManager = CacheManager.getInstance(); 240 } 241 242 if (this.cacheName == null) { 244 this.cacheName = this.beanName; 245 } 246 247 if (this.cacheManager.cacheExists(this.cacheName)) { 250 if (logger.isDebugEnabled()) { 251 logger.debug("Using existing EHCache cache region '" + this.cacheName + "'"); 252 } 253 this.cache = this.cacheManager.getEhcache(this.cacheName); 254 } 255 else { 256 if (logger.isDebugEnabled()) { 257 logger.debug("Creating new EHCache cache region '" + this.cacheName + "'"); 258 } 259 Cache rawCache = createCache(); 260 this.cacheManager.addCache(rawCache); 261 Ehcache decoratedCache = decorateCache(rawCache); 262 this.cacheManager.replaceCacheWithDecoratedCache(rawCache, decoratedCache); 263 this.cache = decoratedCache; 264 } 265 } 266 267 270 private Cache createCache() { 271 return new Cache( 272 this.cacheName, this.maxElementsInMemory, this.memoryStoreEvictionPolicy, 273 this.overflowToDisk, this.diskStorePath, this.eternal, this.timeToLive, this.timeToIdle, 274 this.diskPersistent, this.diskExpiryThreadIntervalSeconds, null); 275 } 276 277 283 protected Ehcache decorateCache(Cache cache) { 284 if (this.cacheEntryFactory != null) { 285 if (this.cacheEntryFactory instanceof UpdatingCacheEntryFactory) { 286 return new UpdatingSelfPopulatingCache(cache, (UpdatingCacheEntryFactory) this.cacheEntryFactory); 287 } 288 else { 289 return new SelfPopulatingCache(cache, this.cacheEntryFactory); 290 } 291 } 292 if (this.blocking) { 293 return new BlockingCache(cache); 294 } 295 return cache; 296 } 297 298 299 public Object getObject() { 300 return this.cache; 301 } 302 303 public Class getObjectType() { 304 return (this.cache != null ? this.cache.getClass() : Ehcache.class); 305 } 306 307 public boolean isSingleton() { 308 return true; 309 } 310 311 } 312 | Popular Tags |