garbageCollect static method

int garbageCollect()

Removes all expired entries from cache memory to reclaim resources.

Returns the number of entries evicted.

Example:

final evictedCount = BloomData.garbageCollect();

Implementation

static int garbageCollect() {
  final expiredKeys = <String>[];
  for (final entryKey in _cache.keys) {
    final entry = _cache[entryKey]!;
    if (entry.isExpired && (_listenerCounts[entryKey] ?? 0) == 0) {
      expiredKeys.add(entryKey);
    }
  }
  for (final k in expiredKeys) {
    _cache.remove(k);
    _disposeController(k);
  }
  if (expiredKeys.isNotEmpty) {
    logger.debug('BloomData: Evicted ${expiredKeys.length} expired query cache entries.');
  }
  return expiredKeys.length;
}