performCleanup static method

Future<bool> performCleanup(
  1. CacheAdapter cacheAdapter
)

Performs the cleanup operation.

This function retrieves the CacheAdapter from the service locator and iterates through the keys in batches, deleting any expired cache items.

Implementation

static Future<bool> performCleanup(CacheAdapter cacheAdapter) async {
  if (_isRunning) return true; // Prevent concurrent runs

  _isRunning = true;
  try {
    const batchSize = 50;
    int offset = 0;

    while (true) {
      final keys =
          await cacheAdapter.getKeys(limit: batchSize, offset: offset);
      if (keys.isEmpty) {
        break;
      }

      for (final key in keys) {
        final cacheItem = await cacheAdapter.get(key);
        if (cacheItem != null && cacheItem.isExpired) {
          await cacheAdapter.delete(key);
        }
      }
      offset += batchSize;
    }
    return true;
  } catch (err) {
    log("Error in background cleanup: $err");
    return false;
  } finally {
    _isRunning = false;
  }
}