ttl in cacheable/node-cache expects milliseconds, says it expects seconds
Closed this issue · 1 comments
Describe the bug
Because cacheable/node-cache is using cacheable and no longer the NodeCache Class under the hood the ttl option is no longer cast right. node-cache expects a ttl in s while cacheable expects ms. It might make sense to delete the NodeCache class as it is no longer used and to either cast ttl to ms before passing it to cacheable (see: https://github.com/jaredwray/cacheable/blob/8429488fdc78e9b204e38f5d61b616d00f476f74/packages/node-cache/src/store.ts#L17C1-L23C46)
@giesf - thanks so much for the information on this and I am updating the readme. There are now two types in node-cache
:
NodeCache
- this is the most compatible and thestdTTL
is in seconds as it uses an in-memory map to make it most compatible. We worked hard to make it as compatible as possible.NodeCacheStore
- this is the new version with Async handling and you can set the store using Keyv with as minimal code changes as possible. https://github.com/jaredwray/cacheable/tree/main/packages/node-cache#advanced-usage is an advanced example. Here we did decide to deprecatestdTTL
forttl
option which is in milliseconds. This is not used byNodeCache
You can pass in options to set the configuration for NodeCache
just like before:
export type NodeCacheOptions = {
stdTTL?: number; // The standard ttl as number in seconds for every generated cache element. 0 = unlimited
checkperiod?: number; // Default is 600, 0 means no periodic check
useClones?: boolean; // Default is true
deleteOnExpire?: boolean; // Default is true, if this is set to true it will delete the key when it expires.
maxKeys?: number; // Default is -1 (unlimited). If this is set it will throw and error if you try to set more keys than the max.
};
When initializing the cache you can pass in the options to set the configuration like the example below where we set the stdTTL
to 10 seconds and checkperiod
to 5 seconds.:
const cache = new NodeCache({stdTTL: 10, checkperiod: 5});
I am updating the readme now with this pull request. #808