/** * Tests expiry notification is hooked up to searchInMemoryStore * * @throws InterruptedException * @throws CacheException */ @Test public void testExpiryViaMemoryStoreCheckingOnGet() throws InterruptedException, CacheException, IOException { cache = manager.getCache(sampleCache2); cache.removeAll(); CountingCacheEventListener listener = getCountingCacheEventListener(cache); listener.resetCounters(); Serializable key = "1"; Serializable value = new Date(); Element element = new Element(key, value); // Check expiry from memory store in 1 second cache.put(element); RetryAssert.assertBy(5, TimeUnit.SECONDS, RetryAssert.elementAt(cache, key), nullValue()); if (cache.getSize() != 0) { assertThat(listener.getCacheElementsExpired(), hasSize(0)); cache.evictExpiredElements(); assertEquals(0, cache.getSize()); List<CacheEvent> notifications = listener.getCacheElementsExpired(); assertThat(notifications, hasSize(1)); assertEquals(element, notifications.get(0).getElement()); } else { List<CacheEvent> notifications = listener.getCacheElementsExpired(); assertThat(notifications, hasSize(1)); assertEquals(element, notifications.get(0).getElement()); } }
/** Drive everything to point of breakage within a 64MB VM. */ public void xTestHugePutsBreaksAsynchronous() throws CacheException, InterruptedException { // Give everything a chance to startup StopWatch stopWatch = new StopWatch(); Integer index = null; for (int i = 0; i < 500; 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); // assertTrue(putTime < 8); assertEquals(100000, cache1.getSize()); Thread.sleep(100000); assertEquals(20000, manager2.getCache("sampleCache1").getSize()); assertEquals(20000, manager3.getCache("sampleCache1").getSize()); assertEquals(20000, manager4.getCache("sampleCache1").getSize()); assertEquals(20000, manager5.getCache("sampleCache1").getSize()); }
@Override public void init() { List<Wine> all = mysql.findAll(); for (Wine wine : all) { cache.put(new Element(wine.getId(), wine)); } System.out.println("-------->>>>cache.getSize() : " + cache.getSize()); }
private boolean checkForCacheSize(long size, Ehcache... caches) { boolean sizeReached = true; for (Ehcache cache : caches) { if (cache.getSize() != size) { sizeReached = false; break; } } return sizeReached; }
private void dumpCacheInfo(final Ehcache cache) { out.println("cache-size: " + cache.getSize()); out.println("cache-status: " + cache.getStatus()); out.println("cache-guild: " + cache.getGuid()); out.println("cache-name: " + cache.getName()); out.println("mem-size: " + cache.calculateInMemorySize()); out.println("heap-size: " + cache.calculateOffHeapSize()); out.println("avg get-time: " + cache.getAverageGetTime()); out.println("mem store-size: " + cache.getMemoryStoreSize()); out.println("off-heap size: " + cache.getOffHeapStoreSize()); out.println("cache-statistics: " + cache.getStatistics()); }
/** @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"); }
/** * @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"); }
@Override public long size() { return Long.valueOf(impl.getSize()); }
/** * 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()); }