public synchronized void evictToTargetSize(int targetSize) { if (targetSize <= 0) { values.clear(); } else { checkCleanUpObsoleteEntries(); Iterator<KEY> keys = values.keySet().iterator(); while (keys.hasNext() && values.size() > targetSize) { countEvicted++; keys.next(); keys.remove(); } } }
/** Stores all entries contained in the given map in the cache. */ public void putAll(Map<KEY, VALUE> mapDataToPut) { int targetSize = maxSize - mapDataToPut.size(); if (maxSize > 0 && values.size() > targetSize) { evictToTargetSize(targetSize); } Set<Entry<KEY, VALUE>> entries = mapDataToPut.entrySet(); for (Entry<KEY, VALUE> entry : entries) { put(entry.getKey(), entry.getValue()); } }
public boolean containsKeyWithValue(KEY key) { return get(key) != null; }
private ObjectCache<String, String> createCacheWith4Entries(int expirationMillis) { ObjectCache<String, String> cache = new ObjectCache(ObjectCache.ReferenceType.STRONG, 4, expirationMillis); cache.put("1", "a"); cache.put("2", "b"); cache.put("3", "c"); cache.put("4", "d"); assertEquals(4, cache.size()); return cache; } }
/** Stores an new entry in the cache. */ public VALUE put(KEY key, VALUE object) { CacheEntry<VALUE> entry; if (referenceType == ReferenceType.WEAK) { entry = new CacheEntry<>(new WeakReference<>(object), null); } else if (referenceType == ReferenceType.SOFT) { entry = new CacheEntry<>(new SoftReference<>(object), null); } else { entry = new CacheEntry<>(null, object); } countPutCountSinceEviction++; countPut++; if (isExpiring && nextCleanUpTimestamp == 0) { nextCleanUpTimestamp = System.currentTimeMillis() + expirationMillis + 1; } CacheEntry<VALUE> oldEntry; synchronized (this) { if (values.size() >= maxSize) { evictToTargetSize(maxSize - 1); } oldEntry = values.put(key, entry); } return getValueForRemoved(oldEntry); }
@Test public void testNotExpired() throws InterruptedException { ObjectCache<String, String> cache = new ObjectCache(ObjectCache.ReferenceType.STRONG, 4, 1000); cache.put("1", "a"); Thread.sleep(3); assertEquals(cache.get("1"), "a"); assertEquals(0, cache.getCountExpired()); }
private void doTestBasics(ObjectCache.ReferenceType referenceType) { ObjectCache<String, String> cache = new ObjectCache(referenceType, 10, 0); String value = "foo"; String value2 = "bar"; String key = "mykey"; assertNull(cache.get(key)); assertNull(cache.put(key, value)); assertTrue(cache.containsKey(key)); assertTrue(cache.containsKeyWithValue(key)); assertEquals(value, cache.get(key)); assertEquals(value, cache.put(key, value2)); assertEquals(value2, cache.get(key)); assertEquals(value2, cache.remove(key)); assertNull(value2, cache.get(key)); assertFalse(cache.containsKey(key)); assertFalse(cache.containsKeyWithValue(key)); }
@Test public void testMaxSize() { ObjectCache<String, String> cache = createCacheWith4Entries(0); cache.put("5", "e"); assertEquals(4, cache.size()); assertNull(cache.get("1")); assertEquals(cache.get("5"), "e"); assertEquals(1, cache.getCountEvicted()); }
@Test public void testEvictToTargetSize() { ObjectCache<String, String> cache = createCacheWith4Entries(0); cache.evictToTargetSize(2); assertEquals(2, cache.size()); assertEquals(cache.get("3"), "c"); assertEquals(cache.get("4"), "d"); cache.evictToTargetSize(0); assertEquals(0, cache.size()); }
@Test public void testCleanUpObsoleteEntries() throws InterruptedException { // Use more than one entry to detect ConcurrentModificationException ObjectCache<String, String> cache = createCacheWith4Entries(1); Thread.sleep(3); cache.checkCleanUpObsoleteEntries(); assertEquals(0, cache.size()); assertEquals(4, cache.getCountExpired()); }
void checkCleanUpObsoleteEntries() { if (!isStrongReference || isExpiring) { if ((isExpiring && nextCleanUpTimestamp != 0 && System.currentTimeMillis() > nextCleanUpTimestamp) || countPutCountSinceEviction > maxSize / 2) { cleanUpObsoleteEntries(); } } }
/** * Removes an entry from the cache. * * @return The removed entry */ public VALUE remove(KEY key) { return getValueForRemoved(values.remove(key)); }
@Test public void testExpired() throws InterruptedException { ObjectCache<String, String> cache = new ObjectCache(ObjectCache.ReferenceType.STRONG, 4, 1); cache.put("1", "a"); Thread.sleep(3); assertNull(cache.get("1")); assertEquals(0, cache.size()); assertEquals(1, cache.getCountExpired()); }
/** Stores an new entry in the cache. */ public VALUE put(KEY key, VALUE object) { CacheEntry<VALUE> entry; if (referenceType == ReferenceType.WEAK) { entry = new CacheEntry<>(new WeakReference<>(object), null); } else if (referenceType == ReferenceType.SOFT) { entry = new CacheEntry<>(new SoftReference<>(object), null); } else { entry = new CacheEntry<>(null, object); } countPutCountSinceEviction++; countPut++; if (isExpiring && nextCleanUpTimestamp == 0) { nextCleanUpTimestamp = System.currentTimeMillis() + expirationMillis + 1; } CacheEntry<VALUE> oldEntry; synchronized (this) { if (values.size() >= maxSize) { evictToTargetSize(maxSize - 1); } oldEntry = values.put(key, entry); } return getValueForRemoved(oldEntry); }
void checkCleanUpObsoleteEntries() { if (!isStrongReference || isExpiring) { if ((isExpiring && nextCleanUpTimestamp != 0 && System.currentTimeMillis() > nextCleanUpTimestamp) || countPutCountSinceEviction > maxSize / 2) { cleanUpObsoleteEntries(); } } }
/** * Removes an entry from the cache. * * @return The removed entry */ public VALUE remove(KEY key) { return getValueForRemoved(values.remove(key)); }
/** Stores all entries contained in the given map in the cache. */ public void putAll(Map<KEY, VALUE> mapDataToPut) { int targetSize = maxSize - mapDataToPut.size(); if (maxSize > 0 && values.size() > targetSize) { evictToTargetSize(targetSize); } Set<Entry<KEY, VALUE>> entries = mapDataToPut.entrySet(); for (Entry<KEY, VALUE> entry : entries) { put(entry.getKey(), entry.getValue()); } }
/** Stores an new entry in the cache. */ public VALUE put(KEY key, VALUE object) { CacheEntry<VALUE> entry; if (referenceType == ReferenceType.WEAK) { entry = new CacheEntry<>(new WeakReference<>(object), null); } else if (referenceType == ReferenceType.SOFT) { entry = new CacheEntry<>(new SoftReference<>(object), null); } else { entry = new CacheEntry<>(null, object); } countPutCountSinceEviction++; countPut++; if (isExpiring && nextCleanUpTimestamp == 0) { nextCleanUpTimestamp = System.currentTimeMillis() + expirationMillis + 1; } CacheEntry<VALUE> oldEntry; synchronized (this) { if (values.size() >= maxSize) { evictToTargetSize(maxSize - 1); } oldEntry = values.put(key, entry); } return getValueForRemoved(oldEntry); }