getCache<T> method

Cache<T> getCache<T>(
  1. String identifier,
  2. CacheConfig<T> config
)

Get the cache associated with identifier, or create it if it does not yet exist.

config is only used if the cache does not exist and needs to be created.

Implementation

Cache<T> getCache<T>(String identifier, CacheConfig<T> config) {
  if (_caches[identifier] case final cache?) {
    if (cache is Cache<T>) {
      return cache;
    }

    throw ArgumentError(
        'Type of cache (${cache.runtimeType}) does not match type argument ($T) for $identifier');
  }

  if (_emptyCaches[identifier] case final reference?) {
    if (reference.target case final cache?) {
      if (cache is Cache<T>) {
        return cache;
      }

      throw ArgumentError(
          'Type of cache (${cache.runtimeType}) does not match type argument ($T) for $identifier');
    } else {
      _emptyCaches.remove(identifier);
    }
  }

  final cache = Cache._(this, identifier, config);
  _onEmpty(cache); // Cache starts out empty. Don't be afraid to discard it.
  return cache;
}