示例#1
0
 @Override
 public void clear() {
   try {
     cache.delete();
   } catch (IOException e) {
     L.e(e);
   } finally {
     initCache(cache.getDirectory(), reserveCacheDir, cache.getMaxSize(), cache.getMaxFileCount());
   }
 }
示例#2
0
  /**
   * Opens the cache in {@code directory}, creating a cache if none exists there.
   *
   * @param directory a writable directory
   * @param valueCount the number of values per cache entry. Must be positive.
   * @param maxSize the maximum number of bytes this cache should use to store
   * @param maxFileCount the maximum file count this cache should store
   * @throws java.io.IOException if reading or writing the cache directory fails
   */
  public static DiskLruCache open(
      File directory, int appVersion, int valueCount, long maxSize, int maxFileCount)
      throws IOException {
    if (maxSize <= 0) {
      throw new IllegalArgumentException("maxSize <= 0");
    }
    if (maxFileCount <= 0) {
      throw new IllegalArgumentException("maxFileCount <= 0");
    }
    if (valueCount <= 0) {
      throw new IllegalArgumentException("valueCount <= 0");
    }

    // If a bkp file exists, use it instead.
    File backupFile = new File(directory, JOURNAL_FILE_BACKUP);
    if (backupFile.exists()) {
      File journalFile = new File(directory, JOURNAL_FILE);
      // If journal file also exists just delete backup file.
      if (journalFile.exists()) {
        backupFile.delete();
      } else {
        renameTo(backupFile, journalFile, false);
      }
    }

    // Prefer to pick up where we left off.
    DiskLruCache cache = new DiskLruCache(directory, appVersion, valueCount, maxSize, maxFileCount);
    if (cache.journalFile.exists()) {
      try {
        cache.readJournal();
        cache.processJournal();
        cache.journalWriter =
            new BufferedWriter(
                new OutputStreamWriter(
                    new FileOutputStream(cache.journalFile, true), Util.US_ASCII));
        return cache;
      } catch (IOException journalIsCorrupt) {
        System.out.println(
            "DiskLruCache "
                + directory
                + " is corrupt: "
                + journalIsCorrupt.getMessage()
                + ", removing");
        cache.delete();
      }
    }

    // Create a new empty cache.
    directory.mkdirs();
    cache = new DiskLruCache(directory, appVersion, valueCount, maxSize, maxFileCount);
    cache.rebuildJournal();
    return cache;
  }