@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; }
/** * Returns a snapshot of the entry named {@code key}, or null if it doesn't * exist is not currently readable. If a value is returned, it is moved to * the head of the LRU queue. */ public synchronized Value get(String key) throws IOException { checkNotClosed(); Entry entry = lruEntries.get(key); if (entry == null) { return null; } if (!entry.readable) { return null; } for (File file : entry.cleanFiles) { // A file must have been deleted manually! if (!file.exists()) { return null; } } redundantOpCount++; journalWriter.append(READ); journalWriter.append(' '); journalWriter.append(key); journalWriter.append('\n'); if (journalRebuildRequired()) { executorService.submit(cleanupCallable); } return new Value(key, entry.sequenceNumber, entry.cleanFiles, entry.lengths); }
/** * Returns a snapshot of the entry named {@code key}, or null if it doesn't * exist is not currently readable. If a value is returned, it is moved to * the head of the LRU queue. */ public synchronized Value get(String key) throws IOException { checkNotClosed(); Entry entry = lruEntries.get(key); if (entry == null) { return null; } if (!entry.readable) { return null; } for (File file : entry.cleanFiles) { // A file must have been deleted manually! if (!file.exists()) { return null; } } redundantOpCount++; journalWriter.append(READ); journalWriter.append(' '); journalWriter.append(key); journalWriter.append('\n'); if (journalRebuildRequired()) { executorService.submit(cleanupCallable); } return new Value(key, entry.sequenceNumber, entry.cleanFiles, entry.lengths); }
/** * Returns a snapshot of the entry named {@code key}, or null if it doesn't * exist is not currently readable. If a value is returned, it is moved to * the head of the LRU queue. */ public synchronized Value get(String key) throws IOException { checkNotClosed(); Entry entry = lruEntries.get(key); if (entry == null) { return null; } if (!entry.readable) { return null; } for (File file : entry.cleanFiles) { // A file must have been deleted manually! if (!file.exists()) { return null; } } redundantOpCount++; journalWriter.append(READ); journalWriter.append(' '); journalWriter.append(key); journalWriter.append('\n'); if (journalRebuildRequired()) { executorService.submit(cleanupCallable); } return new Value(key, entry.sequenceNumber, entry.cleanFiles, entry.lengths); }
@Test public void journalFileIsPreferredOverBackupFile() throws Exception { DiskLruCache.Editor creator = cache.edit("k1"); creator.set(0, "ABC"); creator.set(1, "DE"); creator.commit(); cache.flush(); FileUtils.copyFile(journalFile, journalBkpFile); creator = cache.edit("k2"); creator.set(0, "F"); creator.set(1, "GH"); creator.commit(); cache.close(); assertThat(journalFile.exists()).isTrue(); assertThat(journalBkpFile.exists()).isTrue(); cache = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE); DiskLruCache.Value valueA = cache.get("k1"); assertThat(valueA.getString(0)).isEqualTo("ABC"); assertThat(valueA.getLength(0)).isEqualTo(3); assertThat(valueA.getString(1)).isEqualTo("DE"); assertThat(valueA.getLength(1)).isEqualTo(2); DiskLruCache.Value valueB = cache.get("k2"); assertThat(valueB.getString(0)).isEqualTo("F"); assertThat(valueB.getLength(0)).isEqualTo(1); assertThat(valueB.getString(1)).isEqualTo("GH"); assertThat(valueB.getLength(1)).isEqualTo(2); assertThat(journalBkpFile.exists()).isFalse(); assertThat(journalFile.exists()).isTrue(); }
private Bitmap getBitmap(DiskLruCache.Value value) { Bitmap bitmap = null; final File file = value.getFile(BITMAP_INDEX); if (file != null) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.ARGB_8888; FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(file); bitmap = BitmapFactory.decodeStream(fileInputStream, null, options); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } return bitmap; }
@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; }
/** * 获取是否有某张原图的缓存 * 缓存模式必须是:DiskCacheStrategy.SOURCE 才能获取到缓存文件 */ public static File getGlideCacheFile(Context context, String url) { try { OriginalKey originalKey = new OriginalKey(url, EmptySignature.obtain()); SafeKeyGenerator safeKeyGenerator = new SafeKeyGenerator(); String safeKey = safeKeyGenerator.getSafeKey(originalKey); File file = new File(context.getCacheDir(), DiskCache.Factory.DEFAULT_DISK_CACHE_DIR); DiskLruCache diskLruCache = DiskLruCache.open(file, 1, 1, DiskCache.Factory.DEFAULT_DISK_CACHE_SIZE); DiskLruCache.Value value = diskLruCache.get(safeKey); if (value != null) { return value.getFile(0); } } catch (Exception e) { e.printStackTrace(); } return null; }
@Test public void restoreBackupFile() throws Exception { DiskLruCache.Editor creator = cache.edit("k1"); creator.set(0, "ABC"); creator.set(1, "DE"); creator.commit(); cache.close(); assertThat(journalFile.renameTo(journalBkpFile)).isTrue(); assertThat(journalFile.exists()).isFalse(); cache = DiskLruCache.open(cacheDir, appVersion, 2, Integer.MAX_VALUE); 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); assertThat(journalBkpFile.exists()).isFalse(); assertThat(journalFile.exists()).isTrue(); }
@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 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 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 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); }
/** @see <a href="https://github.com/JakeWharton/DiskLruCache/issues/2">Issue #2</a> */ @Test public void aggressiveClearingHandlesPartialEdit() throws Exception { set("a", "a", "a"); set("b", "b", "b"); DiskLruCache.Editor a = cache.get("a").edit(); a.set(0, "a1"); FileUtils.deleteDirectory(cacheDir); a.set(1, "a2"); a.commit(); assertThat(cache.get("a")).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(); }
@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 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(); }
/** @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(); }