/** * Predict the particular {@code Ehcache} implementation that will be returned from * {@link #getObject()} based on logic in {@link #createCache()} and * {@link #decorateCache(Ehcache)} as orchestrated by {@link #afterPropertiesSet()}. */ @Override public Class<? extends Ehcache> getObjectType() { if (this.cache != null) { return this.cache.getClass(); } if (this.cacheEntryFactory != null) { if (this.cacheEntryFactory instanceof UpdatingCacheEntryFactory) { return UpdatingSelfPopulatingCache.class; } else { return SelfPopulatingCache.class; } } if (this.blocking) { return BlockingCache.class; } return Cache.class; }
@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(); } }
/** * Predict the particular {@code Ehcache} implementation that will be returned from * {@link #getObject()} based on logic in {@link #createCache()} and * {@link #decorateCache(Ehcache)} as orchestrated by {@link #afterPropertiesSet()}. */ public Class<? extends Ehcache> getObjectType() { if (this.cache != null) { return this.cache.getClass(); } if (this.cacheEntryFactory != null) { if (this.cacheEntryFactory instanceof UpdatingCacheEntryFactory) { return UpdatingSelfPopulatingCache.class; } else { return SelfPopulatingCache.class; } } if (this.blocking) { return BlockingCache.class; } return Cache.class; }
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; }
/** * Decorate the given Cache, if necessary. * @param cache the raw Cache object, based on the configuration of this FactoryBean * @return the (potentially decorated) cache object to be registered with the CacheManager */ protected Ehcache decorateCache(Ehcache cache) { if (this.cacheEntryFactory != null) { if (this.cacheEntryFactory instanceof UpdatingCacheEntryFactory) { return new UpdatingSelfPopulatingCache(cache, (UpdatingCacheEntryFactory) this.cacheEntryFactory); } else { return new SelfPopulatingCache(cache, this.cacheEntryFactory); } } if (this.blocking) { return new BlockingCache(cache); } return cache; }
/** * Creates an instance over an underlying source specifying the cache manager. * * @param name the cache name, not empty * @param underlying the underlying source, not null * @param cacheManager the cache manager, not null */ public AbstractEHCachingMaster(final String name, final AbstractChangeProvidingMaster<D> underlying, final CacheManager cacheManager) { ArgumentChecker.notEmpty(name, "name"); ArgumentChecker.notNull(underlying, "underlying"); ArgumentChecker.notNull(cacheManager, "cacheManager"); _underlying = underlying; _cacheManager = cacheManager; // Load cache configuration if (cacheManager.getCache(name + CACHE_NAME_SUFFIX) == null) { // If cache config not found, set up programmatically s_logger.warn("Could not load a cache configuration for " + name + CACHE_NAME_SUFFIX + ", building a default configuration programmatically instead"); getCacheManager().addCache(new Cache(tweakCacheConfiguration(new CacheConfiguration(name + CACHE_NAME_SUFFIX, 10000)))); } _uidToDocumentCache = new SelfPopulatingCache(_cacheManager.getCache(name + CACHE_NAME_SUFFIX), new UidToDocumentCacheEntryFactory<>(_underlying)); getCacheManager().replaceCacheWithDecoratedCache(_cacheManager.getCache(name + CACHE_NAME_SUFFIX), getUidToDocumentCache()); // Listen to change events from underlying, clean this cache accordingly and relay events to our change listeners _changeManager = new BasicChangeManager(); _changeListener = new ChangeListener() { @Override public void entityChanged(ChangeEvent event) { final ObjectId oid = event.getObjectId(); final Instant versionFrom = event.getVersionFrom(); final Instant versionTo = event.getVersionTo(); cleanCaches(oid, versionFrom, versionTo); _changeManager.entityChanged(event.getType(), event.getObjectId(), event.getVersionFrom(), event.getVersionTo(), event.getVersionInstant()); } }; underlying.changeManager().addChangeListener(_changeListener); }
/** * Decorate the given Cache, if necessary. * <p>The default implementation simply returns the given cache object as-is. * * @param cache the raw Cache object, based on the configuration of this FactoryBean * @param model the model containing the name of the cache to retrieve * @return the (potentially decorated) cache object to be registered with the CacheManager */ protected Ehcache decorateCache(Cache cache, EhCacheCachingModel model) { if (model.getCacheEntryFactory() != null) { if (model.getCacheEntryFactory() instanceof UpdatingCacheEntryFactory) { return new UpdatingSelfPopulatingCache(cache, (UpdatingCacheEntryFactory) model.getCacheEntryFactory()); } else { return new SelfPopulatingCache(cache, model.getCacheEntryFactory()); } } if (model.isBlocking()) { return new BlockingCache(cache); } return cache; }
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 FrontCacheUpdater(SelfPopulatingCache back) { backCache = back; }
public MyDataAccessClass(Ehcache cache) { cache.registerCacheWriter(new MyCacheWriter()); this.cache = new SelfPopulatingCache(cache,new MyCacheEntryFactory()); }
/** * Create new cache. * * @param cache Cache where computed values are stored. * @param parser Parser to compute concrete {@link ReadableUserAgent} instances. */ public EhCacheCache(Ehcache cache, UserAgentStringParser parser) { super(parser); this.cache = new SelfPopulatingCache(cache, new EhCacheEntryFactory(parser)); }