public CacheItem put(final String url, final Object obj, DateTime expiresAt) { if (obj == null) { return null; } Timber.d(String.format("put(%s)", url)); CacheItem d = new CacheItem(obj, expiresAt); if (null != mMemoryCache) { mMemoryCache.put(url, d); } if (null != mDiskCache) { checkNotOnMainThread(); final String key = transformUrlForDiskCacheKey(url); final ReentrantLock lock = getLockForDiskCacheEdit(key); lock.lock(); try { DiskLruCache.Editor editor = mDiskCache.edit(key); writeValueToDisk(editor.newOutputStream(0), obj); writeExpirationToDisk(editor.newOutputStream(1), expiresAt); editor.commit(); } catch (IOException e) { e.printStackTrace(); } finally { lock.unlock(); scheduleDiskCacheFlush(); } } return d; }
public void put(String key, Bitmap image) { if (key == null || image == null) { return; } addBitmapToMemoryCache(key, image); DiskLruCache.Editor editor = null; try { editor = _diskCache.edit(key); if (editor == null || image == null) { return; } if (writeBitmapToFile(image, editor)) { _diskCache.flush(); editor.commit(); } else { editor.abort(); } } catch (IOException e) { try { if (editor != null) { editor.abort(); } } catch (IOException ignored) { } } }
private boolean writeBitmapToDisk(InputStream is, String key) { OutputStream os = null; try { DiskLruCache.Editor editor = getDiskEditor(key); if (editor != null) { os = editor.newOutputStream(0); if (IoUtils.copy(is, os)) { editor.commit(); return true; } } } catch (Exception e) { e.printStackTrace(); } finally { IoUtils.closeStream(os); } return false; }
private boolean writeBitmapToFile(Bitmap bitmap, DiskLruCache.Editor editor) throws IOException, FileNotFoundException { OutputStream out = null; try { out = new BufferedOutputStream(editor.newOutputStream(0), Utilities.IO_BUFFER_SIZE); return bitmap.compress(CompressFormat.JPEG, 70, out); } finally { if (out != null) { out.close(); } } }
private boolean putVal(KeyEntry key, ValueEntry value) { if (value.getValue() == null) { return false; } DiskLruCache.Editor editor = getDiskEditor(key.getKey()); if (editor != null) { OutputStream os = null; try { os = editor.newOutputStream(0); if (value.getValue().compress(CompressFormat.JPEG, 100, os)) { editor.commit(); return true; } } catch (IOException e) { e.printStackTrace(); } finally { IoUtils.closeStream(os); } } return false; }