@Override public void init() { try { mCache.init(); } catch (Exception e) { Billing.error(e); } }
public void putIfNotExist(@Nonnull Key key, @Nonnull Entry entry) { if (mCache == null) { return; } synchronized (this) { if (mCache.get(key) == null) { Billing.debug(TAG, "Adding entry with key=" + key + " to the cache"); mCache.put(key, entry); } else { Billing.debug(TAG, "Entry with key=" + key + " is already in the cache, won't add"); } } }
@Override public void clear() { try { mCache.clear(); } catch (Exception e) { Billing.error(e); } } }
@Override public void put(@Nonnull Key key, @Nonnull Entry entry) { try { mCache.put(key, entry); } catch (Exception e) { Billing.error(e); } }
@Override @Nullable public Entry get(@Nonnull Key key) { if (mCache == null) { return null; } synchronized (this) { final Entry entry = mCache.get(key); if (entry == null) { Billing.debug(TAG, "Key=" + key + " is not in the cache"); return null; } final long now = currentTimeMillis(); if (now >= entry.expiresAt) { Billing.debug(TAG, "Key=" + key + " is in the cache but was expired at " + entry.expiresAt + ", now is " + now); mCache.remove(key); return null; } Billing.debug(TAG, "Key=" + key + " is in the cache"); return entry; } }
@Override public void remove(@Nonnull Key key) { try { mCache.remove(key); } catch (Exception e) { Billing.error(e); } }
@Override public void removeAll(int type) { try { mCache.removeAll(type); } catch (Exception e) { Billing.error(e); } }
@Override @Nullable public Entry get(@Nonnull Key key) { try { return mCache.get(key); } catch (Exception e) { Billing.error(e); return null; } }
@Override public void put(@Nonnull Key key, @Nonnull Entry entry) { if (mCache == null) { return; } synchronized (this) { Billing.debug(TAG, "Adding entry with key=" + key + " to the cache"); mCache.put(key, entry); } }
@Override public void remove(@Nonnull Key key) { if (mCache == null) { return; } synchronized (this) { Billing.debug(TAG, "Removing entry with key=" + key + " from the cache"); mCache.remove(key); } }
@Override public void removeAll(int type) { if (mCache == null) { return; } synchronized (this) { Billing.debug(TAG, "Removing all entries with type=" + type + " from the cache"); mCache.removeAll(type); } }
@Test public void testShouldPutAllEntries() throws Exception { final Cache mockCache = mock(Cache.class); final ConcurrentCache cache = new ConcurrentCache(mockCache); final Cache.Entry entry = newEntry(); cache.put(newKey(), entry); cache.put(newKey(), entry); cache.put(newKey(), entry); verify(mockCache, times(3)).put(any(Cache.Key.class), eq(entry)); }
@Override public void init() { if (mCache == null) { return; } synchronized (this) { Billing.debug(TAG, "Initializing cache"); mCache.init(); } }
@Override public void clear() { if (mCache == null) { return; } synchronized (this) { Billing.debug(TAG, "Clearing the cache"); mCache.clear(); } } }
@Test public void testShouldCallInit() throws Exception { final Cache mockCache = mock(Cache.class); final ConcurrentCache cache = new ConcurrentCache(mockCache); cache.init(); verify(mockCache, times(1)).init(); }