Exemplo n.º 1
0
  public CacheItem getFromDiskCache(final String url, boolean checkExpiration) {
    CacheItem result = null;

    if (null != mDiskCache) {
      checkNotOnMainThread();

      try {
        final String key = transformUrlForDiskCacheKey(url);
        DiskLruCache.Snapshot snapshot = mDiskCache.get(key);
        if (null != snapshot) {

          Object value = readValueFromDisk(snapshot.getInputStream(0));
          DateTime expiresAt = new DateTime(readExpirationFromDisk(snapshot.getInputStream(1)));

          if (value != null) {

            if (checkExpiration && expiresAt.isBeforeNow()) {
              mDiskCache.remove(key);
              scheduleDiskCacheFlush();
            } else {
              result = new CacheItem(value, expiresAt);
              if (null != mMemoryCache) {
                mMemoryCache.put(url, result);
              }
            }
          } else {
            // If we get here, the file in the cache can't be
            // decoded. Remove it and schedule a flush.
            mDiskCache.remove(key);
            scheduleDiskCacheFlush();
          }
        }
      } catch (IOException e) {
        Timber.e(e, "getFromDiskCache failed.");
      }
    }

    return result;
  }
Exemplo n.º 2
0
  public void remove(String url) {
    if (null != mMemoryCache) {
      mMemoryCache.remove(url);
    }

    if (null != mDiskCache) {
      checkNotOnMainThread();

      try {
        mDiskCache.remove(transformUrlForDiskCacheKey(url));
        scheduleDiskCacheFlush();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }