IgDiskCache { public OptionalStream<SnapshotInputStream> get(String key) { validateKey(key); Entry entry; synchronized (mDiskCacheLock) { entry = mLruEntries.get(key); } if (entry == null || !entry.isReadable()) { mMissCount++; return OptionalStream.absent(); } else { mHitCount++; try { return OptionalStream.of(new SnapshotInputStream(entry)); } catch (IOException e) { return OptionalStream.absent(); } } } IgDiskCache(File directory);  IgDiskCache(File directory, Executor serialExecutor);  IgDiskCache(File directory, long maxSizeInBytes);  IgDiskCache(File directory, long maxSizeInBytes, Executor serialExecutor);  IgDiskCache(File directory, long maxSizeInBytes, int maxCount);  IgDiskCache(File directory, long maxSizeInBytes, int maxCount, Executor serialExecutor); boolean has(String key); OptionalStream<SnapshotInputStream> get(String key); OptionalStream<EditorOutputStream> edit(String key); void remove(String key); void flush(); void close(); File getDirectory(); long getMaxSizeInBytes(); int getMaxCount(); void setMaxSizeInBytes(long maxSizeInBytes); long size(); int count(); final String getHitRateString();  }
@Test public void fileDeletedExternally() throws Exception { set(mCache, "a", "a"); getCleanFile("a").delete(); assertThat(mCache.get("a").isPresent()).isFalse(); }
@Test public void aggressiveClearingHandlesRead() throws Exception { deletePathRecursively(mCacheDir); assertThat(mCache.get("a").isPresent()).isFalse(); }
@Test public void openWithDirtyKeyDeletesAllFilesForThatKey() throws Exception { File cleanFile = getCleanFile("k1"); File dirtyFile = getDirtyFile("k1"); writeFile(cleanFile, "A"); writeFile(dirtyFile, "D"); createJournal("CLEAN k1 1", "DIRTY k1"); mCache = new IgDiskCache(mCacheDir, Integer.MAX_VALUE); assertThat(cleanFile.exists()).isFalse(); assertThat(dirtyFile.exists()).isFalse(); assertThat(mCache.get("k1").isPresent()).isFalse(); }
@Test public void openWithInvalidJournalLineClearsDirectory() throws Exception { generateSomeGarbageFiles(); createJournal("CLEAN k1 1 1", "BOGUS"); mCache = new IgDiskCache(mCacheDir, Integer.MAX_VALUE); assertGarbageFilesAllDeleted(); assertThat(mCache.get("k1").isPresent()).isFalse(); }
@Test public void openWithInvalidFileSizeClearsDirectory() throws Exception { generateSomeGarbageFiles(); createJournal("CLEAN k1 0000x001"); mCache = new IgDiskCache(mCacheDir, Integer.MAX_VALUE); assertGarbageFilesAllDeleted(); assertThat(mCache.get("k1").isPresent()).isFalse(); }
@Test public void openWithTooManyFileSizesClearsDirectory() throws Exception { generateSomeGarbageFiles(); createJournal("CLEAN k1 1 1 1"); mCache = new IgDiskCache(mCacheDir, Integer.MAX_VALUE); assertGarbageFilesAllDeleted(); assertThat(mCache.get("k1").isPresent()).isFalse(); }