@Test public void testEhCacheFactoryBeanWithSelfPopulatingCache() throws Exception { EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean(); cacheManagerFb.afterPropertiesSet(); try { CacheManager cm = cacheManagerFb.getObject(); EhCacheFactoryBean cacheFb = new EhCacheFactoryBean(); cacheFb.setCacheManager(cm); cacheFb.setCacheName("myCache1"); cacheFb.setCacheEntryFactory(new CacheEntryFactory() { @Override public Object createEntry(Object key) throws Exception { return key; } }); assertEquals(cacheFb.getObjectType(), SelfPopulatingCache.class); cacheFb.afterPropertiesSet(); Ehcache myCache1 = cm.getEhcache("myCache1"); assertTrue(myCache1 instanceof SelfPopulatingCache); assertEquals("myKey1", myCache1.get("myKey1").getValue()); } finally { cacheManagerFb.destroy(); } }
public void testEhCacheFactoryBeanWithSelfPopulatingCache() throws Exception { EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean(); cacheManagerFb.afterPropertiesSet(); try { CacheManager cm = cacheManagerFb.getObject(); EhCacheFactoryBean cacheFb = new EhCacheFactoryBean(); cacheFb.setCacheManager(cm); cacheFb.setCacheName("myCache1"); cacheFb.setCacheEntryFactory(new CacheEntryFactory() { @Override public Object createEntry(Object key) throws Exception { return key; } }); assertEquals(cacheFb.getObjectType(), SelfPopulatingCache.class); cacheFb.afterPropertiesSet(); Ehcache myCache1 = cm.getEhcache("myCache1"); assertTrue(myCache1 instanceof SelfPopulatingCache); assertEquals("myKey1", myCache1.get("myKey1").getValue()); } finally { cacheManagerFb.destroy(); } }
@Override public <K, V> LoadingBenchmarkCache<K, V> createLoadingCache( final Class<K> _keyType, final Class<V> _valueType, final int _maxElements, final BenchmarkCacheSource<K, V> _source) { MyLoadingBenchmarkCache c = new MyLoadingBenchmarkCache(); Ehcache ehc = new Cache(createCacheConfiguration(_maxElements)); getManager().addCache(ehc); int _cpus = Runtime.getRuntime().availableProcessors(); int _stripes = cpuCount2StripeCount(_cpus * 2); c.cache = new SelfPopulatingCache(ehc, _stripes, new CacheEntryFactory() { @Override public Object createEntry(final Object key) throws Exception { return _source.load((K) key); } }); c.size = _maxElements; return c; }
public static Ehcache createInMemoryCache(String cacheName, CacheEntryFactory entryFactory, int maxElements) { CacheManager manager = CacheManager.getInstance(); Ehcache cache = getCache(cacheName); if (cache == null) { // Create the cache cache = new Cache(cacheName, maxElements, memoryStoreEvictionPolicy, overflowToDisk, diskStorePath, eternal, timeToLive, timeToIdle, diskPersistent, diskExpiryThreadIntervalSeconds, null); // Associate the cacheEntryFactory with the cache SelfPopulatingCache selfPopulatingCache = new SelfPopulatingCache(cache, entryFactory); // Add any additional listener properties if (manager.getCachePeerListener("RMI") != null) { LOG.info("Setting RMI properties"); Properties properties = new Properties(); properties.put("replicateAsynchronously", "true"); properties.put("replicatePuts", "false"); properties.put("replicateUpdates", "true"); properties.put("replicateRemovals", "true"); properties.put("replicateUpdatesViaCopy", "false"); properties.put("asynchronousReplicationIntervalMillis", "1000"); RMICacheReplicatorFactory factory = new RMICacheReplicatorFactory(); CacheEventListener listener = factory.createCacheEventListener(properties); selfPopulatingCache.getCacheEventNotificationService().registerListener(listener); RMIBootstrapCacheLoaderFactory bootstrapFactory = new RMIBootstrapCacheLoaderFactory(); BootstrapCacheLoader bootstrapCacheLoader = bootstrapFactory.createBootstrapCacheLoader(new Properties()); selfPopulatingCache.setBootstrapCacheLoader(bootstrapCacheLoader); LOG.debug("RMI enabled"); } // Make the cache available manager.addCache(selfPopulatingCache); LOG.info("cache created: " + cache.getName()); } return cache; }
public static SelfPopulatingCache createCache(String ehcachename, CacheEntryFactory cu) { Logger.getLogger(SPCacheFactory.class.getName()).log(Level.INFO, "Setting up cache... {0}", ehcachename); MulticastKeepaliveHeartbeatSender.setHeartBeatInterval(1000); Logger.getLogger(SPCacheFactory.class.getName()).log(Level.INFO, "Waiting cluster {0}", manager.getName()); SPCacheFactory.waitForClusterMembership(10, TimeUnit.SECONDS, Collections.singleton(ehcachename), manager); Logger.getLogger(SPCacheFactory.class.getName()).log(Level.INFO, "Cluster connected."); Cache orig_ehcache = manager.getCache(ehcachename); if (orig_ehcache == null) { return null; } orig_ehcache.bootstrap(); SelfPopulatingCache ehcache = new SelfPopulatingCache(orig_ehcache, cu); ehcache.bootstrap(); return ehcache; }
public CacheEntryFactory getCacheEntryFactory() { return cacheEntryFactory; }
/** * Set an EhCache {@link net.sf.ehcache.constructs.blocking.CacheEntryFactory} * to use for a self-populating cache. If such a factory is specified, * the cache will be decorated with EhCache's * {@link net.sf.ehcache.constructs.blocking.SelfPopulatingCache}. * <p>The specified factory can be of type * {@link net.sf.ehcache.constructs.blocking.UpdatingCacheEntryFactory}, * which will lead to the use of an * {@link net.sf.ehcache.constructs.blocking.UpdatingSelfPopulatingCache}. * <p>Note: Any such self-populating cache is automatically a blocking cache. * @see net.sf.ehcache.constructs.blocking.SelfPopulatingCache * @see net.sf.ehcache.constructs.blocking.UpdatingSelfPopulatingCache * @see net.sf.ehcache.constructs.blocking.UpdatingCacheEntryFactory */ public void setCacheEntryFactory(CacheEntryFactory cacheEntryFactory) { this.cacheEntryFactory = cacheEntryFactory; }
/** * Set an EHCache {@link net.sf.ehcache.constructs.blocking.CacheEntryFactory} * to use for a self-populating cache. If such a factory is specified, * the cache will be decorated with EHCache's * {@link net.sf.ehcache.constructs.blocking.SelfPopulatingCache}. * <p>The specified factory can be of type * {@link net.sf.ehcache.constructs.blocking.UpdatingCacheEntryFactory}, * which will lead to the use of an * {@link net.sf.ehcache.constructs.blocking.UpdatingSelfPopulatingCache}. * <p>Note: Any such self-populating cache is automatically a blocking cache. * * @see net.sf.ehcache.constructs.blocking.SelfPopulatingCache * @see net.sf.ehcache.constructs.blocking.UpdatingSelfPopulatingCache * @see net.sf.ehcache.constructs.blocking.UpdatingCacheEntryFactory * @see org.springframework.cache.ehcache.EhCacheFactoryBean#setCacheEntryFactory(net.sf.ehcache.constructs.blocking.CacheEntryFactory) */ public void setCacheEntryFactory(CacheEntryFactory cacheEntryFactory) { this.cacheEntryFactory = cacheEntryFactory; }