operator [] method

Future<V> operator [](
  1. K key
)

Retrieves a value from the cache by key.

If the value is not in the cache, it will be loaded from the backing store using the provided loader function.

Implementation

Future<V> operator [](K key) async {
  // Check if the key exists in cache
  final entry = await _storage.get(key);

  if (entry != null) {
    // Update access metadata
    entry.updateAccess();
    await _storage.put(key, entry);
    return entry.value;
  }

  // Load from backing store
  final value = await _loader(key);

  // Create new cache entry
  final newEntry = CacheEntry<K, V>(
    key: key,
    value: value,
  );

  // Check if we need to evict an item
  final currentSize = await _storage.size;
  if (currentSize >= _maxSize) {
    await _evictItem();
  }

  // Store the new entry
  await _storage.put(key, newEntry);

  return value;
}