/** 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 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); }
/** 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 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()); }
/** 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 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); }
/** 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); }