@Override public File get(Key key) { String safeKey = safeKeyGenerator.getSafeKey(key); if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "Get: Obtained: " + safeKey + " for for Key: " + key); } File result = null; try { // It is possible that the there will be a put in between these two gets. If so that shouldn't // be a problem because we will always put the same value at the same key so our input streams // will still represent the same data. final DiskLruCache.Value value = getDiskCache().get(safeKey); if (value != null) { result = value.getFile(0); } } catch (IOException e) { if (Log.isLoggable(TAG, Log.WARN)) { Log.w(TAG, "Unable to get from disk cache", e); } } return result; }
Value current = diskCache.get(safeKey); if (current != null) { return;
@Override public File get(Key key) { String safeKey = safeKeyGenerator.getSafeKey(key); File result = null; try { //It is possible that the there will be a put in between these two gets. If so that shouldn't be a problem //because we will always put the same value at the same key so our input streams will still represent //the same data final DiskLruCache.Value value = getDiskCache().get(safeKey); if (value != null) { result = value.getFile(0); } } catch (IOException e) { if (Log.isLoggable(TAG, Log.WARN)) { Log.w(TAG, "Unable to get from disk cache", e); } } return result; }
private boolean containsKey(String key) { boolean contained = false; try { contained = mDiskLruCache.get(key) != null; } catch (IOException e) { e.printStackTrace(); } return contained; }
/** @see <a href="https://github.com/JakeWharton/DiskLruCache/issues/2">Issue #2</a> */ @Test public void aggressiveClearingHandlesRead() throws Exception { FileUtils.deleteDirectory(cacheDir); assertThat(cache.get("a")).isNull(); }
@Test public void fileDeletedExternally() throws Exception { set("a", "a", "a"); getCleanFile("a", 1).delete(); assertThat(cache.get("a")).isNull(); }
/** @see <a href="https://github.com/JakeWharton/DiskLruCache/issues/2">Issue #2</a> */ @Test public void aggressiveClearingHandlesEdit() throws Exception { set("a", "a", "a"); DiskLruCache.Editor a = cache.get("a").edit(); FileUtils.deleteDirectory(cacheDir); a.set(1, "a2"); a.commit(); }
@Test public void readingTheSameFileMultipleTimes() throws Exception { set("a", "a", "b"); DiskLruCache.Value value = cache.get("a"); assertThat(value.getFile(0)).isSameAs(value.getFile(0)); }
@Test public void editSameVersion() throws Exception { set("a", "a", "a"); DiskLruCache.Value value = cache.get("a"); DiskLruCache.Editor editor = value.edit(); editor.set(1, "a2"); editor.commit(); assertValue("a", "a", "a2"); }
@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 editSnapshotAfterChangeCommitted() throws Exception { set("a", "a", "a"); DiskLruCache.Value value = cache.get("a"); DiskLruCache.Editor toAbort = value.edit(); toAbort.set(0, "b"); toAbort.commit(); assertThat(value.edit()).isNull(); }
@Test public void openWithInvalidJournalLineClearsDirectory() throws Exception { cache.close(); generateSomeGarbageFiles(); createJournal("CLEAN k1 1 1", "BOGUS"); cache = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE); assertGarbageFilesAllDeleted(); assertThat(cache.get("k1")).isNull(); }
@Test public void openWithInvalidFileSizeClearsDirectory() throws Exception { cache.close(); generateSomeGarbageFiles(); createJournal("CLEAN k1 0000x001 1"); cache = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE); assertGarbageFilesAllDeleted(); assertThat(cache.get("k1")).isNull(); }
@Test public void openWithTooManyFileSizesClearsDirectory() throws Exception { cache.close(); generateSomeGarbageFiles(); createJournal("CLEAN k1 1 1 1"); cache = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE); assertGarbageFilesAllDeleted(); assertThat(cache.get("k1")).isNull(); }
@Test public void editSnapshotAfterChangeAborted() throws Exception { set("a", "a", "a"); DiskLruCache.Value value = cache.get("a"); DiskLruCache.Editor toAbort = value.edit(); toAbort.set(0, "b"); toAbort.abort(); DiskLruCache.Editor editor = value.edit(); editor.set(1, "a2"); editor.commit(); assertValue("a", "a", "a2"); }
@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"); }
private void assertValue(String key, String value0, String value1) throws Exception { DiskLruCache.Value value = cache.get(key); assertThat(value.getString(0)).isEqualTo(value0); assertThat(value.getLength(0)).isEqualTo(value0.length()); assertThat(value.getString(1)).isEqualTo(value1); assertThat(value.getLength(1)).isEqualTo(value1.length()); assertThat(getCleanFile(key, 0)).exists(); assertThat(getCleanFile(key, 1)).exists(); } }
@Test public void editSinceEvicted() throws Exception { cache.close(); cache = DiskLruCache.open(cacheDir, appVersion, 2, 10); set("a", "aa", "aaa"); // size 5 DiskLruCache.Value value = cache.get("a"); set("b", "bb", "bbb"); // size 5 set("c", "cc", "ccc"); // size 5; will evict 'A' cache.flush(); assertThat(value.edit()).isNull(); }
@Test public void editSinceEvictedAndRecreated() throws Exception { cache.close(); cache = DiskLruCache.open(cacheDir, appVersion, 2, 10); set("a", "aa", "aaa"); // size 5 DiskLruCache.Value value = cache.get("a"); set("b", "bb", "bbb"); // size 5 set("c", "cc", "ccc"); // size 5; will evict 'A' set("a", "a", "aaaa"); // size 5; will evict 'B' cache.flush(); assertThat(value.edit()).isNull(); }
private void assertAbsent(String key) throws Exception { DiskLruCache.Value value = cache.get(key); if (value != null) { Assert.fail(); } assertThat(getCleanFile(key, 0)).doesNotExist(); assertThat(getCleanFile(key, 1)).doesNotExist(); assertThat(getDirtyFile(key, 0)).doesNotExist(); assertThat(getDirtyFile(key, 1)).doesNotExist(); }