@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; }
public void put(Key key) { String encodedKey = keyGenerator.getKey(key); if (encodedKey == null) { return; } lruCache.put(encodedKey, NULL_VALUE); try { synchronized (DISK_CACHE_LOCK) { DiskLruCache.Editor editor = diskLruCache.edit(encodedKey); // Editor will be null if there are two concurrent puts. In the worst case we will just silently fail. if (editor != null) { try { if (editor.getFile(0).createNewFile()) { editor.commit(); } } finally { editor.abortUnlessCommitted(); } } } } catch (IOException ignore) { } }
@Nullable public String get(@Nullable String key) { if (TextUtils.isEmpty(key)) { return null; } String encodedKey = keyGenerator.getKey(key); String result; result = lruCache.get(encodedKey); if (result == null) { try { synchronized (DISK_CACHE_LOCK) { DiskLruCache.Value value = diskLruCache.get(encodedKey); if (value != null) { result = value.getString(0); } } } catch (IOException ignore) { result = null; } } return result; }
@Override public void put(Key key, Writer writer) { // We want to make sure that puts block so that data is available when put completes. We may // actually not write any data if we find that data is written by the time we acquire the lock. writeLocker.acquire(key); try { String safeKey = safeKeyGenerator.getSafeKey(key); if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "Put: Obtained: " + safeKey + " for for Key: " + key); } try { // We assume we only need to put once, so if data was written while we were trying to get // the lock, we can simply abort. DiskLruCache diskCache = getDiskCache(); Value current = diskCache.get(safeKey); if (current != null) { return; } DiskLruCache.Editor editor = diskCache.edit(safeKey); if (editor == null) { throw new IllegalStateException("Had two simultaneous puts for: " + safeKey); } 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); } }
@Override public void put(Key key, Writer writer) { // We want to make sure that puts block so that data is available when put completes. We may // actually not write any data if we find that data is written by the time we acquire the lock. String safeKey = safeKeyGenerator.getSafeKey(key); writeLocker.acquire(safeKey); try { if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "Put: Obtained: " + safeKey + " for for Key: " + key); } try { // We assume we only need to put once, so if data was written while we were trying to get // the lock, we can simply abort. DiskLruCache diskCache = getDiskCache(); Value current = diskCache.get(safeKey); if (current != null) { return; } DiskLruCache.Editor editor = diskCache.edit(safeKey); if (editor == null) { throw new IllegalStateException("Had two simultaneous puts for: " + safeKey); } 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(safeKey); } }
public static DiskLruCache getLruCache(Context context) { long DISK_CACHE_SIZE = 1024 * 1024 * 50; File disCacheDir = new File(context.getCacheDir(), "bitmap"); if (!disCacheDir.exists()) { disCacheDir.mkdirs(); } try { DiskLruCache cache = DiskLruCache.open(disCacheDir, 1, 1, DISK_CACHE_SIZE); return cache; } catch (IOException e) { e.printStackTrace(); } return null; }
/** * 获取磁盘中图片流 */ public static OutputStream getStreamFromDis(String url, Context context) { String key = hashKeyFromUrl(url); try { DiskLruCache.Editor editor = getLruCache(context).edit(key); if (editor != null) { // OutputStream outputStream = editor. } } catch (IOException e) { e.printStackTrace(); } return null; }
public AvatarUrlsCache() { lruCache = new LruCache<>(MEMORY_CACHE_MAX_NUMBER); File file = new File(App.get().getCacheDir().getPath() + File.separator + DISK_CACHE_DIRECTORY); try { diskLruCache = DiskLruCache.open(file, BuildConfig.VERSION_CODE, 1, DISK_CACHE_MAX_SIZE); } catch (IOException e) { throw new RuntimeException("Failed to open the cache in " + file + ".", e); } keyGenerator = new KeyGenerator(); }
public EditorDiskCache() { lruCache = new LruCache<>(MEMORY_CACHE_MAX_NUMBER); File file = new File(App.get().getCacheDir().getPath() + File.separator + DISK_CACHE_DIRECTORY); try { diskLruCache = DiskLruCache.open(file, BuildConfig.VERSION_CODE, 1, DISK_CACHE_MAX_SIZE); } catch (IOException e) { throw new RuntimeException("Failed to open the cache in " + file + ".", e); } keyGenerator = new KeyGenerator(); }
private synchronized DiskLruCache getDiskCache() throws IOException { if (diskLruCache == null) { diskLruCache = DiskLruCache.open(directory, APP_VERSION, VALUE_COUNT, maxSize); } return diskLruCache; }
/** * if value is empty, then remove cache */ public void put(@Nullable String key, @Nullable String value) { if (TextUtils.isEmpty(key)) { return; } if (TextUtils.isEmpty(value)) { remove(key); return; } String encodedKey = keyGenerator.getKey(key); lruCache.put(encodedKey, value); try { synchronized (DISK_CACHE_LOCK) { DiskLruCache.Editor editor = diskLruCache.edit(encodedKey); // Editor will be null if there are two concurrent puts. In the worst case we will just silently fail. if (editor != null) { BufferedWriter writer = null; try { File file = editor.getFile(0); writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")); writer.write(value); editor.commit(); } finally { editor.abortUnlessCommitted(); if (writer != null) { try { writer.close(); } catch (IOException e) { L.report(e); } } } } } } catch (IOException ignore) { } }