/** * Ehcache 1.5 allows the diskStore element to be optional. Check that is is null Add different * cache constructors to make sure none inadvertently create a disk store */ @Test public void testCacheManagerWithNoDiskCachesFromConfiguration() throws CacheException, InterruptedException { LOG.info(System.getProperty("java.io.tmpdir")); singletonManager = CacheManager.create(AbstractCacheTest.TEST_CONFIG_DIR + "ehcache-nodisk.xml"); singletonManager.addCache("jsecurity-activeSessionCache"); Cache cacheA = singletonManager.getCache("jsecurity-activeSessionCache"); Cache cacheB = new Cache("1", 10, false, false, 2, 2); singletonManager.addCache(cacheB); Cache cacheC = new Cache("2", 10, false, false, 2, 2, false, 100); singletonManager.addCache(cacheC); for (int i = 0; i < 100; i++) { cacheA.put(new Element(i + "", "dog")); cacheB.put(new Element(i + "", "dog")); cacheC.put(new Element(i + "", "dog")); } Cache diskCache = new Cache("disk", 10, true, false, 2, 2); try { singletonManager.addCache(diskCache); throw new AssertionError( "Expected that adding a disk cache to a cache manager with no configured disk store path would throw CacheException"); } catch (CacheException e) { LOG.info("Caught expected exception", e); } singletonManager.shutdown(); assertEquals(null, singletonManager.getDiskStorePath()); }
@Test public void test() { Cache<String, Integer> cache = new Cache<String, Integer>(5); cache.put("a", 1); cache.put("b", 2); cache.put("c", 3); cache.put("d", 4); cache.put("e", 5); Assert.assertTrue(cache.containsKey("a")); Assert.assertTrue(cache.containsKey("b")); Assert.assertTrue(cache.containsKey("c")); Assert.assertTrue(cache.containsKey("d")); Assert.assertTrue(cache.containsKey("e")); // Access the "a" element in the cache cache.get("a"); // Now add a new element that exceeds the capacity of the cache cache.put("f", 6); Assert.assertTrue(cache.containsKey("a")); }
@Test public void testCache2() { Cache<String, String> cache = new LRUCache<String, String>(3); cache.put("1", "1"); cache.put("2", "2"); assertFalse(cache.isFull()); cache.put("3", "3"); assertTrue(cache.isFull()); assertNotNull(cache.get("3")); assertNotNull(cache.get("3")); assertNotNull( cache.get( "3")); // boost usage of a 3, but this doesn't change a thing, since this is a LRU cache // and not a LFU cache assertNotNull(cache.get("1")); assertNotNull(cache.get("2")); cache.put("4", "4"); assertNull(cache.get("3")); assertNotNull(cache.get("1")); assertNotNull(cache.get("2")); assertNotNull(cache.get("4")); cache.put("3", "3"); assertNull(cache.get("1")); }
public void testCacheSimpleReturn(String type) { int size = 100; // this test does not fill the cache Cache c1 = CacheManager.createCache(type, "c1", size); String k1 = "one"; String k2 = k1; String k3 = k2; Integer v1 = new Integer(-1); Integer v2 = v1; Integer v3 = v2; c1.put(k1, v1); for (int i = 0; i < size; i++) { k1 = k2; v1 = v2; Object o = c1.get(k1); assertTrue("expected a hit", o != null); assertEquals("should be the expected object", o, v1); k2 = k3; v2 = v3; o = c1.get(k2); assertTrue("expected a hit", o != null); assertEquals("should be the expected object", o, v2); k3 = "T" + i; v3 = new Integer(i); c1.put(k3, v3); } }
@Test public void testCacheTime() { Cache<String, String> cache = new LFUCache<>(3); cache.put("1", "1", 50); assertNotNull(cache.get("1")); assertNotNull(cache.get("1")); // boost usage cache.put("2", "2"); cache.get("2"); assertFalse(cache.isFull()); cache.put("3", "3"); assertTrue(cache.isFull()); ThreadUtil.sleep(100); assertNull(cache.get("1")); // expired assertFalse(cache.isFull()); cache.put("1", "1", 50); assertNotNull(cache.get("1")); assertNotNull(cache.get("1")); ThreadUtil.sleep(100); assertTrue(cache.isFull()); cache.put("4", "4"); assertNotNull(cache.get("3")); assertNotNull(cache.get("2")); assertNotNull(cache.get("4")); assertNull(cache.get("1")); }
private void assertCacheHonorsPutsSemantics(Cache<EntityWithSizeObject> cache) { Entity version1 = new Entity(10); assertTrue(version1 == cache.put(version1)); // WHEN Entity version2 = new Entity(10); // THEN assertTrue(version1 == cache.put(version2)); }
public void put(CacheObject r) { if (r.isStream()) { fifo.put(r); } else if (recentlyUsed.get(r.getPos()) != null) { lru.put(r); } else { fifo.put(r); lastUsed = r.getPos(); } }
/* * Cache the given hostname and address. */ private static void cacheAddress(String hostname, Object address, boolean success) { hostname = hostname.toLowerCase(); synchronized (addressCache) { cacheInitIfNeeded(); if (success) { addressCache.put(hostname, address); } else { negativeCache.put(hostname, address); } } }
@Test public void testEndless() { Cache<String, String> cache = new LRUCache<String, String>(0); assertFalse(cache.isFull()); cache.put("1", "1"); assertEquals(1, cache.size()); assertFalse(cache.isFull()); cache.put("2", "2"); assertEquals(2, cache.size()); assertFalse(cache.isFull()); }
@Test public void testPrune() { Cache<String, String> cache = new LRUCache<String, String>(3); cache.put("1", "1"); cache.put("2", "2"); cache.put("3", "3"); assertEquals(0, cache.prune()); assertEquals(3, cache.size()); cache.put("4", "4"); assertEquals(0, cache.prune()); assertEquals(3, cache.size()); }
/** Main processing per sentence. */ public void mapTokens(final LanguageModel lang, final Cache cache, final Graph graph) throws Exception { token_list = lang.tokenizeSentence(text); // determine an MD5 signature for this sentence cache.md_sent.reset(); for (int i = 0; i < token_list.length; i++) { if (LOG.isDebugEnabled()) { LOG.debug("token: " + token_list[i]); } cache.md_sent.update(token_list[i].getBytes()); } md5_hash = hexFormat(cache.md_sent.digest()); // use MD5 hash to lookup sentence in the cache final Sentence cache_hit = cache.get(md5_hash); if (cache_hit == null) { // add another scanned sentence to the cache cache.put(md5_hash, this); // scan each token to determine part-of-speech final String[] tag_list = lang.tagTokens(token_list); // create nodes for the graph Node last_node = null; node_list = new Node[token_list.length]; for (int i = 0; i < token_list.length; i++) { final String pos = tag_list[i]; if (LOG.isDebugEnabled()) { LOG.debug("token: " + token_list[i] + " pos tag: " + pos); } if (lang.isRelevant(pos)) { final String key = lang.getNodeKey(token_list[i], pos); final KeyWord value = new KeyWord(token_list[i], pos); final Node n = Node.buildNode(graph, key, value); // emit nodes to construct the graph if (last_node != null) { n.connect(last_node); } last_node = n; node_list[i] = n; } } } }
/** * This test is designed to be used with a profiler to explore the ways in which DiskStore uses * memory. It does not do much on its own. */ @Test public void testOutOfMemoryErrorOnOverflowToDisk() throws Exception { // Set size so the second element overflows to disk. Cache cache = new Cache( "test", 1000, MemoryStoreEvictionPolicy.LRU, true, null, false, 500, 500, false, 1, null); manager.addCache(cache); int i = 0; Random random = new Random(); for (; i < 5500; i++) { byte[] bytes = new byte[10000]; random.nextBytes(bytes); cache.put(new Element("" + i, bytes)); } LOG.info("Elements written: " + i); // Thread.sleep(100000); }
/** Internal method to read an existing pkg from the database */ synchronized AreaOfInterest read(String id, Connection conn) throws Exception { // check cache AreaOfInterest pkg = (AreaOfInterest) cache.get(id); if (pkg != null) { return pkg; } // pull from database and populate the object PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM AreaOfInterest WHERE guid LIKE ?"); pstmt.setString(1, id); ResultSet rs = pstmt.executeQuery(); pkg = new AreaOfInterest(id); if (rs.next()) { pkg.setObjectAlreadyInDB(true); pkg.setDirty(false); pkg.setDescription(rs.getString(2)); } else { throw new DataException("bad AreaOfInterest read"); } pkg.setDirty(false); pkg.setObjectAlreadyInDB(true); // put in the cache cache.put(pkg.getId(), pkg); // return the object return pkg; } // read
/** * The business logic of this selector for use as ResourceSelector of FileSelector. * * @param basedir as described in BaseExtendSelector * @param filename as described in BaseExtendSelector * @param cacheKey the name for the key for storing the hashvalue * @return */ private boolean isSelected(File basedir, String filename, String cacheKey) { validate(); File f = new File(basedir, filename); // You can not compute a value for a directory if (f.isDirectory()) { return selectDirectories; } // Get the values and do the comparison String cachedValue = String.valueOf(cache.get(f.getAbsolutePath())); String newValue = algorithm.getValue(f); boolean rv = (comparator.compare(cachedValue, newValue) != 0); // Maybe update the cache if (update && rv) { cache.put(f.getAbsolutePath(), newValue); setModified(getModified() + 1); if (!getDelayUpdate()) { saveCache(); } } return rv; }
private void cache(String dn, Set<String> indexes, Cache cache) { rwlock.writeLock().lock(); String cacheName = cacheToName.get(cache); try { for (String s : indexes) { Set<String> setDNs = (Set<String>) cache.get(s); if (setDNs == null) { setDNs = new HashSet<String>(); cache.put(s, setDNs); setDNs.add(dn); } else { if (!CacheTaboo.isTaboo(cacheName, s)) { if (setDNs.size() >= CACHE_BUCKET_LIMIT) { CacheTaboo.taboo(cacheName, s); cache.remove(s); } else { setDNs.add(dn); } } } } } finally { rwlock.writeLock().unlock(); } }
public void putObect(String skey, Object value) { if (skey == null) return; if (cache.contain(skey)) return; cache.put(skey, new CacheableWrapper(skey, value)); Debug.logVerbose( "[JdonFramework]<-cache->save cache: " + skey + ", cache size:" + cache.size(), module); }
public void testFillTheCache(String type) { final int size = 100; Cache c1 = CacheManager.createCache(type, "c1", size); String[] k = new String[size]; String[] v = new String[size]; for (int i = 0; i < size; i++) { k[i] = "K" + i; v[i] = "V" + i; c1.put(k[i], v[i]); } int count = 0; for (int i = 0; i < size; i++) { if (c1.get(k[i]) != null) { count++; } } assertTrue("too low a hit rate: " + type + " = " + count, count > size / 2); assertEquals("count puts", size, c1.getPuts()); assertEquals("count gets", size, c1.getGets()); assertEquals("count hits", count, c1.getHits()); }
/** * 保存到缓存中 * * @param cacheKey * @param value */ public void putObect(CacheKey ckey, Object value) { if (ckey == null) return; if (cache.contain(ckey.getKey())) return; cache.put(ckey.getKey(), new CacheableWrapper(ckey.getDataKey(), value)); Debug.logVerbose( "[JdonFramework]<-cache->save cache: " + ckey.getKey() + ", cache size:" + cache.size(), module); }
private static void compressLater(ByteBuffer[] data, int page) { CompressItem c = new CompressItem(); c.data = data; c.page = page; synchronized (LZF) { COMPRESS_LATER.put(c, c); } }
@Test public void testOnRemove() { final MutableInteger mutableInteger = new MutableInteger(); Cache<String, String> cache = new LFUCache<String, String>(2) { @Override protected void onRemove(String key, String cachedObject) { mutableInteger.value++; } }; cache.put("1", "val1"); cache.put("2", "val2"); assertEquals(0, mutableInteger.value); cache.put("3", "val3"); assertEquals(2, mutableInteger.value); }
@Test public void testLFUEvictionFromDiskStore() throws IOException, InterruptedException { Cache cache = new Cache( "testNonPersistent", 1, MemoryStoreEvictionPolicy.LFU, true, null, false, 2000, 1000, false, 1, null, null, 10); manager.addCache(cache); Store store = cache.getStore(); Element element; assertEquals(0, store.getSize()); for (int i = 0; i < 11; i++) { element = new Element("key" + i, "value" + i); cache.put(element); if (i > 0) { cache.get("key" + i); cache.get("key" + i); cache.get("key" + i); cache.get("key" + i); } } // allow to move through spool Thread.sleep(220); assertEquals(10, store.getOnDiskSize()); element = new Element("keyNew", "valueNew"); store.put(element); // allow to get out of spool Thread.sleep(220); assertEquals(10, store.getOnDiskSize()); // check new element not evicted assertNotNull(store.get(element.getObjectKey())); // check evicted honours LFU policy assertNull(store.get("key0")); for (int i = 0; i < 2000; i++) { store.put(new Element("" + i, new Date())); } // wait for spool to empty waitLonger(); assertEquals(10, store.getOnDiskSize()); }
@Override public void put(@Nonnull Key key, @Nonnull Entry entry) { if (cache != null) { synchronized (this) { Billing.debug(TAG, "Adding entry with key=" + key + " to the cache"); cache.put(key, entry); } } }
@Test public void testCache() { Cache<String, String> cache = new LRUCache<String, String>(3); cache.put("1", "1"); cache.put("2", "2"); assertFalse(cache.isFull()); cache.put("3", "3"); assertTrue(cache.isFull()); assertNotNull(cache.get("1")); assertNotNull(cache.get("2")); cache.put("4", "4"); assertNull(cache.get("3")); assertNotNull(cache.get("1")); assertNotNull(cache.get("2")); cache.put("3", "3"); assertNull(cache.get("4")); }
@Test public void testCache() { Cache<String, String> cache = new LFUCache<>(3); cache.put("1", "1"); cache.put("2", "2"); assertFalse(cache.isFull()); cache.put("3", "3"); assertTrue(cache.isFull()); assertNotNull(cache.get("1")); assertNotNull(cache.get("2")); cache.put("4", "4"); // new element, cache is full, prune is invoked assertNull(cache.get("3")); assertNotNull(cache.get("1")); assertNotNull(cache.get("2")); cache.put("3", "3"); assertNull(cache.get("4")); assertNotNull(cache.get("3")); }
/** * Answer an enhanced node that wraps the given node and conforms to the given interface type. * * @param n A node (assumed to be in this graph) * @param interf A type denoting the enhanced facet desired * @return An enhanced node */ public EnhNode getNodeAs(Node n, Class interf) { // We use a cache to avoid reconstructing the same Node too many times. EnhNode eh = (EnhNode) enhNodes.get(n); if (eh != null) return eh.viewAs(interf); // not in the cache, so build a new one eh = (EnhNode) ((GraphPersonality) personality).nodePersonality().newInstance(interf, n, this); enhNodes.put(n, eh); return eh; }
@Test public void testCache2() { Cache<String, String> cache = new LFUCache<>(3); cache.put("1", "1"); cache.put("2", "2"); assertFalse(cache.isFull()); cache.put("3", "3"); assertTrue(cache.isFull()); assertNotNull(cache.get("3")); assertNotNull(cache.get("3")); assertNotNull(cache.get("3")); // boost usage of a 3 assertNotNull(cache.get("1")); assertNotNull(cache.get("2")); cache.put("4", "4"); // since this is LFU cache, 1 AND 2 will be removed, but not 3 assertNotNull(cache.get("3")); assertNotNull(cache.get("4")); assertEquals(2, cache.size()); }
/** * Populate cache with {@code 'facts'}, which in our case are {@link FactPurchase} objects. * * @param factCache Cache to populate. * @throws IgniteException If failed. */ private static void populateFacts(Cache<Integer, FactPurchase> factCache) throws IgniteException { for (int i = 0; i < 100; i++) { int id = idGen++; DimStore store = rand(dataStore.values()); DimProduct prod = rand(dataProduct.values()); factCache.put(id, new FactPurchase(id, prod.getId(), store.getId(), (i + 1))); } }
public JsonObject getSocialObject(String targetId, String tenant, SortOrder order) { List<Activity> activities = listActivitiesChronologically(targetId, tenant); ActivitySummarizer summarizer = new ActivitySummarizer(targetId, order); for (Activity activity : activities) { summarizer.add(activity); } JsonObject summarize = summarizer.summarize(); cache.put(targetId, tenant, summarize); return summarize; }
@Test public void testVersioningCanRevertToOldBehavior() { System.setProperty("net.sf.ehcache.element.version.auto", "true"); try { CacheManager cacheManager = CacheManager.getInstance(); cacheManager.addCache( new Cache( "mltest", 50, MemoryStoreEvictionPolicy.LRU, true, null, true, 0, 0, false, 120, null, null, 0, 2, false)); Cache cache = cacheManager.getCache("mltest"); Element a = new Element("a key", "a value", 1L); cache.put(a); Element aAfter = cache.get("a key"); assertEquals(aAfter.getLastUpdateTime(), aAfter.getVersion()); Element b = new Element("a key", "a value"); cache.put(b); Element bAfter = cache.get("a key"); assertEquals(bAfter.getLastUpdateTime(), bAfter.getVersion()); Element c = new Element("a key", "a value", 3L); cache.put(c); Element cAfter = cache.get("a key"); assertEquals(cAfter.getLastUpdateTime(), cAfter.getVersion()); } finally { System.getProperties().remove("net.sf.ehcache.element.version.auto"); } }
/** Creates pkg new pkg in the database */ public AreaOfInterest create(String id) throws DataException { // create new BO, set whether in the DB or not AreaOfInterest pkg = new AreaOfInterest(id); pkg.setObjectAlreadyInDB(false); // put into the cache cache.put(pkg.id, pkg); // return the new object return pkg; } // create