@thermopylae/lib.cache
Version:
25 lines (24 loc) • 797 B
JavaScript
import sizeof from 'object-sizeof';
import { BaseLFUEvictionPolicy } from "./lfu-base.js";
class GDSFEvictionPolicy extends BaseLFUEvictionPolicy {
sizeOf;
cacheAge;
constructor(cacheMaxCapacity, cacheBackendElementsCount, sizeOfInBytes) {
super(cacheMaxCapacity, cacheBackendElementsCount);
this.sizeOf = sizeOfInBytes || sizeof;
this.cacheAge = 0;
}
onUpdate(entry) {
this.onHit(entry);
}
get initialFrequency() {
return this.cacheAge;
}
computeEntryFrequency(entry, entryScore) {
return Math.round((entryScore / this.sizeOf(entry.value)) * 10) / 10 + this.cacheAge + 1;
}
onEvict(frequencyOfTheEvictedEntry) {
this.cacheAge = frequencyOfTheEvictedEntry;
}
}
export { GDSFEvictionPolicy };