@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1) @Override protected int sizeOf(Object key, CacheItem<String> value) { String data = value.getData(); return data.length(); } }
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); } }
@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; } }
@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; }
@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); }
@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; } }
@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(); } } }