For OnEvict method to be invoked, earlier only capacity was checked, but for this new release 0.4.0 there are a few conditions to be met,
element should not be in cache
_loaderFunc should be initialized
1 and 2 steps are triggered only when we try to get something from cache,
Please could you look into this?
earlier :-
`
@override
SimpleCache<K, V> _set(K key, V element) {
// Remove the first key to respect the size, no time logic here.
// It's a wanted simple and not smart implementation
if (!this._internalStorage.containsKey(key) && this.length >= this._internalStorage.capacity) {
if (onEvict != null) {
CacheEntry<K, V> c = this._internalStorage.get(this._internalStorage.keys.first);
this._internalStorage.remove(this._internalStorage.keys.first);
onEvict(c.key, c.value);
} else {
this._internalStorage.remove(this._internalStorage.keys.first);
}
}
this._internalStorage[key] = new CacheEntry(key, element, new DateTime.now());
return this;
}
`
Latest:-
`
V? get(K key) {
if (_loaderFunc != null && !containsKey(key)) {
if (_internalStorage.length >= _internalStorage.capacity) {
var garbage = _collectGarbage(_internalStorage.length - _internalStorage.capacity + 1);
if (onEvict != null) {
for (var e in garbage) {
onEvict!(e.key, e.value);
}
}
for (var e in garbage) {
_internalStorage.remove(e.key);
}
}
_loadFirstValue(key);
}
var entry = _get(key);
if (entry == null) {
return null;
}
Also running into onEvict not getting called. Temp fix by modifying lib
if (_loaderFunc != null && !containsKey(key)) {
replaced by this fixes it
if (!containsKey(key)) {
For OnEvict method to be invoked, earlier only capacity was checked, but for this new release 0.4.0 there are a few conditions to be met,
Please could you look into this?
earlier :-
`
@override SimpleCache<K, V> _set(K key, V element) { // Remove the first key to respect the size, no time logic here. // It's a wanted simple and not smart implementation if (!this._internalStorage.containsKey(key) && this.length >= this._internalStorage.capacity) { if (onEvict != null) { CacheEntry<K, V> c = this._internalStorage.get(this._internalStorage.keys.first); this._internalStorage.remove(this._internalStorage.keys.first); onEvict(c.key, c.value); } else { this._internalStorage.remove(this._internalStorage.keys.first); } } this._internalStorage[key] = new CacheEntry(key, element, new DateTime.now()); return this; }
`
Latest:- `
V? get(K key) { if (_loaderFunc != null && !containsKey(key)) { if (_internalStorage.length >= _internalStorage.capacity) { var garbage = _collectGarbage(_internalStorage.length - _internalStorage.capacity + 1); if (onEvict != null) { for (var e in garbage) { onEvict!(e.key, e.value); } } for (var e in garbage) { _internalStorage.remove(e.key); } } _loadFirstValue(key); } var entry = _get(key); if (entry == null) { return null; }
`