clean method

  1. @override
Future<void> clean({
  1. CachePriority priorityOrBelow = CachePriority.high,
  2. bool staleOnly = false,
})

Removes all keys from store. priorityOrBelow flag will remove keys only for the priority or below. staleOnly flag will remove keys only if expired (from maxStale).

By default, all keys will be removed.

Implementation

@override
Future<void> clean({
  CachePriority priorityOrBelow = CachePriority.high,
  bool staleOnly = false,
}) async {
  final box = await _openBox();

  final keys = <String>[];

  for (var i = 0; i < box.keys.length; i++) {
    final resp = await box.getAt(i);

    if (resp != null) {
      var shouldRemove = resp.priority.index <= priorityOrBelow.index;
      shouldRemove &= (staleOnly && resp.isStaled()) || !staleOnly;

      if (shouldRemove) {
        keys.add(resp.key);
      }
    }
  }

  return box.deleteAll(keys);
}