platelk / dcache

Dart cache package
https://pub.dartlang.org/packages/dcache
MIT License
29 stars 9 forks source link

OnEvict is not getting invoked on latest 0.4.0 #15

Open dhirajhimani opened 2 years ago

dhirajhimani commented 2 years ago

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,

  1. element should not be in cache
  2. _loaderFunc should be initialized
  3. 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; }

`

robertflesch commented 2 years ago

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)) {