@Test public void testEhCacheFactoryBeanWithSelfPopulatingCache() { EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean(); cacheManagerFb.afterPropertiesSet(); try { CacheManager cm = cacheManagerFb.getObject(); EhCacheFactoryBean cacheFb = new EhCacheFactoryBean(); cacheFb.setCacheManager(cm); cacheFb.setCacheName("myCache1"); cacheFb.setCacheEntryFactory(key -> key); assertEquals(cacheFb.getObjectType(), SelfPopulatingCache.class); cacheFb.afterPropertiesSet(); Ehcache myCache1 = cm.getEhcache("myCache1"); assertTrue(myCache1 instanceof SelfPopulatingCache); assertEquals("myKey1", myCache1.get("myKey1").getObjectValue()); } finally { cacheManagerFb.destroy(); } }
@Test public void testEhCacheFactoryBeanWithUpdatingSelfPopulatingCache() { EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean(); cacheManagerFb.afterPropertiesSet(); try { CacheManager cm = cacheManagerFb.getObject(); EhCacheFactoryBean cacheFb = new EhCacheFactoryBean(); cacheFb.setCacheManager(cm); cacheFb.setCacheName("myCache1"); cacheFb.setCacheEntryFactory(new UpdatingCacheEntryFactory() { @Override public Object createEntry(Object key) { return key; } @Override public void updateEntryValue(Object key, Object value) { } }); assertEquals(cacheFb.getObjectType(), UpdatingSelfPopulatingCache.class); cacheFb.afterPropertiesSet(); Ehcache myCache1 = cm.getEhcache("myCache1"); assertTrue(myCache1 instanceof UpdatingSelfPopulatingCache); assertEquals("myKey1", myCache1.get("myKey1").getObjectValue()); } finally { cacheManagerFb.destroy(); } }
@Test public void testEhCacheFactoryBeanWithBlockingCache() { EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean(); cacheManagerFb.afterPropertiesSet(); try { CacheManager cm = cacheManagerFb.getObject(); EhCacheFactoryBean cacheFb = new EhCacheFactoryBean(); cacheFb.setCacheManager(cm); cacheFb.setCacheName("myCache1"); cacheFb.setBlocking(true); assertEquals(cacheFb.getObjectType(), BlockingCache.class); cacheFb.afterPropertiesSet(); Ehcache myCache1 = cm.getEhcache("myCache1"); assertTrue(myCache1 instanceof BlockingCache); } finally { cacheManagerFb.destroy(); } }
cacheFb.setCacheName("myCache1"); cacheFb.afterPropertiesSet(); cache = (Cache) cacheFb.getObject(); cacheFb.setCacheManager(cacheManagerFb.getObject()); cacheFb.setCacheName("undefinedCache"); cacheFb.afterPropertiesSet(); cache = (Cache) cacheFb.getObject();
private Ehcache buildCache(final TicketDefinition ticketDefinition) { val cache = casProperties.getTicket().getRegistry().getEhcache(); val configExists = ResourceUtils.doesResourceExist(cache.getConfigLocation()); val ehcacheProperties = casProperties.getTicket().getRegistry().getEhcache(); val bean = new EhCacheFactoryBean(); bean.setCacheName(ticketDefinition.getProperties().getStorageName()); LOGGER.debug("Constructing Ehcache cache [{}]", bean.getName()); if (configExists) { bean.setCacheEventListeners(CollectionUtils.wrapSet(ticketRMISynchronousCacheReplicator())); bean.setBootstrapCacheLoader(ticketCacheBootstrapCacheLoader()); } else { LOGGER.warn("In registering ticket definition [{}], Ehcache configuration file [{}] cannot be found " + "so no cache event listeners will be configured to bootstrap. " + "The ticket registry will operate in standalone mode", ticketDefinition.getPrefix(), cache.getConfigLocation()); } bean.setTimeToIdle((int) ticketDefinition.getProperties().getStorageTimeout()); bean.setTimeToLive((int) ticketDefinition.getProperties().getStorageTimeout()); bean.setDiskExpiryThreadIntervalSeconds(ehcacheProperties.getDiskExpiryThreadIntervalSeconds()); bean.setEternal(ehcacheProperties.isEternal()); bean.setMaxEntriesLocalHeap(ehcacheProperties.getMaxElementsInMemory()); bean.setMaxEntriesInCache(ehcacheProperties.getMaxElementsInCache()); bean.setMaxEntriesLocalDisk(ehcacheProperties.getMaxElementsOnDisk()); bean.setMemoryStoreEvictionPolicy(ehcacheProperties.getMemoryStoreEvictionPolicy()); val c = new PersistenceConfiguration(); c.strategy(ehcacheProperties.getPersistence()); c.setSynchronousWrites(ehcacheProperties.isSynchronousWrites()); bean.persistence(c); bean.afterPropertiesSet(); return bean.getObject(); }