private Observable<V> fromMemory(User user, K key) { UserMemoryCache<K, V> userMemoryCache = userMemoryCaches.get(user); if (!userMemoryCache.containsKey(key)) { return Observable.empty(); } else { Pair<Long, V> cachedValue = userMemoryCache.get(key); long cachedSince = DateUtils.getCurrentTimestamp() - cachedValue.first; if (cachedSince < staleThreshold) { return Observable.just(userMemoryCache.get(key).second); } else { // Value has expired userMemoryCache.remove(key); return Observable.empty(); } } }
private void saveToMemory(User user, K key, V value) { long currentTimestamp = DateUtils.getCurrentTimestamp(); getUserCache(user).put(key, value, currentTimestamp); }