@Override public void onPurchasesChanged() { mCache.removeAll(RequestType.GET_PURCHASES.getCacheKeyType()); } };
@Override public void onSuccess(@Nonnull Purchase result) { mCache.removeAll(RequestType.GET_PURCHASES.getCacheKeyType()); super.onSuccess(result); } };
@Override public void onError(int response, @Nonnull Exception e) { final RequestType type = mRequest.getType(); // sometimes it is possible that cached data is not synchronized with data on Google Play => we can // clear caches if such situation occurs switch (type) { case PURCHASE: case CHANGE_PURCHASE: if (response == ITEM_ALREADY_OWNED) { mCache.removeAll(RequestType.GET_PURCHASES.getCacheKeyType()); } break; case CONSUME_PURCHASE: if (response == ITEM_NOT_OWNED) { mCache.removeAll(RequestType.GET_PURCHASES.getCacheKeyType()); } break; } super.onError(response, e); } }
@Override public void onSuccess(@Nonnull R result) { final String key = mRequest.getCacheKey(); final RequestType type = mRequest.getType(); if (key != null) { final long now = currentTimeMillis(); final Cache.Entry entry = new Cache.Entry(result, now + type.expiresIn); mCache.putIfNotExist(type.getCacheKey(key), entry); } switch (type) { case PURCHASE: case CHANGE_PURCHASE: case CONSUME_PURCHASE: // these requests might affect the state of purchases => we need to invalidate caches. // see Billing#onPurchaseFinished() also mCache.removeAll(RequestType.GET_PURCHASES.getCacheKeyType()); break; } super.onSuccess(result); }
@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 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)); }