/** * Test. * * @throws IOException e */ @Test public void testCache() throws IOException { final String cacheName = "test 1"; final CacheManager cacheManager = CacheManager.getInstance(); cacheManager.addCache(cacheName); final String cacheName2 = "test 2"; try { final Cache cache = cacheManager.getCache(cacheName); cache.put(new Element(1, Math.random())); cache.get(1); cache.get(0); cacheManager.addCache(cacheName2); final Cache cache2 = cacheManager.getCache(cacheName2); cache2.getCacheConfiguration().setOverflowToDisk(false); cache2.getCacheConfiguration().setEternal(true); cache2.getCacheConfiguration().setMaxElementsInMemory(0); // JavaInformations doit être réinstancié pour récupérer les caches final List<JavaInformations> javaInformationsList2 = Collections.singletonList(new JavaInformations(null, true)); final HtmlReport htmlReport = new HtmlReport(collector, null, javaInformationsList2, Period.TOUT, writer); htmlReport.toHtml(null, null); assertNotEmptyAndClear(writer); setProperty(Parameter.SYSTEM_ACTIONS_ENABLED, "false"); htmlReport.toHtml(null, null); assertNotEmptyAndClear(writer); } finally { setProperty(Parameter.SYSTEM_ACTIONS_ENABLED, null); cacheManager.removeCache(cacheName); cacheManager.removeCache(cacheName2); } }
/** ** Private Methods ** */ private CacheEntry getCache(ProceedingJoinPoint pjp) { String name = getCacheName(pjp); CacheEntry entry = _entries.get(name); if (entry == null) { Method method = _cacheableMethodKeyFactoryManager.getMatchingMethodForJoinPoint(pjp); CacheableMethodKeyFactory keyFactory = getKeyFactory(pjp, method); boolean valueSerializable = isValueSerializable(pjp, method); Cache cache = _cacheManager.getCache(name); if (cache == null) { cache = createCache(pjp, name); if (cache == null) { _cacheManager.addCache(name); cache = _cacheManager.getCache(name); } else { _cacheManager.addCache(cache); } } entry = new CacheEntry(keyFactory, valueSerializable, cache); _entries.put(name, entry); } return entry; }
@PostConstruct public void initialize() { stats = new Stats(); boolean cacheExists = cacheManager.cacheExists(getName()); if (cacheExists) { if (failOnDuplicateCache) { throw new RuntimeException("A previous cache with name [" + getName() + "] exists."); } else { log.warn("skip duplicate cache " + getName()); ehcache = cacheManager.getCache(getName()); } } if (!cacheExists) { ehcache = new Cache( getName(), getMaxElementsInMemory(), getMemoryStoreEvictionPolicy(), isOverflowToDisk(), getDiskStorePath(), isEternal(), getTimeToLiveSeconds(), getTimeToIdleSeconds(), isDiskPersistent(), getDiskExpiryThreadIntervalSeconds(), null); cacheManager.addCache(ehcache); ehcache.setStatisticsEnabled(statisticsEnabled); } }
/** * Creating 11 Threads which attempt to get a non-null entry will result in 1 call to the * CacheEntryFactory * * @throws InterruptedException */ @Test public void testSelfPopulatingBlocksWithoutTimeoutSetNonNull() throws InterruptedException { selfPopulatingCache = new SelfPopulatingCache( new Cache("TestCache", 50, false, false, 0, 0), new NonNullCachePopulator()); // selfPopulatingCache.setTimeoutMillis(200); manager.addCache(selfPopulatingCache); CacheAccessorThread[] cacheAccessorThreads = new CacheAccessorThread[10]; for (int i = 0; i < cacheAccessorThreads.length; i++) { cacheAccessorThreads[i] = new CacheAccessorThread(selfPopulatingCache, "key1"); cacheAccessorThreads[i].start(); // Do a slight delay here so that all the timeouts // don't happen simultaneously - this is key try { Thread.sleep(20); } catch (InterruptedException ignored) { // } } // All of the others should have timed out. The first thread will have returned null. // This thread should be able to have a go, thus setting the count to 2 Thread.sleep(2000); Thread lateThread = new CacheAccessorThread(selfPopulatingCache, "key1"); lateThread.start(); lateThread.join(); assertEquals( "The wrong number of cacheAccessorThreads tried to create selfPopulatingCache entry for key1", 1, cacheEntryFactoryRequests); }
public Cache getEhCache() { Cache cache = ehcacheManager.getCache(name); if (null == cache) { ehcacheManager.addCache(name); return ehcacheManager.getCache(name); } else { return cache; } }
/** * 获得一个Cache,没有则创建一个。 * * @param cacheName * @return */ private static Cache getCache(String cacheName) { Cache cache = cacheManager.getCache(cacheName); if (cache == null) { cacheManager.addCache(cacheName); cache = cacheManager.getCache(cacheName); cache.getCacheConfiguration().setEternal(true); } return cache; }
@Test public void testReadThroughSimpleCase() { CacheManager manager = new CacheManager(); manager.removalAll(); CacheConfiguration config = new CacheConfiguration() .name("sampleCacheReadThru") .maxElementsInMemory(100) .timeToIdleSeconds(8) .timeToLiveSeconds(8) .overflowToDisk(false); manager.addCache(new Cache(config)); Ehcache cache = manager.getEhcache("sampleCacheReadThru"); ReadThroughCacheConfiguration readThroughConfig = new ReadThroughCacheConfiguration().modeGet(true).build(); ReadThroughCache decorator = new ReadThroughCache(cache, readThroughConfig); cache.registerCacheLoader(stringifyCacheLoader); // should not be in the cache Element got = cache.get(new Integer(1)); Assert.assertNull(got); // now load with decorator via get got = decorator.get(new Integer(1)); Assert.assertNotNull(got); // now should be in the base cache got = cache.get(new Integer(1)); Assert.assertNotNull(got); // now let it expire. sleepySeconds(10); // missing got = cache.get(new Integer(1)); Assert.assertNull(got); // now load with get with loader got = decorator.get(new Integer(1)); Assert.assertNotNull(got); // now should be in the base cache too. got = cache.get(new Integer(1)); Assert.assertNotNull(got); manager.removalAll(); manager.shutdown(); }
/** @throws IOException */ @Test public void customCachedVariableFragment() throws IOException { logging.setLogLevel("log4j.category.net.sf.ehcache", "DEBUG"); Fragments.setDefaultFragmentCacheType(CacheType.EHCACHE); FileFragment ff = new FileFragment(tf.newFolder("cachedVariableFragmentTest"), "testfrag.cdf"); File cacheLocation = createCacheDir(); Configuration cacheManagerConfig = new Configuration() .diskStore(new DiskStoreConfiguration().path(cacheLocation.getAbsolutePath())); CacheManager manager = CacheManager.newInstance(cacheManagerConfig); CacheConfiguration config = new CacheConfiguration(ff.getName() + "-variable-fragment-cache-custom", 100); config.persistence( new PersistenceConfiguration().strategy(PersistenceConfiguration.Strategy.LOCALTEMPSWAP)); config.setMaxElementsInMemory(10); config.setMaxElementsOnDisk(1000); config.setDiskSpoolBufferSizeMB(10); Ehcache cache = new Cache(config); manager.addCache(cache); log.info( "Storing cache on disk at {}", cacheManagerConfig.getDiskStoreConfiguration().getPath()); log.info("Using disk store size of {}", cache.getDiskStoreSize()); log.info("Overflowing to disk: {}", config.isOverflowToDisk()); ff.setCache(new VariableFragmentArrayCache(cache)); for (int j = 0; j < 100; j++) { VariableFragment vf1 = new VariableFragment(ff, "a" + j); vf1.setArray(new ArrayDouble.D2(10, 39)); VariableFragment vfIndex = new VariableFragment(ff, "index" + j); vfIndex.setArray(new ArrayInt.D1(20)); VariableFragment vf2 = new VariableFragment(ff, "b" + j, vfIndex); List<Array> l = new ArrayList<>(); Array indexArray = vfIndex.getArray(); int offset = 0; for (int i = 0; i < 20; i++) { l.add(new ArrayDouble.D1(10)); indexArray.setInt(i, offset); offset += 10; } vf2.setIndexedArray(l); Assert.assertNotNull(vf1.getArray()); Assert.assertNotNull(vf2.getIndexedArray()); Assert.assertEquals(20, vf2.getIndexedArray().size()); Assert.assertNotNull(vfIndex.getArray()); log.info("In memory: {}; On disk: {}", cache.getSize(), cache.getDiskStoreSize()); } for (IVariableFragment var : ff) { Assert.assertNotNull(var.getArray()); log.info(var.getName() + ": " + var.getArray()); } logging.setLogLevel("log4j.category.net.sf.ehcache", "INFO"); }
public void afterPropertiesSet() throws Exception { if (cacheManager == null) cacheManager = CacheManager.create(); if (sessionCache == null) { sessionCache = cacheManager.getCache(cacheName); if (sessionCache == null) { sessionCache = new Cache(cacheName, 1000000, true, true, 0, maxInactiveInterval); cacheManager.addCache(sessionCache); } } sessionCache.getCacheEventNotificationService().registerListener(this); }
/** * Builds a Cache. * * <p>Even though this method provides properties, they are not used. Properties for EHCache are * specified in the ehcache.xml file. Configuration will be read from ehcache.xml for a cache * declaration where the name attribute matches the name parameter in this builder. * * @param name the name of the cache. Must match a cache configured in ehcache.xml * @param properties not used * @return a newly built cache will be built and initialised * @throws CacheException inter alia, if a cache of the same name already exists */ public void init() throws CacheException { try { manager = new CacheManager(); ehcache = manager.getCache(G4_RESOURCE_GROUP); if (ehcache == null) { logger.warn("Could not find configuration [" + G4_RESOURCE_GROUP + "]; using defaults."); manager.addCache(G4_RESOURCE_GROUP); ehcache = manager.getCache(G4_RESOURCE_GROUP); logger.debug("started EHCache region: " + G4_RESOURCE_GROUP); } } catch (net.sf.ehcache.CacheException e) { throw new CacheException(e); } }
private void initCache() { cacheManager = new CacheManager(); persistantCache = new Cache( new CacheConfiguration("persistantCache", 1024) .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU) .overflowToDisk(true) .eternal(false) .timeToLiveSeconds(0) .timeToIdleSeconds(0) .diskPersistent(true) .diskExpiryThreadIntervalSeconds(0)); // wtf is this? cacheManager.addCache(persistantCache); }
/** {@inheritDoc} */ @Override public void addCache(final String context, final CacheConfig cacheConfig) { if (!manager.cacheExists(context)) { final boolean overflowToDisk = cacheConfig.shouldSerializeElements(); // don't overflow final net.sf.ehcache.Cache cache = new net.sf.ehcache.Cache( context, cacheConfig.getMaxElementsInMemory(), overflowToDisk, false, // not eternal cacheConfig.getTimeToLiveSeconds(), cacheConfig.getTimeToIdleSeconds()); manager.addCache(cache); cacheConfigsPerContext.put(context, cacheConfig); } }
public static void main(String args[]) { Map<Object, Element> map = new HashMap<Object, Element>(); List<String> list = new ArrayList<String>(); // Create a cache manager CacheManager cacheManager = CacheManager.getInstance(); // Creates a cache called newCache cacheManager.addCache("newCache"); // Get cache called newCache Cache cache = cacheManager.getCache("newCache"); StatisticsGateway stats = cache.getStatistics(); // put into cache cache.put(new Element("1", "Monday")); list.add("1"); cache.put(new Element("2", "Tuesday")); list.add("2"); cache.put(new Element("3", "Wednesday")); list.add("3"); cache.put(new Element("4", "Thursday")); list.add("4"); // Displaying all elements System.out.println("All elements"); map = cache.getAll(list); Iterator it = map.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry) it.next(); System.out.println(pair.getKey() + " = " + pair.getValue()); } // Displaying elements and size of cache Element element = cache.get("1"); System.out.println("Value of key 1 :" + element.getObjectValue().toString()); System.out.println("Cache Size " + cache.getSize()); element = cache.get("2"); System.out.println("Value of key 2 :" + element.getObjectValue().toString()); System.out.println("Cache Size " + cache.getSize()); cache.removeElement(element); System.out.println("Cache Size after removing an element : " + cache.getSize()); cache.flush(); System.out.println("Removed Cache with key 3 :" + cache.remove("3")); System.out.println("Size after remove : " + cache.getSize()); }
/** Are all of the CachePeers for replicated caches bound to the listener and working? */ @Test public void testBoundListenerPeersAfterDefaultCacheAdd() throws RemoteException { String[] boundCachePeers = ((RMICacheManagerPeerListener) manager1.getCachePeerListener("RMI")) .listBoundRMICachePeers(); assertEquals(1, boundCachePeers.length); validateBoundCachePeer(boundCachePeers); // Add from default which is has a CacheReplicator configured. manager1.addCache("fromDefaultCache"); boundCachePeers = ((RMICacheManagerPeerListener) manager1.getCachePeerListener("RMI")) .listBoundRMICachePeers(); assertEquals(2, boundCachePeers.length); validateBoundCachePeer(boundCachePeers); }
private static synchronized Cache getCache() throws CacheException { if (cacheManager == null) { // 'new CacheManager' used instead of 'CacheManager.create' to avoid overriding // default cache String cacheConfigFile = CACHE_CFG_FILE; String cfgFile = PentahoSystem.getApplicationContext() .getSolutionPath(DashboardDesignerContentGenerator.PLUGIN_PATH + cacheConfigFile); cacheManager = new CacheManager(cfgFile); // CacheManager.create(cfgFile); } enableCacheProperShutdown(true); if (!cacheManager.cacheExists(CACHE_NAME)) { cacheManager.addCache(CACHE_NAME); } return cacheManager.getCache(CACHE_NAME); }
public void startup() throws InitializationException { if (initialized) { return; } cacheManager = CacheManager.create(); // Create a Cache for storing entity definitions entityDefinitionCache = new Cache( new CacheConfiguration(ENTITY_DEFINITION_CACHE, MAX_ENTITY_DEFINITION_CACHE_ELEMENTS) .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU) .eternal(false) .timeToLiveSeconds(0) .timeToIdleSeconds(0)); cacheManager.addCache(entityDefinitionCache); preloadCache(); Context.registerObserver(this, ObservationEventType.CUSTOM_FIELD_ADD_EVENT); Context.registerObserver(this, ObservationEventType.CUSTOM_FIELD_DELETE_EVENT); Context.registerObserver(this, ObservationEventType.CUSTOM_FIELD_UPDATE_EVENT); initialized = true; }
@Override public CachePool createCachePool(String poolName, int cacheSize, int expiredSeconds) { CacheManager cacheManager = CacheManager.create(); Cache enCache = cacheManager.getCache(poolName); if (enCache == null) { CacheConfiguration cacheConf = cacheManager.getConfiguration().getDefaultCacheConfiguration().clone(); cacheConf.setName(poolName); if (cacheConf.getMaxEntriesLocalHeap() != 0) { cacheConf.setMaxEntriesLocalHeap(cacheSize); } else { cacheConf.setMaxBytesLocalHeap(String.valueOf(cacheSize)); } cacheConf.setTimeToIdleSeconds(expiredSeconds); Cache cache = new Cache(cacheConf); cacheManager.addCache(cache); return new EnchachePool(poolName, cache, cacheSize); } else { return new EnchachePool(poolName, enCache, cacheSize); } }
/** * @throws java.io.IOException * @throws IOException */ @Test public void cachedVariableFragment() throws IOException { logging.setLogLevel("log4j.category.net.sf.ehcache", "DEBUG"); Fragments.setDefaultFragmentCacheType(CacheType.EHCACHE); FileFragment ff = new FileFragment(tf.newFolder("cachedVariableFragmentTest"), "testfrag.cdf"); Configuration cacheManagerConfig = new Configuration(); CacheManager manager = CacheManager.newInstance(cacheManagerConfig); CacheConfiguration config = new CacheConfiguration(ff.getName() + "-variable-fragment-cache", 100); Ehcache cache = new Cache(config); manager.addCache(cache); for (int j = 0; j < 100; j++) { VariableFragment vf1 = new VariableFragment(ff, "a" + j); vf1.setArray(new ArrayDouble.D2(10, 39)); VariableFragment vfIndex = new VariableFragment(ff, "index" + j); vfIndex.setArray(new ArrayInt.D1(20)); VariableFragment vf2 = new VariableFragment(ff, "b" + j, vfIndex); List<Array> l = new ArrayList<>(); Array indexArray = vfIndex.getArray(); int offset = 0; for (int i = 0; i < 20; i++) { l.add(new ArrayDouble.D1(10)); indexArray.setInt(i, offset); offset += 10; } vf2.setIndexedArray(l); Assert.assertNotNull(vf1.getArray()); Assert.assertNotNull(vf2.getIndexedArray()); Assert.assertEquals(20, vf2.getIndexedArray().size()); Assert.assertNotNull(vfIndex.getArray()); log.info("In memory: {}; On disk: {}", cache.getSize(), cache.getDiskStoreSize()); } for (IVariableFragment var : ff) { Assert.assertNotNull(var.getArray()); log.info(var.getName() + ": " + var.getArray()); } logging.setLogLevel("log4j.category.net.sf.ehcache", "INFO"); }
/** Are all of the CachePeers for replicated caches bound to the listener and working? */ @Test public void testBoundListenerPeersAfterProgrammaticCacheAdd() throws RemoteException { String[] boundCachePeers = ((RMICacheManagerPeerListener) manager1.getCachePeerListener("RMI")) .listBoundRMICachePeers(); assertEquals(1, boundCachePeers.length); validateBoundCachePeer(boundCachePeers); // Add from default which is has a CacheReplicator configured. RMICacheReplicatorFactory factory = new RMICacheReplicatorFactory(); CacheEventListener replicatingListener = factory.createCacheEventListener(null); Cache cache = new Cache(new CacheConfiguration().name("programmaticallyAdded").maxEntriesLocalHeap(0)); cache.getCacheEventNotificationService().registerListener(replicatingListener); manager1.addCache(cache); boundCachePeers = ((RMICacheManagerPeerListener) manager1.getCachePeerListener("RMI")) .listBoundRMICachePeers(); assertEquals(2, boundCachePeers.length); validateBoundCachePeer(boundCachePeers); }
public Cache build(String name) throws CacheException { EhCache ehcache = cacheManager.get(name); if (null == ehcache) { try { synchronized (manager) { net.sf.ehcache.Cache cache = manager.getCache(name); if (cache == null) { log.warn("Could not find configuration [" + name + "]; using defaults."); manager.addCache(name); cache = manager.getCache(name); log.debug("started EHCache region: " + name); } synchronized (cacheManager) { ehcache = new EhCache(cache); cacheManager.put(name, ehcache); return ehcache; } } } catch (net.sf.ehcache.CacheException e) { throw new CacheException(e); } } return ehcache; }
@SuppressWarnings("serial") public void init(NutConfig nc) { NutShiro.DefaultLoginURL = "/admin/logout"; // 检查环境 if (!Charset.defaultCharset().name().equalsIgnoreCase(Encoding.UTF8)) { log.warn("This project must run in UTF-8, pls add -Dfile.encoding=UTF-8 to JAVA_OPTS"); } // 获取Ioc容器及Dao对象 Ioc ioc = nc.getIoc(); // 加载freemarker自定义标签 自定义宏路径 ioc.get(Configuration.class) .setAutoImports( new HashMap<String, String>(2) { { put("p", "/ftl/pony/index.ftl"); put("s", "/ftl/spring.ftl"); } }); ioc.get(FreeMarkerConfigurer.class, "mapTags"); Dao dao = ioc.get(Dao.class); // 为全部标注了@Table的bean建表 Daos.createTablesInPackage(dao, getClass().getPackage().getName() + ".bean", false); // 获取配置对象 PropertiesProxy conf = ioc.get(PropertiesProxy.class, "conf"); // 初始化SysLog,触发全局系统日志初始化 ioc.get(SysLogService.class); // 初始化默认根用户 User admin = dao.fetch(User.class, "admin"); if (admin == null) { UserService us = ioc.get(UserService.class); admin = us.add("admin", "123456"); } // 初始化游客用户 User guest = dao.fetch(User.class, "guest"); if (guest == null) { UserService us = ioc.get(UserService.class); guest = us.add("guest", "123456"); UserProfile profile = dao.fetch(UserProfile.class, guest.getId()); profile.setNickname("游客"); dao.update(profile, "nickname"); } // 获取NutQuartzCronJobFactory从而触发计划任务的初始化与启动 ioc.get(NutQuartzCronJobFactory.class); // 权限系统初始化 AuthorityService as = ioc.get(AuthorityService.class); as.initFormPackage("net.wendal.nutzbook"); as.checkBasicRoles(admin); // 检查一下Ehcache CacheManager 是否正常. CacheManager cacheManager = ioc.get(CacheManager.class); log.debug("Ehcache CacheManager = " + cacheManager); // CachedNutDaoExecutor.DEBUG = true; // 启用FastClass执行入口方法 Mvcs.disableFastClassInvoker = false; // 设置Markdown缓存 if (cacheManager.getCache("markdown") == null) cacheManager.addCache("markdown"); Markdowns.cache = cacheManager.getCache("markdown"); if (conf.getBoolean("cdn.enable", false) && !Strings.isBlank(conf.get("cdn.urlbase"))) { MarkdownFunction.cdnbase = conf.get("cdn.urlbase"); } }
/** * @param cacheName the name of the cache * @param configuration [OPTIONAL] a config to use when building the cache, if null then use * default methods to create cache * @return an Ehcache */ private Ehcache makeEhcache( String cacheName, org.sakaiproject.memory.api.Configuration configuration) { /** Indicates a cache is a new one and should be configured */ boolean newCache = false; String name = cacheName; if (name == null || "".equals(name)) { name = "DefaultCache" + UUID.randomUUID().toString(); log.warn("Creating cache without a name, generating dynamic name: (" + name + ")"); newCache = true; } Ehcache cache; // fetch an existing cache first if possible if (!newCache && cacheManager.cacheExists(name)) { cache = cacheManager.getEhcache(name); if (log.isDebugEnabled()) log.debug("Retrieved existing ehcache (" + name + ")"); } else { // create a new defaulted cache cacheManager.addCache(name); cache = cacheManager.getEhcache(name); newCache = true; log.info("Created ehcache (" + name + ") using defaults"); } if (newCache) { if (log.isDebugEnabled()) log.debug("Prepared to configure new ehcache (" + name + "): " + cache); // warn people if they are using an old config style if (serverConfigurationService.getString(name) == null) { log.warn( "Old cache configuration for cache (" + name + "), must be changed to memory." + name + " or it will be ignored"); } // load the ehcache config from the Sakai config service String config = serverConfigurationService.getString("memory." + name); if (StringUtils.isNotBlank(config)) { log.info("Configuring ehcache (" + name + ") from Sakai config: " + config); try { // ehcache specific code here - no exceptions thrown //noinspection deprecation new CacheInitializer().configure(config).initialize(cache.getCacheConfiguration()); } catch (Exception e) { // nothing to do here but proceed log.error("Failure configuring cache (" + name + "): " + config + " :: " + e, e); } } /* KNL-532 - Upgraded Ehcache 2.5.1 (2.1.0+) defaults to no stats collection. * We may choose to allow configuration per-cache for performance tuning. * For now, we default everything to on, while this property allows a system-wide override. */ boolean enabled = true; if (serverConfigurationService != null) { enabled = !serverConfigurationService.getBoolean("memory.cache.statistics.force.disabled", false); } if (cache.isStatisticsEnabled() != enabled) { cache.setStatisticsEnabled(enabled); } } // apply config to the cache (every time) if (configuration != null) { if (configuration.getMaxEntries() >= 0) { cache.getCacheConfiguration().setMaxEntriesLocalHeap(configuration.getMaxEntries()); } if (configuration.isEternal()) { cache.getCacheConfiguration().setTimeToLiveSeconds(0l); cache.getCacheConfiguration().setTimeToIdleSeconds(0l); } else { if (configuration.getTimeToLiveSeconds() >= 0) { cache.getCacheConfiguration().setTimeToLiveSeconds(configuration.getTimeToLiveSeconds()); } if (configuration.getTimeToIdleSeconds() >= 0) { cache.getCacheConfiguration().setTimeToIdleSeconds(configuration.getTimeToIdleSeconds()); } } cache.getCacheConfiguration().setEternal(configuration.isEternal()); cache.setStatisticsEnabled(configuration.isStatisticsEnabled()); log.info("Configured ehcache (" + name + ") from inputs: " + configuration); } if (log.isDebugEnabled()) log.debug("Returning initialized ehcache (" + name + "): " + cache); return cache; }
/** * @param cacheName * @param legacyMode If true always create a new Cache. If false, cache must be defined in bean * factory. * @return */ private Ehcache instantiateCache(String cacheName) { if (M_log.isDebugEnabled()) M_log.debug("createNewCache(String " + cacheName + ")"); String name = cacheName; if (name == null || "".equals(name)) { name = "DefaultCache" + UUID.randomUUID().toString(); } // Cache creation should all go to the cache manager and be // configured via the cache manager setup. if (cacheManager.cacheExists(name)) { return cacheManager.getEhcache(name); } cacheManager.addCache(name); return cacheManager.getEhcache(name); /* if(legacyMode) { if (cacheManager.cacheExists(name)) { M_log.warn("Cache already exists and is bound to CacheManager; creating new cache from defaults: " + name); // favor creation of new caches for backwards compatibility // in the future, it seems like you would want to return the same // cache if it already exists name = name + UUID.randomUUID().toString(); } } Ehcache cache = null; // try to locate a named cache in the bean factory try { cache = (Ehcache) ComponentManager.get(name); } catch (Throwable e) { cache = null; M_log.error("Error occurred when trying to load cache from bean factory!", e); } if(cache != null) // found the cache { M_log.info("Loaded Named Cache " + cache); return cache; } else // did not find the cache { if(legacyMode) { cacheManager.addCache(name); // create a new cache cache = cacheManager.getEhcache(name); M_log.info("Loaded Default Cache " + cache); } else { M_log.error("Could not find named cache in the bean factory!:" + name); } return cache; } */ }
public void testServerMapLocalCacheUseCaseEviction() throws Exception { final TCObjectSelfFactory factory = new TCObjectSelfFactory(); final CacheManager cacheManager = CacheManager.create(new Configuration().name("test-cm")); Cache ehcache = new Cache(new CacheConfiguration().name("test-cache").maxEntriesLocalHeap(2000)); cacheManager.addCache(ehcache); TCObjectSelfStore tcoSelfStore = new TCObjectSelfStore(ehcache); final ServerMapLocalCache serverMapLocalCache = new ServerMapLocalCache(tcoSelfStore, ehcache); final int numMutators = 5; final int numReaders = 100; final AtomicInteger readTpsCounter = new AtomicInteger(); final AtomicInteger nonNullReadsCounter = new AtomicInteger(); final AtomicInteger writeTpsCounter = new AtomicInteger(); final TestStatus testStatus = new TestStatus(); Thread[] mutators = new Thread[numMutators]; for (int i = 0; i < numMutators; i++) { mutators[i] = new Thread( new Mutator(testStatus, factory, serverMapLocalCache, writeTpsCounter), "Mutator thread - " + i); mutators[i].setDaemon(true); mutators[i].start(); } Thread[] readers = new Thread[numReaders]; for (int i = 0; i < numReaders; i++) { readers[i] = new Thread( new Reader( testStatus, factory, serverMapLocalCache, readTpsCounter, nonNullReadsCounter), "Reader thread - " + i); readers[i].setDaemon(true); readers[i].start(); } Thread reporter = new Thread( new Runnable() { long zeroTpsStartTime = Long.MAX_VALUE; public void run() { try { final int sleepSecs = 2; while (!testStatus.isStopTestRequested()) { long timeInSecsSinceZeroTPS = getTimeInSecsSinceZeroTPS(); if (timeInSecsSinceZeroTPS > 60) { throw new RuntimeException("TPS went to zero for more than 1 min"); } int reads = readTpsCounter.getAndSet(0); int nonNullReads = nonNullReadsCounter.getAndSet(0); int writes = writeTpsCounter.getAndSet(0); int readTps = reads / sleepSecs; int writeTps = writes / sleepSecs; int nonNullReadsTps = nonNullReads / sleepSecs; System.out.println("============ Iteration Stats ================"); System.out.println("Reads: " + reads + ", Read TPS: " + readTps); System.out.println( "Non-null Reads: " + nonNullReads + ", Non-null Reads TPS: " + nonNullReadsTps); System.out.println("Writes: " + writes + ", Write TPS: " + writeTps); System.out.println("Time since zero TPS: " + timeInSecsSinceZeroTPS + " secs"); if (readTps == 0 && nonNullReadsTps == 0 && writeTps == 0) { if (zeroTpsStartTime == Long.MAX_VALUE) { zeroTpsStartTime = System.nanoTime(); } } else { zeroTpsStartTime = Long.MAX_VALUE; } try { Thread.sleep(sleepSecs * 1000); } catch (InterruptedException e) { // ignored } } } catch (Throwable t) { testStatus.requestStopWithError(t); } } private long getTimeInSecsSinceZeroTPS() { if (zeroTpsStartTime != Long.MAX_VALUE) { long currentTime = System.nanoTime(); long diff = currentTime - zeroTpsStartTime; Assert.assertTrue("Diff should be > 0", diff > 0); return TimeUnit.NANOSECONDS.toSeconds(diff); } else { return -1; } } }, "Reporter thread"); reporter.setDaemon(true); reporter.start(); // stop after 5 mins testStatus.stopAfterSeconds(60 * 5); if (testStatus.getError() != null) { testStatus.getError().printStackTrace(); Assert.fail("Test failed with exception"); } System.out.println("Test completed"); cacheManager.shutdown(); }
/** * @param cacheName * @param legacyMode If true always create a new Cache. If false, cache must be defined in bean * factory. * @return */ private Ehcache instantiateCache(String cacheName) { if (M_log.isDebugEnabled()) M_log.debug("createNewCache(String " + cacheName + ")"); String name = cacheName; if (name == null || "".equals(name)) { name = "DefaultCache" + UUID.randomUUID().toString(); } // Cache creation should all go to the cache manager and be // configured via the cache manager setup. if (cacheManager.cacheExists(name)) { return cacheManager.getEhcache(name); } Ehcache cache = null; try { Ehcache defaultCache = getDefaultCache(); if (defaultCache != null) { cache = (Ehcache) defaultCache.clone(); cache.setName(cacheName); // Not look for any custom configuration. // Check for old configuration properties. if (serverConfigurationService().getString(name) == null) { M_log.warn("Old cache configuration " + name + " must be changed to memory." + name); } String config = serverConfigurationService().getString("memory." + name); if (config != null && config.length() > 0) { M_log.debug("Found configuration for cache: " + name + " of: " + config); new CacheInitializer().configure(config).initialize(cache.getCacheConfiguration()); } cacheManager.addCache(cache); } } catch (Exception ex) { M_log.warn("Unable to access or close default cache", ex); } if (cache == null) { cacheManager.addCache(name); cache = cacheManager.getEhcache(name); } // KNL-1292: do not set if the cache is not yet init'ed if (cache != null && cache.getStatus().equals(Status.STATUS_ALIVE)) { // KNL-532 - Upgraded Ehcache 2.5.1 (2.1.0+) defaults to no stats collection. // We may choose to allow configuration per-cache for performance tuning. // For now, we default everything to on, while this property allows a system-wide override. cache.setStatisticsEnabled( !(serverConfigurationService() .getBoolean("memory.cache.statistics.force.disabled", false))); } return cache; /* if(legacyMode) { if (cacheManager.cacheExists(name)) { M_log.warn("Cache already exists and is bound to CacheManager; creating new cache from defaults: " + name); // favor creation of new caches for backwards compatibility // in the future, it seems like you would want to return the same // cache if it already exists name = name + UUID.randomUUID().toString(); } } Ehcache cache = null; // try to locate a named cache in the bean factory try { cache = (Ehcache) ComponentManager.get(name); } catch (Exception e) { cache = null; M_log.error("Error occurred when trying to load cache from bean factory!", e); } if(cache != null) // found the cache { M_log.info("Loaded Named Cache " + cache); return cache; } else // did not find the cache { if(legacyMode) { cacheManager.addCache(name); // create a new cache cache = cacheManager.getEhcache(name); M_log.info("Loaded Default Cache " + cache); } else { M_log.error("Could not find named cache in the bean factory!:" + name); } return cache; } */ }
@Before public void setUp() { cache = new Cache(UUID.randomUUID().toString(), 20000, false, false, 5, 2); cacheManager.addCache(cache); instance = new EHCacheDatastore(cache); }
/** * Performance and capacity tests. * * <p> */ @Test public void testBootstrap() throws CacheException, InterruptedException, RemoteException { // load up some data StopWatch stopWatch = new StopWatch(); Integer index = null; for (int i = 0; i < 2; i++) { for (int j = 0; j < 1000; j++) { index = Integer.valueOf(((1000 * i) + j)); cache1.put( new Element( index, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")); } } long elapsed = stopWatch.getElapsedTime(); long putTime = ((elapsed / 1000)); LOG.info("Put Elapsed time: " + putTime); assertEquals(2000, cache1.getSize()); Thread.sleep(7000); assertEquals(2000, manager2.getCache("sampleCache1").getSize()); assertEquals(2000, manager3.getCache("sampleCache1").getSize()); assertEquals(2000, manager4.getCache("sampleCache1").getSize()); assertEquals(2000, manager5.getCache("sampleCache1").getSize()); // now test bootstrap manager1.addCache("bootStrapResults"); Cache cache = manager1.getCache("bootStrapResults"); List cachePeers = manager1.getCacheManagerPeerProvider("RMI").listRemoteCachePeers(cache1); CachePeer cachePeer = (CachePeer) cachePeers.get(0); List keys = cachePeer.getKeys(); assertEquals(2000, keys.size()); Element firstElement = cachePeer.getQuiet((Serializable) keys.get(0)); long size = firstElement.getSerializedSize(); assertEquals(504, size); int chunkSize = (int) (5000000 / size); List requestChunk = new ArrayList(); for (int i = 0; i < keys.size(); i++) { Serializable serializable = (Serializable) keys.get(i); requestChunk.add(serializable); if (requestChunk.size() == chunkSize) { fetchAndPutElements(cache, requestChunk, cachePeer); requestChunk.clear(); } } // get leftovers fetchAndPutElements(cache, requestChunk, cachePeer); assertEquals(keys.size(), cache.getSize()); }