The add method adds a key with given value when key doesn't exists. Returns true when key doesn't exists otherwise false will be returned. You can provide a ttl value. If null is passed, the item will not expire.
The addListener method is used for adding listener for certain event. A listener can only be added once for a specific event. Adding the same listener multiple times will have no effect and nothing will occur.
The addListenerMany method is used for adding multiple listeners for certain events. A listener can only be added once for a specific event. Adding the same listener multiple times will have no effect and nothing will occur.
The addMany method adds new keys. Returns true for the keys that where added otherwise false will be returned.
The clear method removes all the keys in the cache.
The decrement method will decrement the given key if found otherwise nonthing will occur. Returns true if key exists otherwise false will be returned. An error will thrown if the key is not a number.
TypeCacheError An error will thrown if the key is not a number.
The exists method returns true when key is found otherwise false will be returned.
The existsMany method returns true for the keys that are found otherwise false will be returned.
The get method returns the value when key is found otherwise null will be returned.
The getAndRemove method removes the given key and returns it when found otherwise null will be returned.
The getMany returns the value for the keys that are found otherwise null will be returned.
The getOr method returns the value when key is found otherwise defaultValue will be returned.
The getOrAdd method will retrieve the given key if found otherwise valueToAdd will be added and returned.
Optional
ttl: TimeSpanThe getOrFail method returns the value when key is found otherwise an error will be thrown.
The getOrMany method returns the value for the keys that are found otherwise defaultValue will be returned.
The increment method will increment the given key if found otherwise nonthing will occur. Returns true if key is incremented otherwise false will be returned. An error will thrown if the key is not a number.
The listenOnce method is used for adding listener for certain event that is trigged only once.
The missing method returns true when key is not found otherwise false will be returned.
The missingMany method returns true for the keys that are not found otherwise false will be returned.
The put method replaces a key if the key exists including the ttl value or adds key that do not exists with a given ttl. Returns true if the key where replaced otherwise false is returned. You must provide a ttl value. If null is passed, the item will not expire.
The putMany method replaces the keys that exists including their ttl values or adds keys that do not exists. Returns true for all the keys that where replaced otherwise false is returned.
The remove method removes the given key when found. Returns true if the key is found otherwise false is returned.
The removeListener method is used for removing listener for certain event. Removing unadded listener will have no effect and nothing will occur.
The removeListener method is used for removing multiple listeners for certain event. Removing unadded listener will have no effect and nothing will occur.
The removeMany method removes keys. Returns true for the keys that are removed otherwise false is returned.
The subscribe method is used for adding listener for certain event. A listener can only be added once for a specific event. Adding the same listener multiple times will have no effect and nothing will occur.
The subscribeMany method is used for adding listener for multiple events. A listener can only be added once for a specific event. Adding the same listener multiple times will have no effect and nothing will occur.
The update method updates the given key with given value. Returns true when key otherwise false will be returned.
The updateMany method updates the given keys. Returns true for the keys that where updated otherwise false will be returned.
The withGroup method returns a new ICache instance that groups keys together. Only keys in the group can be updated, removed, or retrieved, leaving keys outside the group unaffected. This useful for multitennat applications.
import { type ICache } from "@daiso-tech/core";
async function main(cache: ICache): Promise<void> {
const cacheA = cache.withGroup("a");
await cacheA.add("a", 1);
const cacheB = cache.withGroup("b");
await cacheB.add("b", 2);
// Will print { a: 1, b: null }
console.log(await cacheA.getMany(["a", "b"]));
}
Cache class can be derived from any ICacheAdapter.
Example