open static method

Future<DiskLruCache> open(
  1. Directory directory, {
  2. String version = "1.0.0",
  3. int valueCount = 1,
  4. int maxSize = 20 * 1024 * 1024,
})

检查是否存在缓存目录,没有就创建

Implementation

static Future<DiskLruCache> open(
  Directory directory, {
  String version = "1.0.0",
  int valueCount = 1,
  int maxSize = 20 * 1024 * 1024,
}) async {
  if (maxSize <= 0) throw Exception("maxSize <= 0");
  if (valueCount <= 0) throw Exception("valueCount <= 0");

  if (!await directory.exists()) {
    await directory.create();
  }

  File backupFile = File("${directory.path}/$JOURNAL_FILE_BACKUP");
  if (await backupFile.exists()) {
    File journalFile = File("${directory.path}/$JOURNAL_FILE");
    await journalFile.exists() ? await backupFile.delete() : await backupFile.rename(journalFile.path);
  }

  DiskLruCache cache = new DiskLruCache._(directory, version, valueCount, maxSize);
  if (await cache.journalFile.exists()) {
    try {
      await cache._readJournal();
      await cache._processJournal();
      return cache;
    } catch (e) {
      await cache.delete();
    }
  }
  await cache._rebuildJournal();
  return cache;
}