mDiskCache.flush(); editor.commit(); if ( BuildConfig.DEBUG ) {
public void run() { // Make sure we're running with a background priority Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); if (Constants.DEBUG) { Log.d(Constants.LOG_TAG, "Flushing Disk Cache"); } try { mDiskCache.flush(); } catch (IOException e) { e.printStackTrace(); } } }
@Override public Bitmap put(String key, Bitmap data) { if (contains(key)) { return data; } DiskLruCache.Editor editor = null; try { editor = mDiskCache.edit(key); if (editor != null) { if (writeBitmapToCache(data, editor)) { mDiskCache.flush(); editor.commit(); } else { editor.abort(); } } } catch (IOException e) { try { if (editor != null) { editor.abort(); } } catch (IOException ignored) { // } } return data; }
/** * Force buffered operations to the file system. * * @throws IOException */ public synchronized final void flush() throws IOException { if (mDiskCache != null) { mDiskCache.flush(); } }
/** * 同步记录文件 */ public void flush() { if (mDiskLruCache != null) { try { mDiskLruCache.flush(); } catch (IOException e) { e.printStackTrace(); } } } }
public void run() { // Make sure we're running with a background priority Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); if (Constants.DEBUG) { Log.d(Constants.LOG_TAG, "Flushing Disk Cache"); } try { mDiskCache.flush(); } catch (IOException e) { e.printStackTrace(); } } }
@Override public void put(String key, File value, CacheLoadFinishListener listener) { if (diskLruCache == null || value == null || TextUtils.isEmpty(key)) { return; } DiskLruCache.Editor editor = null; try { String cacheKey = cacheKeyProvider.buildCacheKey(key); editor = diskLruCache.edit(cacheKey); if (editor != null) { OutputStream os = editor.newOutputStream(0); if (writeCache(value, os) && listener != null) { listener.onFinished(); } editor.commit(); } diskLruCache.flush(); } catch (IOException e) { try { if (editor != null) { editor.abort(); } } catch (IOException e1) { e1.printStackTrace(); } } }
mDiskCache.flush(); editor.commit(); if (BuildConfig.DEBUG) {
mDiskLruCache.flush();
private void putDataToDiskLruCache(Image image) { try { /* 第一步:获取将要缓存的图片的对应唯一 key 值 */ String key = DiskCacheUtil.getMD5String(image.getUrl()); /* 第二步:获取 DiskLruCache 的 Editor */ DiskLruCache.Editor editor = mDiskLruCache.edit(key); if (null != editor) { /* 第三步:从 Editor 中获取 OutputStream */ OutputStream outputStream = editor.newOutputStream(0); /* 第四步:下载网络图片且保存至 DiskLruCache 图片中 */ boolean isSuccessful = download(image.getUrl(), outputStream); if (isSuccessful) { editor.commit(); } else { editor.abort(); } mDiskLruCache.flush(); } } catch (IOException e) { e.printStackTrace(); } }
/** * 下载图片放入本地缓存 * @param urlString 下载图片的链接 * @throws IOException */ public void downloadImageToDiskCache(String urlString) throws IOException { if(mDiskLruCache == null){ return ; } String key =MD5Utils.hashKeyFromUrl(urlString); DiskLruCache.Editor editor =mDiskLruCache.edit(key); if(editor != null){ //打开本地缓存的输入流 OutputStream outputStream =editor.newOutputStream(DISK_CACHE_INDEX); //将从网络下载并写入输出流中 if(NetRequest.downloadUrlToStream(urlString,outputStream)){ //提交数据,并是释放连接 editor.commit(); }else{ //释放连接 editor.abort(); } mDiskLruCache.flush(); } }
if (mDiskLruCache != null) { try { mDiskLruCache.flush(); if (BuildConfig.DEBUG) { Log.d(TAG, "Disk cache flushed");