DiskLruCache.Editor editor = diskCache.edit(safeKey); if (editor == null) { throw new IllegalStateException("Had two simultaneous puts for: " + safeKey);
/** * Returns an editor for the entry named {@code key}, or null if another * edit is in progress. */ public Editor edit(String key) throws IOException { return edit(key, ANY_SEQUENCE_NUMBER); }
@Override public void put(Key key, Writer writer) { String safeKey = safeKeyGenerator.getSafeKey(key); writeLocker.acquire(key); try { DiskLruCache.Editor editor = getDiskCache().edit(safeKey); // Editor will be null if there are two concurrent puts. In the worst case we will just silently fail. if (editor != null) { try { File file = editor.getFile(0); if (writer.write(file)) { editor.commit(); } } finally { editor.abortUnlessCommitted(); } } } catch (IOException e) { if (Log.isLoggable(TAG, Log.WARN)) { Log.w(TAG, "Unable to put to disk cache", e); } } finally { writeLocker.release(key); } }
/** * Returns an editor for the entry named {@code key}, or null if another * edit is in progress. */ public Editor edit(String key) throws IOException { return edit(key, ANY_SEQUENCE_NUMBER); }
/** * Returns an editor for the entry named {@code key}, or null if another * edit is in progress. */ public Editor edit(String key) throws IOException { return edit(key, ANY_SEQUENCE_NUMBER); }
@Test public void nullKeyThrows() throws Exception { try { cache.edit(null); Assert.fail(); } catch (NullPointerException expected) { } }
private void set(String key, String value0, String value1) throws Exception { DiskLruCache.Editor editor = cache.edit(key); editor.set(0, value0); editor.set(1, value1); editor.commit(); }
@Test public void unterminatedEditIsRevertedOnClose() throws Exception { cache.edit("k1"); cache.close(); assertJournalEquals("DIRTY k1", "REMOVE k1"); }
@Test public void journalWithEditAndPublishAndRead() throws Exception { DiskLruCache.Editor k1Creator = cache.edit("k1"); k1Creator.set(0, "AB"); k1Creator.set(1, "C"); k1Creator.commit(); DiskLruCache.Editor k2Creator = cache.edit("k2"); k2Creator.set(0, "DEF"); k2Creator.set(1, "G"); k2Creator.commit(); DiskLruCache.Value k1Value = cache.get("k1"); cache.close(); assertJournalEquals("DIRTY k1", "CLEAN k1 2 1", "DIRTY k2", "CLEAN k2 3 1", "READ k1"); }
@Test public void cannotOperateOnEditAfterPublish() throws Exception { DiskLruCache.Editor editor = cache.edit("k1"); editor.set(0, "A"); editor.set(1, "B"); editor.commit(); assertInoperable(editor); }
@Test public void cannotOperateOnEditAfterRevert() throws Exception { DiskLruCache.Editor editor = cache.edit("k1"); editor.set(0, "A"); editor.set(1, "B"); editor.abort(); assertInoperable(editor); }
@Test public void revertedNewFileIsRemoveInJournal() throws Exception { DiskLruCache.Editor creator = cache.edit("k1"); assertJournalEquals("DIRTY k1"); // DIRTY must always be flushed. creator.set(0, "AB"); creator.set(1, "C"); creator.abort(); cache.close(); assertJournalEquals("DIRTY k1", "REMOVE k1"); }
@Test public void revertWithTooFewValues() throws Exception { DiskLruCache.Editor creator = cache.edit("k1"); creator.set(1, "A"); creator.abort(); assertThat(getCleanFile("k1", 0).exists()).isFalse(); assertThat(getCleanFile("k1", 1).exists()).isFalse(); assertThat(getDirtyFile("k1", 0).exists()).isFalse(); assertThat(getDirtyFile("k1", 1).exists()).isFalse(); assertThat(cache.get("k1")).isNull(); }
@Test public void journalWithEditAndPublish() throws Exception { DiskLruCache.Editor creator = cache.edit("k1"); assertJournalEquals("DIRTY k1"); // DIRTY must always be flushed. creator.set(0, "AB"); creator.set(1, "C"); creator.commit(); cache.close(); assertJournalEquals("DIRTY k1", "CLEAN k1 2 1"); }
@Test public void explicitRemoveAppliedToDiskImmediately() throws Exception { DiskLruCache.Editor editor = cache.edit("k1"); editor.set(0, "ABC"); editor.set(1, "B"); editor.commit(); File k1 = getCleanFile("k1", 0); assertThat(readFile(k1)).isEqualTo("ABC"); cache.remove("k1"); assertThat(k1.exists()).isFalse(); }
@Test public void journalDoesNotIncludeReadOfYetUnpublishedValue() throws Exception { DiskLruCache.Editor creator = cache.edit("k1"); assertThat(cache.get("k1")).isNull(); creator.set(0, "A"); creator.set(1, "BC"); creator.commit(); cache.close(); assertJournalEquals("DIRTY k1", "CLEAN k1 1 2"); }
@Test public void updateExistingEntryWithTooFewValuesReusesPreviousValues() throws Exception { DiskLruCache.Editor creator = cache.edit("k1"); creator.set(0, "A"); creator.set(1, "B"); creator.commit(); DiskLruCache.Editor updater = cache.edit("k1"); updater.set(0, "C"); updater.commit(); DiskLruCache.Value value = cache.get("k1"); assertThat(value.getString(0)).isEqualTo("C"); assertThat(value.getLength(0)).isEqualTo(1); assertThat(value.getString(1)).isEqualTo("B"); assertThat(value.getLength(1)).isEqualTo(1); }
@Test public void readAndWriteEntryAcrossCacheOpenAndClose() throws Exception { DiskLruCache.Editor creator = cache.edit("k1"); creator.set(0, "A"); creator.set(1, "B"); creator.commit(); cache.close(); cache = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE); DiskLruCache.Value value = cache.get("k1"); assertThat(value.getString(0)).isEqualTo("A"); assertThat(value.getLength(0)).isEqualTo(1); assertThat(value.getString(1)).isEqualTo("B"); assertThat(value.getLength(1)).isEqualTo(1); }
@Test public void readAndWriteEntryWithoutProperClose() throws Exception { DiskLruCache.Editor creator = cache.edit("k1"); creator.set(0, "A"); creator.set(1, "B"); creator.commit(); // Simulate a dirty close of 'cache' by opening the cache directory again. DiskLruCache cache2 = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE); DiskLruCache.Value value = cache2.get("k1"); assertThat(value.getString(0)).isEqualTo("A"); assertThat(value.getLength(0)).isEqualTo(1); assertThat(value.getString(1)).isEqualTo("B"); assertThat(value.getLength(1)).isEqualTo(1); cache2.close(); }
@Test public void writeAndReadEntry() throws Exception { DiskLruCache.Editor creator = cache.edit("k1"); creator.set(0, "ABC"); creator.set(1, "DE"); assertThat(creator.getString(0)).isNull(); assertThat(creator.getString(1)).isNull(); creator.commit(); DiskLruCache.Value value = cache.get("k1"); assertThat(value.getString(0)).isEqualTo("ABC"); assertThat(value.getLength(0)).isEqualTo(3); assertThat(value.getString(1)).isEqualTo("DE"); assertThat(value.getLength(1)).isEqualTo(2); }