checkAndEvict method

Future<bool> checkAndEvict()

Checks if the cache needs eviction and performs it if necessary.

Returns true if eviction was performed, false otherwise.

Implementation

Future<bool> checkAndEvict() async {
  if (_maxSize == null && _maxItems == null) {
    return false; // No limits set, no eviction needed
  }

  bool needsEviction = false;

  // Check if we're over the size limit
  if (_maxSize != null && _analytics.totalSize > _maxSize) {
    _log.info(
        'Cache size (${_analytics.totalSize}) exceeds maximum size ($_maxSize). Evicting items...');
    needsEviction = true;
  }

  // Check if we're over the item count limit
  if (_maxItems != null) {
    final keys = await _cacheAdapter.getKeys();
    if (keys.length > _maxItems) {
      _log.info(
          'Cache item count (${keys.length}) exceeds maximum count ($_maxItems). Evicting items...');
      needsEviction = true;
    }
  }

  if (needsEviction) {
    return await _evictItems();
  }

  return false;
}