open<T extends Object> method

  1. @override
Future<void> open<T extends Object>({
  1. required Duration expiry,
  2. DecodeFunc<T>? fromEncodable,
})

Opens a cache for the specified type with the given configuration.

Implementation

@override
Future<void> open<T extends Object>({
  required Duration expiry,
  DecodeFunc<T>? fromEncodable,
}) async {
  return _openLock.synchronized(() async {
    if (_caches.containsKey(T) && _caches[T]!._open) {
      return;
    }

    fromEncodable ??= Pluggable.storage.getDecoder<T>();

    if (!isPrimitiveType<T>() && fromEncodable == null) {
      throw Exception(
        'Must provide fromEncodable for non-primitive types, or register a decoder before opening',
      );
    }

    await init();

    _caches[T] = _RedisAdapter<T>(
      host: host,
      port: port,
      password: password,
      fromEncodable: fromEncodable,
      expiresIn: expiry,
    );

    await _caches[T]!.open();
  });
}