Skip to main content

xlru 0.1.3 copy "xlru: ^0.1.3" to clipboard
xlru: ^0.1.3 copied to clipboard

A zero-dependency LRU cache with reason-aware removal callbacks, reference-counted pinning, and non-promoting lookups.

example/xlru_example.dart

import 'package:xlru/xlru.dart';

// A pure Dart stand-in for a scene feed owned by a Flutter application.
class SceneFeed {
  SceneFeed(this.scene);
  final String scene;

  void dispose() => print('Disposed $scene');
}

void main() {
  final feeds = XlruCache<String, SceneFeed>(
    maxSize: 2,
    onRemove: (key, feed, cause) {
      print('$key: ${cause.name}');
      feed.dispose();
    },
  );

  SceneFeed open(String scene) =>
      feeds.putIfAbsent(scene, () => SceneFeed(scene));

  open('forest');
  feeds.pin('forest'); // The drawer is using this feed.
  open('desert');
  open('ocean'); // Disposes desert; forest is protected.
  print(feeds.keys.toList()); // [ocean, forest]
  print(feeds.peek('forest')?.scene); // Does not promote forest.

  feeds.unpin('forest'); // The drawer has closed.
  open('mountain'); // Disposes forest, the least recently used feed.
  feeds.clear(); // Disposes ocean, then mountain.
}
1
likes
160
points
462
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A zero-dependency LRU cache with reason-aware removal callbacks, reference-counted pinning, and non-promoting lookups.

Repository (GitHub)
View/report issues

Topics

#cache #lru #data-structures #collections #eviction

License

MIT (license)

More

Packages that depend on xlru