AkashBabu / lfu-cache

LFU cache implementation with a complexity of `O(1)` for all transactions
MIT License
7 stars 0 forks source link

Saving value for existing key not working #3

Closed msetten closed 1 year ago

msetten commented 1 year ago

I have the issue that setting a new value for an existing key, won't save the new value, but retains the old value.

This is piece of code with test logging:

console.log("Value to save: " + value);
console.log("Old cached value: " + cache.get(key));
cache.set(key, value);
console.log("New cached value: " + cache.get(key));

The console log show:

Value to save: 1006131968259391488
Old cached value: 1012285850970755124
New cached value: 1012285850970755124
msetten commented 1 year ago

My current working around is using:

if ( typeof cache.get(key) !== "undefined" ) {
   cache.delete(key);
}
cache.set(key, value);

But I think that shouldn't be necessary to use.

AkashBabu commented 1 year ago

@msetten , thanks for pointing this out. Will provide a flag force: boolean on set method, so that you can decide whether to override an existing value or not, so that the library would be backwards compatible.

AkashBabu commented 1 year ago

Hi @msetten , have added support for force setting a value in cache by using cache.set('foo', 'bar', true). Please upgrade the library to @akashbabu/lfu-cache@1.1.0