editor = mDiskCache.edit( key ); if ( editor == null ) { return; mDiskCache.flush(); editor.commit(); if ( BuildConfig.DEBUG ) { try { snapshot = mDiskCache.get( key ); if ( snapshot == null ) { return null; DiskLruCache.Snapshot snapshot = null; try { snapshot = mDiskCache.get( key ); contained = snapshot != null; } catch (IOException e) { mDiskCache.delete(); } catch ( IOException e ) { e.printStackTrace(); return mDiskCache.getDirectory();
@Override protected DiskLruCache doInBackground(Void... params) { try { return DiskLruCache.open(mDiskCacheLocation, 0, 1, mDiskCacheMaxSize); } catch (IOException e) { e.printStackTrace(); return null; } }
/** * Commits this edit so it is visible to readers. This releases the * edit lock so another edit may be started on the same key. */ public void commit() throws IOException { if (hasErrors) { completeEdit(this, false); remove(entry.key); // The previous entry is stale. } else { completeEdit(this, true); } committed = true; }
backupFile.delete(); } else { renameTo(backupFile, journalFile, false); DiskLruCache cache = new DiskLruCache(directory, appVersion, valueCount, maxSize); if (cache.journalFile.exists()) { try { cache.readJournal(); cache.processJournal(); return cache; } catch (IOException journalIsCorrupt) { + journalIsCorrupt.getMessage() + ", removing"); cache.delete(); cache = new DiskLruCache(directory, appVersion, valueCount, maxSize); cache.rebuildJournal(); return cache;
synchronized (mDiskCacheLock) if(mDiskLruCache == null || mDiskLruCache.isClosed()) DiskLruCache.Snapshot snapshot = mDiskLruCache.get(encryptedKey); final DiskLruCache.Editor editor = mDiskLruCache.edit(encryptedKey); final DiskLruCache.Snapshot snapshot = mDiskLruCache.get(encryptedKey); mDiskLruCache.flush(); if(!mDiskLruCache.isClosed()) mDiskLruCache.close(); mDiskLruCache = null; if(mDiskLruCache != null && !mDiskLruCache.isClosed()) mDiskLruCache.delete();
@Override public InputStream getInputStream() { try { DiskLruCache.Snapshot snapshot = mDiskCache.get(mKey); if (snapshot != null) { return snapshot.getInputStream(0); } } catch (IOException e) { Log.e(Constants.LOG_TAG, "Could open disk cache for url: " + mKey, e); } return null; } }
/** * Returns an editor for the entry named {@code key}, or null if another * edit is in progress. */ public Editor edit(String key) throws IOException { return edit(key, ANY_SEQUENCE_NUMBER); }
@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; }
String key = hashKeyForDisk(data); final int DISK_CACHE_INDEX = 0; long currentMaxSize = cache.getMaxSize(); float percentageSize = Math.round((cache.size() * 100.0f) / currentMaxSize); if (percentageSize >= 90) // cache size reaches 90% cache.setMaxSize(currentMaxSize + (10 * 1024 * 1024)); // increase size to 10MB try { DiskLruCache.Snapshot snapshot = cache.get(key); if (snapshot == null) { Log.i(getTag(), "Snapshot is not available downloading..."); DiskLruCache.Editor editor = cache.edit(key); if (editor != null) { if (downloadUrlToStream(data, editor.newOutputStream(DISK_CACHE_INDEX))) editor.abort(); snapshot = cache.get(key); } else Log.i(getTag(), "Snapshot found sending");
public DiskCacheManager(Context context, String uniqueName) { try { //先关闭已有的缓存 if (mDiskLruCache != null) { mDiskLruCache.close(); mDiskLruCache = null; } File cacheFile = getCacheFile(context, uniqueName); mDiskLruCache = DiskLruCache.open(cacheFile, MyApplication.getAppVersion(), 1, Constants.CACHE_MAXSIZE); } catch (IOException e) { e.printStackTrace(); } }
public void clear() { try { mDiskCache.delete(); } catch (IOException e) { // no op } }
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(); } } }
/** * Closes the cache and deletes all of its stored values. This will delete * all files in the cache directory including files that weren't created by * the cache. */ public void delete() throws IOException { close(); Util.deleteContents(directory); }
if (mDiskLruCache == null || mDiskLruCache.isClosed()) { File diskCacheDir = mCacheParams.diskCacheDir; if (mCacheParams.diskCacheEnabled && diskCacheDir != null) { OutputStream out = null; try { DiskLruCache.Snapshot snapshot = mDiskLruCache.get(key); if (snapshot == null) { final DiskLruCache.Editor editor = mDiskLruCache.edit(key); if (editor != null) { out = editor.newOutputStream(DISK_CACHE_INDEX); InputStream inputStream = null; try { final DiskLruCache.Snapshot snapshot = mDiskLruCache.get(key); if (snapshot != null) { if (BuildConfig.DEBUG) { if (mDiskLruCache != null && !mDiskLruCache.isClosed()) { try { mDiskLruCache.delete(); if (BuildConfig.DEBUG) { Log.d(TAG, "Disk cache cleared"); if (mDiskLruCache != null) { try { mDiskLruCache.flush(); if (BuildConfig.DEBUG) { Log.d(TAG, "Disk cache flushed");
private boolean contains(String key) { boolean contained = false; DiskLruCache.Snapshot snapshot = null; try { snapshot = mDiskCache.get(key); contained = snapshot != null; } catch (IOException e) { // no op } finally { if (snapshot != null) { snapshot.close(); } } return contained; }
DiskLruCache.Editor editor = mDiskCache.edit(key); os = editor.newOutputStream(0); bitmap.compress(compressFormat, compressQuality, os);
@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(); } } }