@Override public Map<Class, Map<String, Record>> dump() { Map<Class, Map<String, Record>> dump = new LinkedHashMap<>(); dump.put(this.getClass(), Collections.unmodifiableMap(new LinkedHashMap<>(lruCache.asMap()))); if (nextCache().isPresent()) { dump.putAll(nextCache().get().dump()); } return dump; } }
@Override public Record call() throws Exception { return nextCache().flatMap(new Function<NormalizedCache, Optional<Record>>() { @NotNull @Override public Optional<Record> apply(@NotNull NormalizedCache cache) { return Optional.fromNullable(cache.loadRecord(key, cacheHeaders)); } }).get(); // lruCache.get(key, callable) requires non-null. } });
@SuppressWarnings("ResultOfMethodCallIgnored") @Override public void clearAll() { nextCache().apply(new Action<NormalizedCache>() { @Override public void apply(@NotNull NormalizedCache cache) { cache.clearAll(); } }); clearCurrentCache(); }
@Override public boolean remove(@NotNull final CacheKey cacheKey, final boolean cascade) { checkNotNull(cacheKey, "cacheKey == null"); boolean result; result = nextCache().map(new Function<NormalizedCache, Boolean>() { @NotNull @Override public Boolean apply(@NotNull NormalizedCache cache) { return cache.remove(cacheKey, cascade); } }).or(Boolean.FALSE); Record record = lruCache.getIfPresent(cacheKey.key()); if (record != null) { lruCache.invalidate(cacheKey.key()); result = true; if (cascade) { for (CacheReference cacheReference : record.referencedFields()) { result = result & remove(CacheKey.from(cacheReference.key()), true); } } } return result; }
@Test public void testClearPrimaryCache() { LruNormalizedCacheFactory secondaryCacheFactory = new LruNormalizedCacheFactory(EvictionPolicy.NO_EVICTION); LruNormalizedCache primaryCache = (LruNormalizedCache) new LruNormalizedCacheFactory(EvictionPolicy.NO_EVICTION) .chain(secondaryCacheFactory).createChain(basicFieldAdapter); Record record = Record.builder("key").build(); primaryCache.merge(record, CacheHeaders.NONE); primaryCache.clearCurrentCache(); assertThat(primaryCache.nextCache().get() .loadRecord("key", CacheHeaders.NONE)).isNotNull(); assertThat(primaryCache.nextCache().get() .loadRecord("key", CacheHeaders.NONE)).isNotNull(); }
@Override public Record call() throws Exception { return nextCache().flatMap(new Function<NormalizedCache, Optional<Record>>() { @Nonnull @Override public Optional<Record> apply(@Nonnull NormalizedCache cache) { return Optional.fromNullable(cache.loadRecord(key, cacheHeaders)); } }).get(); // lruCache.get(key, callable) requires non-null. } });
@Override public void clearAll() { //noinspection ResultOfMethodCallIgnored nextCache().apply(new Action<NormalizedCache>() { @Override public void apply(@Nonnull NormalizedCache cache) { cache.clearAll(); } }); clearCurrentCache(); }
@Override public Record call() throws Exception { return nextCache().flatMap(new Function<NormalizedCache, Optional<Record>>() { @Nonnull @Override public Optional<Record> apply(@Nonnull NormalizedCache cache) { return Optional.fromNullable(cache.loadRecord(key, cacheHeaders)); } }).get(); // lruCache.get(key, callable) requires non-null. } });
@Override public void clearAll() { //noinspection ResultOfMethodCallIgnored nextCache().apply(new Action<NormalizedCache>() { @Override public void apply(@Nonnull NormalizedCache cache) { cache.clearAll(); } }); clearCurrentCache(); }
@Nonnull @Override public Set<String> merge(@Nonnull final Record apolloRecord, @Nonnull final CacheHeaders cacheHeaders) { if (cacheHeaders.hasHeader(GraphQLCacheHeaders.DO_NOT_STORE)) { return Collections.emptySet(); } //noinspection ResultOfMethodCallIgnored nextCache().apply(new Action<NormalizedCache>() { @Override public void apply(@Nonnull NormalizedCache cache) { cache.merge(apolloRecord, cacheHeaders); } }); final Record oldRecord = lruCache.getIfPresent(apolloRecord.key()); if (oldRecord == null) { lruCache.put(apolloRecord.key(), apolloRecord); return Collections.emptySet(); } else { Set<String> changedKeys = oldRecord.mergeWith(apolloRecord); //re-insert to trigger new weight calculation lruCache.put(apolloRecord.key(), oldRecord); return changedKeys; } }
@Nonnull @Override public Set<String> merge(@Nonnull final Record apolloRecord, @Nonnull final CacheHeaders cacheHeaders) { if (cacheHeaders.hasHeader(GraphQLCacheHeaders.DO_NOT_STORE)) { return Collections.emptySet(); } //noinspection ResultOfMethodCallIgnored nextCache().apply(new Action<NormalizedCache>() { @Override public void apply(@Nonnull NormalizedCache cache) { cache.merge(apolloRecord, cacheHeaders); } }); final Record oldRecord = lruCache.getIfPresent(apolloRecord.key()); if (oldRecord == null) { lruCache.put(apolloRecord.key(), apolloRecord); return Collections.emptySet(); } else { Set<String> changedKeys = oldRecord.mergeWith(apolloRecord); //re-insert to trigger new weight calculation lruCache.put(apolloRecord.key(), oldRecord); return changedKeys; } }
@Override public boolean remove(@Nonnull final CacheKey cacheKey) { checkNotNull(cacheKey, "cacheKey == null"); boolean result; result = nextCache().map(new Function<NormalizedCache, Boolean>() { @Nonnull @Override public Boolean apply(@Nonnull NormalizedCache cache) { return cache.remove(cacheKey); } }).or(Boolean.FALSE); if (lruCache.getIfPresent(cacheKey.key()) != null) { lruCache.invalidate(cacheKey.key()); result = true; } return result; }
@Override public boolean remove(@Nonnull final CacheKey cacheKey) { checkNotNull(cacheKey, "cacheKey == null"); boolean result; result = nextCache().map(new Function<NormalizedCache, Boolean>() { @Nonnull @Override public Boolean apply(@Nonnull NormalizedCache cache) { return cache.remove(cacheKey); } }).or(Boolean.FALSE); if (lruCache.getIfPresent(cacheKey.key()) != null) { lruCache.invalidate(cacheKey.key()); result = true; } return result; }