Skip to content

Commit

Permalink
Re-create http disk LRU cache after it was deleted (#1020)
Browse files Browse the repository at this point in the history
Closes #1017
  • Loading branch information
sav007 authored Aug 13, 2018
1 parent 7983892 commit 7358a79
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import com.apollographql.apollo.api.cache.http.HttpCacheRecordEditor;
import com.apollographql.apollo.api.cache.http.HttpCacheStore;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;

import org.jetbrains.annotations.NotNull;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import okhttp3.internal.cache.DiskLruCache;
import okhttp3.internal.io.FileSystem;
Expand All @@ -20,18 +22,34 @@ public final class DiskLruHttpCacheStore implements HttpCacheStore {
private static final int ENTRY_BODY = 1;
private static final int ENTRY_COUNT = 2;

private final DiskLruCache cache;
private final FileSystem fileSystem;
private final File directory;
private final long maxSize;
private DiskLruCache cache;
private final ReadWriteLock cacheLock = new ReentrantReadWriteLock();

public DiskLruHttpCacheStore(@NotNull File directory, long maxSize) {
this.cache = DiskLruCache.create(FileSystem.SYSTEM, directory, VERSION, ENTRY_COUNT, maxSize);
this(FileSystem.SYSTEM, directory, maxSize);
}

public DiskLruHttpCacheStore(@NotNull FileSystem fileSystem, @NotNull File directory, long maxSize) {
this.cache = DiskLruCache.create(fileSystem, directory, VERSION, ENTRY_COUNT, maxSize);
this.fileSystem = fileSystem;
this.directory = directory;
this.maxSize = maxSize;

cache = createDiskLruCache();
}

@Override public HttpCacheRecord cacheRecord(@NotNull String cacheKey) throws IOException {
final DiskLruCache.Snapshot snapshot = cache.get(cacheKey);
final DiskLruCache.Snapshot snapshot;

cacheLock.readLock().lock();
try {
snapshot = cache.get(cacheKey);
} finally {
cacheLock.readLock().unlock();
}

if (snapshot == null) {
return null;
}
Expand All @@ -53,7 +71,15 @@ public DiskLruHttpCacheStore(@NotNull FileSystem fileSystem, @NotNull File direc
}

@Override public HttpCacheRecordEditor cacheRecordEditor(@NotNull String cacheKey) throws IOException {
final DiskLruCache.Editor editor = cache.edit(cacheKey);
final DiskLruCache.Editor editor;

cacheLock.readLock().lock();
try {
editor = cache.edit(cacheKey);
} finally {
cacheLock.readLock().unlock();
}

if (editor == null) {
return null;
}
Expand All @@ -78,10 +104,25 @@ public DiskLruHttpCacheStore(@NotNull FileSystem fileSystem, @NotNull File direc
}

@Override public void delete() throws IOException {
cache.delete();
cacheLock.writeLock().lock();
try {
cache.delete();
cache = createDiskLruCache();
} finally {
cacheLock.writeLock().unlock();
}
}

@Override public void remove(@NotNull String cacheKey) throws IOException {
cache.remove(cacheKey);
cacheLock.readLock().lock();
try {
cache.remove(cacheKey);
} finally {
cacheLock.readLock().unlock();
}
}

private DiskLruCache createDiskLruCache() {
return DiskLruCache.create(fileSystem, directory, VERSION, ENTRY_COUNT, maxSize);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,25 @@ public class HttpCacheTest {
assertThat(lastHttResponse.cacheResponse()).isNotNull();
}

@Test @SuppressWarnings("CheckReturnValue") public void cacheAfterDelete() throws Exception {
enqueueResponse("/HttpCacheTestAllPlanets.json");
Rx2Apollo.from(apolloClient
.query(new AllPlanetsQuery()))
.test();
checkCachedResponse("/HttpCacheTestAllPlanets.json");

cacheStore.delegate.delete();

enqueueResponse("/HttpCacheTestAllPlanets.json");
Rx2Apollo.from(apolloClient
.query(new AllPlanetsQuery())
.httpCachePolicy(HttpCachePolicy.CACHE_FIRST))
.test();
checkCachedResponse("/HttpCacheTestAllPlanets.json");

assertThat(server.getRequestCount()).isEqualTo(2);
}

@Test @SuppressWarnings("CheckReturnValue") public void cacheStale() throws Exception {
enqueueResponse("/HttpCacheTestAllPlanets.json");
Rx2Apollo.from(apolloClient
Expand Down

0 comments on commit 7358a79

Please sign in to comment.