Skip to content

Instantly share code, notes, and snippets.

@slightfoot
Last active November 6, 2023 03:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save slightfoot/5730285913e11cf34f5e8bb82f620f39 to your computer and use it in GitHub Desktop.
Save slightfoot/5730285913e11cf34f5e8bb82f620f39 to your computer and use it in GitHub Desktop.
Time Aware Least Recently Used Cache - by Simon Lightfoot http://en.wikipedia.org/wiki/Cache_algorithms#LRU
// MIT License
//
// Copyright (c) 2019 Simon Lightfoot
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import 'package:meta/meta.dart';
import 'package:quiver/collection.dart' show LruMap;
/// Time Aware Least Recently Used Cache - by Simon Lightfoot
/// http://en.wikipedia.org/wiki/Cache_algorithms#LRU
class LruCache<T> {
LruCache(
this.maximumSize, {
this.ttl = const Duration(minutes: 10),
}) : _cacheMap = LruMap(maximumSize: maximumSize);
final int maximumSize;
final Duration ttl;
final LruMap<String, Expirable<T>> _cacheMap;
T operator [](String key) {
final item = _cacheMap[key];
if (item?.expired ?? false) {
invalid(key);
return null;
}
return item?.value;
}
void operator []=(String key, T value) {
_cacheMap[key] = Expirable.fromNow(ttl, value);
}
bool invalid(String key) {
return _cacheMap.remove(key) != null;
}
void clear() {
_cacheMap.clear();
}
}
@immutable
class Expirable<T> {
const Expirable(this.expiry, this.value);
factory Expirable.fromNow(Duration ttl, T value) {
return Expirable(DateTime.now().add(ttl), value);
}
final DateTime expiry;
final T value;
bool get expired => DateTime.now().compareTo(expiry) >= 0;
@override
bool operator ==(Object other) =>
identical(this, other) || other is Expirable && runtimeType == other.runtimeType && value == other.value;
@override
int get hashCode => value.hashCode;
@override
String toString() {
return 'Expirable { value: $value, expiry: $expiry }';
}
}
@pishguy
Copy link

pishguy commented Apr 16, 2020

@slightfoot Can you give an example?

@teamextension
Copy link

Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment