@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; } }
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; }
@Override public Bitmap get(String key) { DiskLruCache.Snapshot snapshot = null; try { snapshot = mDiskCache.get(key); if (snapshot == null) { return null; } InputStream inputStream = snapshot.getInputStream(0); if (inputStream != null) { BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream, IO_BUFFER_SIZE); return BitmapFactory.decodeStream(bufferedInputStream); } } catch (IOException e) { // no op } finally { if (snapshot != null) { snapshot.close(); } } return null; }
/** * Returns whether the Disk Cache contains the specified URL. You should not call this method * from main/UI thread. * * @param url the URL to search for. * @return {@code true} if the Disk Cache is enabled and contains the specified URL, {@code * false} otherwise. */ public boolean containsInDiskCache(String url) { if (null != mDiskCache) { checkNotOnMainThread(); try { return null != mDiskCache.get(transformUrlForDiskCacheKey(url)); } catch (IOException e) { e.printStackTrace(); } } return false; }
@Override protected boolean doContainsKey(String key) { if (mDiskLruCache == null) { return false; } try { return mDiskLruCache.get(key) != null; } catch (IOException e) { e.printStackTrace(); } return false; }
@Override public long getLength() throws IOException { return cache.get(key).getLength(INPUT_IMAGE); }
@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; } }
private Snapshot getSnapshot(LoaderRequest request) throws IOException { String key = hashKeyForDisk(request); return cache.get(key); }
/** * 根据 key 获取缓存缩略 * * @param key 缓存的key * @return Snapshot */ private DiskLruCache.Snapshot snapshot(String key) { if (mDiskLruCache != null) { try { mSnapshot = mDiskLruCache.get(key); System.out.println(mDiskLruCache); System.out.println(mSnapshot); } catch (IOException e) { e.printStackTrace(); System.out.println(e.getMessage()); } } return mSnapshot; } /*************************
@Override public InputStream getInput() throws IOException { if (snapshot == null) { snapshot = cache.get(key); if (snapshot == null) { throw new IOException("Snapshot not available"); } } // Wrap input stream so we can close the snapshot return new FilterInputStream(snapshot.getInputStream(INPUT_IMAGE)) { @Override public void close() throws IOException { super.close(); snapshot.close(); snapshot = null; } }; } }
public boolean isCacheExists(String key) { DiskLruCache.Snapshot snapshot = null; try { snapshot = diskLruCache.get(cacheKeyProvider.buildCacheKey(key)); } catch (IOException e) { e.printStackTrace(); } if (snapshot == null) { return false; } snapshot.close(); return true; }
@Override public InputStream get(String key) { if (diskLruCache == null || TextUtils.isEmpty(key)) { return null; } try { String cacheKey = cacheKeyProvider.buildCacheKey(key); DiskLruCache.Snapshot snapshot = diskLruCache.get(cacheKey); if (snapshot != null) { return snapshot.getInputStream(0); } } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return null; }
/** * Returns whether the Disk Cache contains the specified URL. You should not call this method * from main/UI thread. * * @param url the URL to search for. * @return {@code true} if the Disk Cache is enabled and contains the specified URL, {@code * false} otherwise. */ public boolean containsInDiskCache(String url) { if (null != mDiskCache) { checkNotOnMainThread(); try { return null != mDiskCache.get(transformUrlForDiskCacheKey(url)); } catch (IOException e) { e.printStackTrace(); } } return false; }
@Override public <T> byte[] getFromCache(Request request) { String cacheKey = urlToKey(request.url().url()); byte[] memoryResponse = (byte[]) memoryCache.get(cacheKey); if(memoryResponse != null){ Log.d("SmartCall", "Memory "); return memoryResponse; } try { DiskLruCache.Snapshot cacheSnapshot = diskCache.get(cacheKey); if(cacheSnapshot != null){ Log.d("SmartCall", "Disk "); return cacheSnapshot.getString(0).getBytes(); }else{ Log.d("SmartCall", "Disk hit!"); return null; } }catch(IOException exc){ return null; } }
@Override public <T> byte[] getFromCache(Request request) { String cacheKey = urlToKey(request.url().url()); byte[] memoryResponse = (byte[]) memoryCache.get(cacheKey); if(memoryResponse != null){ Log.d("SmartCall", "Memory hit!"); return memoryResponse; } try { DiskLruCache.Snapshot cacheSnapshot = diskCache.get(cacheKey); if(cacheSnapshot != null){ Log.d("SmartCall", "Disk hit!"); return cacheSnapshot.getString(0).getBytes(); }else{ return null; } }catch(IOException exc){ return null; } }
@Override public <T> byte[] getFromCache(Request request) { String cacheKey = urlToKey(request.url().url()); byte[] memoryResponse = (byte[]) memoryCache.get(cacheKey); if(memoryResponse != null){ Log.d("SmartCall", "Memory hit!"); return memoryResponse; } try { DiskLruCache.Snapshot cacheSnapshot = diskCache.get(cacheKey); if(cacheSnapshot != null){ Log.d("SmartCall", "Disk hit!"); return cacheSnapshot.getString(0).getBytes(); }else{ return null; } }catch(IOException exc){ return null; } }
private Bitmap getDataFromDiskLruCache(String url) { FileDescriptor fileDescriptor = null; FileInputStream fileInputStream = null; DiskLruCache.Snapshot snapShot = null; try { /* 生成图片对应的 MD5 值 */ final String key = DiskCacheUtil.getMD5String(url); snapShot = mDiskLruCache.get(key); if (null != snapShot) { fileInputStream = (FileInputStream) snapShot.getInputStream(0); fileDescriptor = fileInputStream.getFD(); } /* 将缓存数据解析成 Bitmap 对象 */ Bitmap bitmap = null; if (null != fileDescriptor) { bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor); } return bitmap; } catch (IOException e) { e.printStackTrace(); } finally { CloseUtil.closeQuietly(fileInputStream); } return null; }
/** * Test if an object is present in cache. * @param key is the key of the object. * @return true if the object is present in cache, false otherwise. */ public boolean contains(String key) { if (!ramMode.equals(DualCacheRamMode.DISABLE) && ramCacheLru.snapshot().containsKey(key)) { return true; } try { dualCacheLock.lockDiskEntryWrite(key); if (!diskMode.equals(DualCacheDiskMode.DISABLE) && diskLruCache.get(key) != null) { return true; } } catch (IOException e) { logger.logError(e); } finally { dualCacheLock.unLockDiskEntryWrite(key); } return false; }
/** * Test if an object is present in cache. * @param key is the key of the object. * @return true if the object is present in cache, false otherwise. */ public boolean contains(String key) { if (!ramMode.equals(DualCacheRamMode.DISABLE) && ramCacheLru.snapshot().containsKey(key)) { return true; } try { dualCacheLock.lockDiskEntryWrite(key); if (!diskMode.equals(DualCacheDiskMode.DISABLE) && diskLruCache.get(key) != null) { return true; } } catch (IOException e) { logger.logError(e); } finally { dualCacheLock.unLockDiskEntryWrite(key); } return false; }