import { Hookified } from 'hookified'; import Keyv from 'keyv'; type NodeCacheStoreOptions = { /** * Time to live in milliseconds. This is a breaking change from the original NodeCache. */ ttl?: number | string; /** * The Keyv store instance. */ store?: Keyv; }; declare class NodeCacheStore extends Hookified { private _keyv; private _ttl?; private _stats; constructor(options?: NodeCacheStoreOptions); /** * Time to live in milliseconds. * @returns {number | string | undefined} * @readonly */ get ttl(): number | string | undefined; /** * Time to live in milliseconds. * @param {number | string | undefined} ttl */ set ttl(ttl: number | string | undefined); /** * The Keyv store instance. * @returns {Keyv} * @readonly */ get store(): Keyv; /** * Set a key/value pair in the cache. * @param {string | number} key * @param {T} value * @param {number} [ttl] * @returns {boolean} */ set(key: string | number, value: T, ttl?: number | string): Promise; /** * Set multiple key/value pairs in the cache. * @param {PartialNodeCacheItem[]} list * @returns {void} */ mset(list: Array>): Promise; /** * Get a value from the cache. * @param {string | number} key * @returns {any | undefined} */ get(key: string | number): Promise; /** * Get multiple values from the cache. * @param {Array} keys * @returns {Record} */ mget(keys: Array): Promise>; /** * Delete a key from the cache. * @param {string | number} key * @returns {boolean} */ del(key: string | number): Promise; /** * Delete multiple keys from the cache. * @param {Array} keys * @returns {boolean} */ mdel(keys: Array): Promise; /** * Clear the cache. * @returns {void} */ clear(): Promise; /** * Set the TTL of an existing key in the cache. * @param {string | number} key * @param {number | string} [ttl] * @returns {boolean} */ setTtl(key: string | number, ttl?: number | string): Promise; /** * Get a key from the cache and delete it. If it does exist it will get the value and delete the item from the cache. * @param {string | number} key * @returns {T | undefined} */ take(key: string | number): Promise; /** * Gets the stats of the cache * @returns {NodeCacheStats} the stats of the cache */ getStats(): NodeCacheStats; /** * Flush the stats. * @returns {void} */ flushStats(): void; /** * Disconnect from the cache. * @returns {void} */ disconnect(): Promise; /** * Resolve the TTL to milliseconds. * @param {number | string | undefined} ttl * @returns {number | undefined} */ private resolveTtl; } type NodeCacheOptions = { /** * The standard ttl as number in seconds for every generated cache element. 0 = unlimited, If string, it will be in shorthand format like '1h' for 1 hour */ stdTTL?: number | string; /** * The interval to check for expired items in seconds. Default is 600 = 5 minutes */ checkperiod?: number; /** * If set to true (Default Setting), the cache will clone the returned items via get() functions. * This means that every time you set a value into the cache, node-cache makes a deep clone of it. * When you get that value back, you receive another deep clone. * This mimics the behavior of an external cache like Redis or Memcached, meaning mutations to the * returned object do not affect the cached copy (and vice versa). If set to false, the original * object will be returned, and mutations will affect the cached copy. */ useClones?: boolean; /** * Delete expired items during an interval check or a single item on a get request. Default is true. */ deleteOnExpire?: boolean; /** * The maximum number of keys that will be stored in the cache. Default is -1 = unlimited * If the limit is reached, it will no longer add any new items until some expire. */ maxKeys?: number; }; type PartialNodeCacheItem = { /** * The key of the item */ key: string | number; /** * The value of the item */ value: T; /** * The ttl of the item in seconds. 0 = unlimited */ ttl?: number; }; type NodeCacheItem = PartialNodeCacheItem & { /** * The ttl of the item in milliseconds. 0 = unlimited */ ttl: number; }; declare enum NodeCacheErrors { ECACHEFULL = "Cache max keys amount exceeded", EKEYTYPE = "The key argument has to be of type `string` or `number`. Found: `__key`", EKEYSTYPE = "The keys argument has to be an array.", ETTLTYPE = "The ttl argument has to be a number or a string for shorthand ttl." } type NodeCacheStats = { /** * The number of keys stored in the cache */ keys: number; /** * The number of hits */ hits: number; /** * The number of misses */ misses: number; /** * The global key size count in approximately bytes */ ksize: number; /** * The global value size count in approximately bytes */ vsize: number; }; declare class NodeCache extends Hookified { readonly options: NodeCacheOptions; readonly store: Map>; private _stats; private intervalId; constructor(options?: NodeCacheOptions); /** * Sets a key value pair. It is possible to define a ttl (in seconds). Returns true on success. * * TTL behavior: * - `ttl > 0`: cache expires after the given number of seconds * - `ttl === 0`: cache indefinitely (overrides stdTTL) * - `ttl < 0`: store the value but it expires immediately on next access (matches original node-cache behavior) * - `ttl` omitted/undefined: use `stdTTL` from options (0 = unlimited if stdTTL is 0 or not set) * - `ttl` as string: shorthand format like '1h', '30s', '5m', '2d' * * @param {string | number} key - it will convert the key to a string * @param {T} value * @param {number | string} [ttl] - TTL in seconds. 0 = unlimited, negative = expires immediately, string = shorthand format * @returns {boolean} true on success * @throws {Error} If the `key` or `ttl` is of an invalid type. */ set(key: string | number, value: T, ttl?: number | string): boolean; /** * Sets multiple key val pairs. It is possible to define a ttl (seconds). Returns true on success. * * Each item follows the same TTL behavior as `set()`: * - Positive TTL: expires after the given seconds * - `0`: cache indefinitely * - Negative TTL: stored but expires immediately on next access * - Omitted: uses `stdTTL` from options * * @param {PartialNodeCacheItem[]} data an array of key value pairs with optional ttl * @returns {boolean} true on success * @throws {Error} If `data` is not an array, or if any item has an invalid key or ttl type. */ mset(data: Array>): boolean; /** * Gets a saved value from the cache. Returns a undefined if not found or expired. If the value was found it returns the value. * @param {string | number} key if the key is a number it will convert it to a string * @returns {T} the value or undefined */ get(key: string | number): T | undefined; /** * Gets multiple saved values from the cache. Returns an empty object {} if not found or expired. * If the value was found it returns an object with the key value pair. * @param {Array} an object with the key as a property and the value as the value */ mget(keys: Array): Record; /** * Get the cached value and remove the key from the cache. Equivalent to calling get(key) + del(key). * Useful for implementing single use mechanism such as OTP, where once a value is read it will become obsolete. * @param {string | number} key * @returns {T | undefined} the value or undefined */ take(key: string | number): V | undefined; /** * Delete a key. Returns the number of deleted entries. A delete will never fail. * @param {string | number | Array} key if the key is a number it will convert it to a string. if an array is passed it will delete all keys in the array. * @returns {number} if it was successful it will return the count that was deleted */ del(key: string | number | Array): number; /** * Delete all keys in Array that exist. Returns the number of deleted entries. * @param {Array} keys an array of keys * @returns {number} the count of deleted keys */ mdel(keys: Array): number; /** * Redefine the ttl of a key. Returns true if the key has been found and changed. * Otherwise returns false. If the ttl-argument isn't passed the default-TTL will be used. * * TTL behavior: * - `ttl > 0`: key expires after the given number of seconds * - `ttl === 0`: key lives indefinitely (overrides stdTTL) * - `ttl < 0`: key expires immediately on next access (matches original node-cache behavior) * - `ttl` omitted/undefined: use `stdTTL` from options (0 = unlimited if stdTTL is 0 or not set) * - `ttl` as string: shorthand format like '1h', '30s', '5m', '2d' * * @param {string | number} key if the key is a number it will convert it to a string * @param {number | string} [ttl] TTL in seconds. 0 = unlimited, negative = expires immediately, string = shorthand format * @returns {boolean} true if the key has been found and changed. Otherwise returns false. * @throws {Error} If the `ttl` is of an invalid type (must be a number or string). */ ttl(key: string | number, ttl?: number | string): boolean; /** * Receive the ttl of a key. * @param {string | number} key if the key is a number it will convert it to a string * @returns {number | undefined} 0 if this key has no ttl, undefined if this key is not in the cache, * a timestamp in ms representing the time at which this key will expire */ getTtl(key: string | number): number | undefined; /** * Returns an array of all existing keys. [ "all", "my", "keys", "foo", "bar" ] * @returns {string[]} an array of all keys */ keys(): string[]; /** * Returns boolean indicating if the key is cached. * @param {string | number} key if the key is a number it will convert it to a string * @returns {boolean} true if the key is cached */ has(key: string | number): boolean; /** * Gets the stats of the cache * @returns {NodeCacheStats} the stats of the cache */ getStats(): NodeCacheStats; /** * Flush the whole data. * @returns {void} */ flushAll(): void; /** * Flush the stats. * @returns {void} */ flushStats(): void; /** * Close the cache. This will clear the interval timeout which is set on check period option. * @returns {void} */ close(): void; /** * Get the interval id * @returns {number | NodeJS.Timeout} the interval id */ getIntervalId(): number | NodeJS.Timeout; startInterval(): void; stopInterval(): void; private formatKey; private clone; private getExpirationTimestamp; /** * Resolves a TTL value to an expiration timestamp, returning 0 (unlimited) if the * resolved timestamp is not in the future (e.g. "0s" or a zero-duration string). */ private resolveExpiration; /** * Checks whether a TTL value is negative. Handles both numbers and * purely numeric strings (e.g. "-1"). */ private isNegativeTtl; private checkData; private createError; } export { NodeCache, NodeCacheErrors, type NodeCacheItem, type NodeCacheOptions, type NodeCacheStats, NodeCacheStore, type NodeCacheStoreOptions, type PartialNodeCacheItem, NodeCache as default };