Skip to content
This repository was archived by the owner on Jul 21, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/main/java/com/jakewharton/disklrucache/DiskLruCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.Callable;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
Expand Down Expand Up @@ -150,6 +151,7 @@ public final class DiskLruCache implements Closeable {
private final LinkedHashMap<String, Entry> lruEntries =
new LinkedHashMap<String, Entry>(0, 0.75f, true);
private int redundantOpCount;
private final Map<Editor, Object> activeEditors = new WeakHashMap<Editor, Object>();

/**
* To differentiate between old and current snapshots, each entry is given
Expand Down Expand Up @@ -471,6 +473,8 @@ private synchronized Editor edit(String key, long expectedSequenceNumber) throws
// Flush the journal before creating files to prevent file leaks.
journalWriter.write(DIRTY + ' ' + key + '\n');
journalWriter.flush();

activeEditors.put(editor, null);
return editor;
}

Expand Down Expand Up @@ -510,6 +514,7 @@ private synchronized void completeEdit(Editor editor, boolean success) throws IO
if (entry.currentEditor != editor) {
throw new IllegalStateException();
}
activeEditors.remove(editor);

// If this edit is creating the entry for the first time, every index must have a value.
if (success && !entry.readable) {
Expand Down Expand Up @@ -654,6 +659,23 @@ public void delete() throws IOException {
Util.deleteContents(directory);
}

/**
* Deletes all of the stored values in the cache. This will delete
* all files in the cache directory including files that weren't created by
* the cache. Any uncommitted {@link Editor editors} will be ignored.
*/
public synchronized void evictAll() throws IOException {
Util.deleteContents(directory);
for (Editor editor : activeEditors.keySet()) {
editor.invalidate();
}
activeEditors.clear();
redundantOpCount = 0;
size = 0;
lruEntries.clear();
rebuildJournal();
}

private void validateKey(String key) {
Matcher matcher = LEGAL_KEY_PATTERN.matcher(key);
if (!matcher.matches()) {
Expand Down Expand Up @@ -723,6 +745,7 @@ public final class Editor {
private final boolean[] written;
private boolean hasErrors;
private boolean committed;
private boolean invalidated;

private Editor(Entry entry) {
this.entry = entry;
Expand Down Expand Up @@ -807,6 +830,9 @@ public void set(int index, String value) throws IOException {
* edit lock so another edit may be started on the same key.
*/
public void commit() throws IOException {
if (invalidated) {
return;
}
if (hasErrors) {
completeEdit(this, false);
remove(entry.key); // The previous entry is stale.
Expand All @@ -821,6 +847,9 @@ public void commit() throws IOException {
* started on the same key.
*/
public void abort() throws IOException {
if (invalidated) {
return;
}
completeEdit(this, false);
}

Expand All @@ -833,6 +862,10 @@ public void abortUnlessCommitted() {
}
}

public void invalidate() {
invalidated = true;
}

private class FaultHidingOutputStream extends FilterOutputStream {
private FaultHidingOutputStream(OutputStream out) {
super(out);
Expand Down
35 changes: 32 additions & 3 deletions src/test/java/com/jakewharton/disklrucache/DiskLruCacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,16 @@ public final class DiskLruCacheTest {
@Before public void setUp() throws Exception {
javaTmpDir = System.getProperty("java.io.tmpdir");
cacheDir = new File(javaTmpDir, "DiskLruCacheTest");
FileUtils.deleteQuietly(cacheDir);
cacheDir.mkdir();
journalFile = new File(cacheDir, JOURNAL_FILE);
journalBkpFile = new File(cacheDir, JOURNAL_FILE_BACKUP);
for (File file : cacheDir.listFiles()) {
file.delete();
}
cache = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE);
}

@After public void tearDown() throws Exception {
cache.close();
FileUtils.deleteQuietly(cacheDir);
}

@Test public void emptyCache() throws Exception {
Expand Down Expand Up @@ -876,6 +875,36 @@ public final class DiskLruCacheTest {
assertThat(cache.get("a")).isNull();
}

@Test public void evictRemovesEverythingFromCache() throws Exception {
set("k1", "A", "B");
assertValue("k1", "A", "B");

cache.evictAll();
assertAbsent("k1");
assertThat(cache.size()).isZero();
assertThat(cache.isClosed()).isFalse();

// Make sure we are still in a writable state
set("k1", "C", "D");
assertValue("k1", "C", "D");
}

@Test public void editorCommittedAfterEvictDoesNothing() throws Exception {
DiskLruCache.Editor kOne = cache.edit("k");
kOne.set(0, "A");
kOne.set(1, "B");

cache.evictAll();

DiskLruCache.Editor kTwo = cache.edit("k");
kTwo.set(0, "C");
kTwo.set(1, "D");

kOne.commit();
kTwo.commit();
assertValue("k", "C", "D");
}

private void assertJournalEquals(String... expectedBodyLines) throws Exception {
List<String> expectedLines = new ArrayList<String>();
expectedLines.add(MAGIC);
Expand Down