@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 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 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; } }