@Override public DiskCache build() { File cacheDir = cacheDirectoryGetter.getCacheDirectory(); if (cacheDir == null) { return null; } if (!cacheDir.mkdirs() && (!cacheDir.exists() || !cacheDir.isDirectory())) { return null; } return DiskLruCacheWrapper.create(cacheDir, diskCacheSize); } }
/** * Create a new DiskCache in the given directory with a specified max size. * * @param directory The directory for the disk cache * @param maxSize The max size for the disk cache * @return The new disk cache with the given arguments */ @SuppressWarnings("deprecation") public static DiskCache create(File directory, long maxSize) { return new DiskLruCacheWrapper(directory, maxSize); }
@Override public synchronized void clear() { try { getDiskCache().delete(); } catch (IOException e) { if (Log.isLoggable(TAG, Log.WARN)) { Log.w(TAG, "Unable to clear disk cache or disk cache cleared externally", e); } } finally { // Delete can close the cache but still throw. If we don't null out the disk cache here, every // subsequent request will try to act on a closed disk cache and fail. By nulling out the disk // cache we at least allow for attempts to open the cache in the future. See #2465. resetDiskCache(); } }
@Override public void delete(Key key) { String safeKey = safeKeyGenerator.getSafeKey(key); try { getDiskCache().remove(safeKey); } catch (IOException e) { if (Log.isLoggable(TAG, Log.WARN)) { Log.w(TAG, "Unable to delete from disk cache", e); } } }
@Override public DiskCache build() { File cacheDir = cacheDirectoryGetter.getCacheDirectory(); if (cacheDir == null) { return null; } if (!cacheDir.mkdirs() && (!cacheDir.exists() || !cacheDir.isDirectory())) { return null; } return DiskLruCacheWrapper.get(cacheDir, diskCacheSize); }
@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; }
@Override public DiskCache build() { // Careful: the external cache directory doesn't enforce permissions File cacheFile = new File(App.getApplication().getCacheDir(), "images"); cacheFile.mkdirs(); //104857600 == 100M return DiskLruCacheWrapper.get(cacheFile, 104857600); } });
@Override public DiskCache build() { // Careful: the external cache directory doesn't enforce permissions return DiskLruCacheWrapper.create(DataHelper.makeDirs(new File(appComponent.cacheFile(), "Glide")), IMAGE_DISK_CACHE_MAX_SIZE); } });
@Override public synchronized void clear() { try { getDiskCache().delete(); resetDiskCache(); } catch (IOException e) { if (Log.isLoggable(TAG, Log.WARN)) { Log.w(TAG, "Unable to clear disk cache", e); } } } }
DiskLruCache diskCache = getDiskCache(); Value current = diskCache.get(safeKey); if (current != null) {
/** * Get a DiskCache in the given directory and size. If a disk cache has already been created with * a different directory and/or size, it will be returned instead and the new arguments will be * ignored. * * @param directory The directory for the disk cache * @param maxSize The max size for the disk cache * @return The new disk cache with the given arguments, or the current cache if one already exists * * @deprecated Use {@link #create(File, long)} to create a new cache with the specified arguments. */ @SuppressWarnings("deprecation") @Deprecated public static synchronized DiskCache get(File directory, long maxSize) { // TODO calling twice with different arguments makes it return the cache for the same // directory, it's public! if (wrapper == null) { wrapper = new DiskLruCacheWrapper(directory, maxSize); } return wrapper; }
@Override public DiskCache build() { File cacheDir = cacheDirectoryGetter.getCacheDirectory(); if (cacheDir == null) { return null; } if (!cacheDir.mkdirs() && (!cacheDir.exists() || !cacheDir.isDirectory())) { return null; } return DiskLruCacheWrapper.get(cacheDir, diskCacheSize); } }
@Before public void setUp() { dir = RuntimeEnvironment.application.getCacheDir(); cache = DiskLruCacheWrapper.create(dir, 10 * 1024 * 1024); key = new ObjectKey("test" + Math.random()); data = new byte[] { 1, 2, 3, 4, 5, 6 }; }
@Override public synchronized void clear() { try { getDiskCache().delete(); resetDiskCache(); } catch (IOException e) { if (Log.isLoggable(TAG, Log.WARN)) { Log.w(TAG, "Unable to clear disk cache", e); } } }
@Override public void delete(Key key) { String safeKey = safeKeyGenerator.getSafeKey(key); try { getDiskCache().remove(safeKey); } catch (IOException e) { if (Log.isLoggable(TAG, Log.WARN)) { Log.w(TAG, "Unable to delete from disk cache", e); } } }
/** * Get a DiskCache in the given directory and size. If a disk cache has alread been created with * a different directory and/or size, it will be returned instead and the new arguments * will be ignored. * * @param directory The directory for the disk cache * @param maxSize The max size for the disk cache * @return The new disk cache with the given arguments, or the current cache if one already exists */ public static synchronized DiskCache get(File directory, int maxSize) { // TODO calling twice with different arguments makes it return the cache for the same directory, it's public! if (wrapper == null) { wrapper = new DiskLruCacheWrapper(directory, maxSize); } return wrapper; }
@Test public void clearDiskCache_afterOpeningDiskCache_andDeleteDirectoryOutsideGlide_doesNotThrow() { DiskCache cache = DiskLruCacheWrapper.create(cacheDir, 1024 * 1024); cache.get(mock(Key.class)); deleteRecursively(cacheDir); cache.clear(); }
@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; }
/** * Get a DiskCache in the given directory and size. If a disk cache has already been created with * a different directory and/or size, it will be returned instead and the new arguments will be * ignored. * * @param directory The directory for the disk cache * @param maxSize The max size for the disk cache * @return The new disk cache with the given arguments, or the current cache if one already exists */ public static synchronized DiskCache get(File directory, int maxSize) { // TODO calling twice with different arguments makes it return the cache for the same // directory, it's public! if (wrapper == null) { wrapper = new DiskLruCacheWrapper(directory, maxSize); } return wrapper; }
@Test public void get_afterDeleteDirectoryOutsideGlideAndClose_doesNotThrow() { DiskCache cache = DiskLruCacheWrapper.create(cacheDir, 1024 * 1024); cache.get(mock(Key.class)); deleteRecursively(cacheDir); cache.clear(); cache.get(mock(Key.class)); }