/** * Clear the cache, calling {@link #entryRemoved} on each removed entry. */ public final void evictAll() { trimToSize(-1); // -1 will evict 0-sized elements }
public LruCacheBitmapObjectPersister(ObjectPersister<Bitmap> decoratedPersister, int lruCacheSize) { super(decoratedPersister, new BitmapLruCache(lruCacheSize)); } }
public LruCacheStringObjectPersister(int lruCacheSize) { super(String.class, new StringLruCache(lruCacheSize)); }
@Override public T loadDataFromCache(Object cacheKey, long maxTimeInCacheBeforeExpiry) throws CacheLoadingException { CacheItem<T> cacheItem = lruCache.get(cacheKey); if (cacheItem == null) { Ln.d("Miss from lru cache for %s", cacheKey); if (decoratedPersister != null) { T data = decoratedPersister.loadDataFromCache(cacheKey, maxTimeInCacheBeforeExpiry); if (data == null) { return null; } CacheItem<T> item = new CacheItem<T>(decoratedPersister.getCreationDateInCache(cacheKey), data); Ln.d("Put in lru cache after miss"); lruCache.put(cacheKey, item); return data; } return null; } else { Ln.d("Hit from lru cache for %s", cacheKey); boolean dataCanExpire = maxTimeInCacheBeforeExpiry != DurationInMillis.ALWAYS_RETURNED; boolean dataIsNotExpired = System.currentTimeMillis() - cacheItem.getCreationDate() <= maxTimeInCacheBeforeExpiry; if (!dataCanExpire || dataIsNotExpired) { return cacheItem.getData(); } return null; } }
V createdValue = create(key); if (createdValue == null) { return null; size += safeSizeOf(key, createdValue); entryRemoved(false, key, createdValue, mapValue); return mapValue; } else { trimToSize(maxSize); return createdValue;
/** * Caches {@code value} for {@code key}. The value is moved to the head of * the queue. * @return the previous value mapped by {@code key}. */ public final V put(K key, V value) { if (key == null || value == null) { throw new NullPointerException("key == null || value == null"); } V previous; synchronized (this) { putCount++; size += safeSizeOf(key, value); previous = map.put(key, value); if (previous != null) { size -= safeSizeOf(key, previous); } } if (previous != null) { entryRemoved(false, key, previous, value); } trimToSize(maxSize); return previous; }
@Override public boolean isDataInCache(Object cacheKey, long maxTimeInCacheBeforeExpiry) { CacheItem<T> cacheItem = lruCache.get(cacheKey); if (cacheItem == null) { if (decoratedPersister != null) { return decoratedPersister.isDataInCache(cacheKey, maxTimeInCacheBeforeExpiry); } return false; } else { boolean dataCanExpire = maxTimeInCacheBeforeExpiry != DurationInMillis.ALWAYS_RETURNED; boolean dataIsNotExpired = System.currentTimeMillis() - cacheItem.getCreationDate() <= maxTimeInCacheBeforeExpiry; return !dataCanExpire || dataIsNotExpired; } }
@Override public T saveDataToCacheAndReturnData(T data, Object cacheKey) throws CacheSavingException { CacheItem<T> itemToCache = new CacheItem<T>(data); lruCache.put(cacheKey, itemToCache); Ln.d("Put in lru cache for %s", cacheKey); if (decoratedPersister != null) { decoratedPersister.saveDataToCacheAndReturnData(data, cacheKey); } return data; }
/** * Removes the entry for {@code key} if it exists. * @return the previous value mapped by {@code key}. */ public final V remove(K key) { if (key == null) { throw new NullPointerException("key == null"); } V previous; synchronized (this) { previous = map.remove(key); if (previous != null) { size -= safeSizeOf(key, previous); } } if (previous != null) { entryRemoved(false, key, previous, null); } return previous; }
@Override public List<T> loadAllDataFromCache() throws CacheLoadingException { if (decoratedPersister != null) { return decoratedPersister.loadAllDataFromCache(); } else { Map<Object, CacheItem<T>> cacheMap = lruCache.snapshot(); List<T> allData = new ArrayList<T>(); for (CacheItem<T> item : cacheMap.values()) { allData.add(item.getData()); } return allData; } }
public class Cache<TKey extends Serializable, TValue extends Serializable & ICacheable > { private Map<Serializable, Serializable> cacheStore = Collections.synchronizedMap(new HashMap<Serializable, Serializable>()); public void add(Serializable key, Serializable value) { cacheStore.put(key, value); } @SuppressWarnings("unchecked") public void add(CacheItem cacheItem) { Serializable key = cacheItem.getKey(); //Do not compiles. Incompatible types. Required: TValue. Found: java.io.Serializable Serializable value = cacheItem.getValue(); //I need to cast to (TValue) here to compile //but it gets the Unchecked cast: 'java.io.Serializable' to 'TValue' add(key, value); } }
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1) @Override protected int sizeOf(Object key, CacheItem<String> value) { String data = value.getData(); return data.length(); } }
private int safeSizeOf(K key, V value) { int result = sizeOf(key, value); if (result < 0) { throw new IllegalStateException("Negative size: " + key + "=" + value); } return result; }
@Override public boolean removeDataFromCache(Object cacheKey) { boolean result = false; if (decoratedPersister != null) { result = decoratedPersister.removeDataFromCache(cacheKey); } return result || lruCache.remove(cacheKey) != null; }
@Override public void removeAllDataFromCache() { lruCache.evictAll(); if (decoratedPersister != null) { decoratedPersister.removeAllDataFromCache(); } }
@Override public List<Object> getAllCacheKeys() { if (decoratedPersister != null) { return decoratedPersister.getAllCacheKeys(); } else { return new ArrayList<Object>(lruCache.snapshot().keySet()); } }
@Override public long getCreationDateInCache(Object cacheKey) throws CacheLoadingException { CacheItem<T> cacheItem = lruCache.get(cacheKey); if (cacheItem != null) { return cacheItem.getCreationDate(); } else { if (decoratedPersister != null) { return decoratedPersister.getCreationDateInCache(cacheKey); } } throw new CacheLoadingException("Data could not be found in cache for cacheKey=" + cacheKey); }
public LruCacheBitmapObjectPersister(int lruCacheSize) { super(Bitmap.class, new BitmapLruCache(lruCacheSize)); }
public LruCacheStringObjectPersister(ObjectPersister<String> decoratedPersister, int lruCacheSize) { super(decoratedPersister, new StringLruCache(lruCacheSize)); }
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1) @Override protected int sizeOf(Object key, CacheItem<Bitmap> value) { Bitmap data = value.getData(); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) { return data.getRowBytes() * data.getHeight(); } else { return data.getByteCount(); } } }