init method

Future<void> init()

Initialize ffcache.

This method is called internally from set/get/remove methods if init() was not called.

Implementation

Future<void> init() async {
  if (_initialized) return;
  if (kIsWeb) {
    _basePath = _name;

    if (_debug) {
      print("FFCache: use idb_shim!");
    }
    final idbFactory = getIdbFactory();
    // open the database
    _db = await idbFactory!.open("$_basePath.db", version: 1,
        onUpgradeNeeded: (VersionChangeEvent event) {
      Database db = event.database;
      // db.createObjectStore(storeName, autoIncrement: true);
      db.createObjectStore(_basePath);
    });

    // load _timeoutMap
    try {
      final val = await _readString(_ffcache_filename);
      if (val != null) {
        final data = json.decode(val);

        for (final k in data.keys) {
          _timeoutMap[k] = data[k];
        }
      }
    } catch (_) {}

    // delete old entries (not tested)
    Map<String, int> _newTimeoutMap = {};

    if (_db != null)
      try {
        var txn = _db!.transaction(_basePath, idbModeReadOnly);
        var store = txn.objectStore(_basePath);
        var keys = await store.getAllKeys();
        await txn.completed;

        for (final keyObject in keys) {
          final key = keyObject as String;
          if (key == _ffcache_filename) {
            continue;
          }
          if (remainingDurationForKey(key).isNegative) {
            if (_debug) {
              print('  $key : delete');
            }
            var txn2 = _db!.transaction(_basePath, idbModeReadWrite);
            var store2 = txn.objectStore(_basePath);
            await store2.delete(key);
            await txn2.completed;
          } else {
            if (_debug) {
              print('  $key : cache ok');
            }
            final val = _timeoutMap[key];
            if (val != null) {
              _newTimeoutMap[key] = val;
            }
          }
        }
      } catch (_) {}

    _timeoutMap = _newTimeoutMap;
    await _saveMap();
  } else {
    final tempDir = await getTemporaryDirectory();
    _basePath = tempDir.path + '/$_name';

    await Directory(_basePath).create(recursive: true);

    if (_debug) {
      print("FFCache path: $_basePath");
    }

    try {
      final data = json
          .decode(await File('$_basePath/$_ffcache_filename').readAsString());

      for (final k in data.keys) {
        _timeoutMap[k] = data[k];
      }
    } catch (_) {}

    Map<String, int> _newTimeoutMap = {};

    try {
      final files =
          Directory(_basePath).listSync(recursive: false, followLinks: false);
      for (final entity in files) {
        final filename = entity.path.split('/').last;
        if (filename == _ffcache_filename) {
          continue;
        }
        if (remainingDurationForKey(filename).isNegative) {
          if (_debug) {
            print('  $filename : delete');
          }
          await entity.delete(recursive: false);
        } else {
          if (_debug) {
            print('  $filename : cache ok');
          }
          final val = _timeoutMap[filename];
          if (val != null) {
            _newTimeoutMap[filename] = val;
          }
        }
      }
    } catch (_) {}

    _timeoutMap = _newTimeoutMap;
    await _saveMap();
  }

  _initialized = true;
}