Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 20 additions & 21 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ import Callback = NodeCache.Callback;
import ValueSetItem = NodeCache.ValueSetItem;
import NodeCacheLegacyCallbacks = NodeCache.NodeCacheLegacyCallbacks;

declare class NodeCache extends events.EventEmitter {
declare class NodeCache<ValueType = unknown> extends events.EventEmitter {
/** container for cached data */
data: Data;

Expand All @@ -256,19 +256,19 @@ declare class NodeCache extends events.EventEmitter {
* @param key cache key
* @returns The value stored in the key
*/
get<T>(
get<T extends ValueType>(
key: Key
): T | undefined;
): ValueType extends unknown ? T : ValueType | undefined;

/**
* get multiple cached keys at once and change the stats
*
* @param keys an array of keys
* @returns an object containing the values stored in the matching keys
*/
mget<T>(
mget<T extends ValueType>(
keys: Key[]
): { [key: string]: T };
): { [key: string]: ValueType extends unknown ? T : ValueType };

/**
* set a cached key and change the stats
Expand All @@ -278,42 +278,42 @@ declare class NodeCache extends events.EventEmitter {
* it to a serialized JSON
* @param ttl The time to live in seconds.
*/
set<T>(
set<T extends ValueType>(
key: Key,
value: T,
value: ValueType extends unknown ? T : ValueType,
ttl: number | string
): boolean;

set<T>(
set<T extends ValueType>(
key: Key,
value: T
value: ValueType extends unknown ? T : ValueType
): boolean;

/**
* in the event of a cache miss (no value is assinged to given cache key), value will be written to cache and returned. In case of cache hit, cached value will be returned without executing given value. If the given value is type of `Function`, it will be executed and returned result will be fetched
*
*
* @param key cache key
* @param ttl The time to live in seconds.
* @param value function that returns a value to be stored in cache, or the value itself
*/
fetch<T>(
fetch<T extends ValueType>(
key: Key,
ttl: number | string,
value: () => T,
): T;
value: () => ValueType extends unknown ? T : ValueType,
): ValueType extends unknown ? T : ValueType;

fetch<T>(
fetch<T extends ValueType>(
key: Key,
value: () => T,
): T;
value: () => ValueType extends unknown ? T : ValueType,
): ValueType extends unknown ? T : ValueType;

/**
* set multiple cached keys at once and change the stats
*
* @param keyValueSet an array of object which includes key,value and ttl
*/
mset<T>(
keyValueSet: ValueSetItem<T>[]
mset<T extends ValueType>(
keyValueSet: ValueSetItem<ValueType extends unknown ? T : ValueType>[]
): boolean;

/**
Expand All @@ -334,10 +334,9 @@ declare class NodeCache extends events.EventEmitter {
* @param key cache key
* @returns The value stored in the key
*/
take<T>(
take<T extends ValueType>(
key: Key
): T | undefined;

): (ValueType extends unknown ? T : ValueType) | undefined;
/**
* reset or redefine the ttl of a key. If `ttl` is not passed or set to 0 it's similar to `.del()`
*/
Expand Down