private boolean checkCache(@Nonnull Request request) { if (!mCache.hasCache()) { return false; } final String key = request.getCacheKey(); if (key == null) { return false; } final Cache.Entry entry = mCache.get(request.getType().getCacheKey(key)); if (entry == null) { return false; } request.onSuccess(entry.data); return true; }
@Test public void testShouldRemoveAllEntriesOfType() throws Exception { final ConcurrentCache cache = new ConcurrentCache(new MapCache()); final Cache.Key k1 = newKey(); cache.put(k1, newEntry()); final Cache.Key k11 = new Cache.Key(k1.type, k1.key + "test"); cache.put(k11, newEntry()); final Cache.Key k2 = new Cache.Key(k1.type + 1, "test"); cache.put(k2, newEntry()); final Cache.Key k3 = new Cache.Key(k1.type + 2, "test"); cache.put(k3, newEntry()); assertNotNull(cache.get(k1)); assertNotNull(cache.get(k11)); assertNotNull(cache.get(k2)); assertNotNull(cache.get(k3)); cache.removeAll(k1.type); assertNull(cache.get(k1)); assertNull(cache.get(k11)); assertNotNull(cache.get(k2)); assertNotNull(cache.get(k3)); }
@Test public void testShouldRemoveValue() throws Exception { final ConcurrentCache cache = new ConcurrentCache(new MapCache()); final Cache.Key key = newKey(); final Cache.Entry entry = newEntry(); cache.put(key, entry); assertSame(entry, cache.get(key)); cache.remove(key); assertNull(cache.get(key)); }
@Test public void testShouldRemoveAllEntries() throws Exception { final ConcurrentCache cache = new ConcurrentCache(new MapCache()); final Cache.Key k1 = newKey(); cache.put(k1, newEntry()); final Cache.Key k2 = newKey(); cache.put(k2, newEntry()); final Cache.Key k3 = newKey(); cache.put(k3, newEntry()); cache.clear(); assertNull(cache.get(k1)); assertNull(cache.get(k2)); assertNull(cache.get(k3)); }
@Test public void testShouldReturnNullIfValueExpired() throws Exception { final ConcurrentCache cache = new ConcurrentCache(new MapCache()); final Cache.Entry entry = newEntry(-DAY); final Cache.Key key = newKey(); cache.put(key, entry); final Cache.Entry actual = cache.get(key); assertNull(actual); }
@Test public void testShouldReturnNotExpiredValue() throws Exception { final ConcurrentCache cache = new ConcurrentCache(new MapCache()); final Cache.Entry expected = newEntry(); final Cache.Key key = newKey(); cache.put(key, expected); final Cache.Entry actual = cache.get(key); assertSame(expected, actual); }
@Override public void run() { switch (random.nextInt(6)) { case 0: cache.get(newKey()); break; case 1: cache.put(newKey(), newEntry()); break; case 2: cache.init(); break; case 3: cache.remove(newKey()); break; case 4: cache.removeAll(newKey().type); break; case 5: cache.clear(); break; } } });
@Test public void testShouldPutOnlyIfNotExists() throws Exception { final ConcurrentCache cache = new ConcurrentCache(new MapCache()); final Cache.Entry entry = newEntry(); final Cache.Entry newEntry = newEntry(); final Cache.Key key = newKey(); cache.put(key, entry); cache.putIfNotExist(key, newEntry); assertSame(entry, cache.get(key)); }