import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
const cache: IGroupableCache = new Cache({
adapter: new MemoryCacheAdapter({
rootGroup: "@global"
}),
});
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.
You can listen to different events of Cache class instance.
Refer to CacheEvents, to se all events dispatched by Cache class instance. Refer to IEventListenable for details on how the method works.
import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
import type { Invokable} from "@daiso-tech/core/event-bus/contracts";
import { KeyAddedCacheEvent, type CacheEvents } from "@daiso-tech/core/cache/contracts";
const cache: IGroupableCache = new Cache({
adapter: new MemoryCacheAdapter({
rootGroup: "@global"
}),
});
const listener: Invokable<CacheEvents> = event => {
console.log(event);
};
await cache.addListener(KeyAddedCacheEvent, listener);
await cache.add("a", 1);
You can listen to different events of Cache class instance.
Refer to CacheEvents, to se all events dispatched by Cache class instance. Refer to IEventListenable for details on how the method works.
import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
import type { Invokable} from "@daiso-tech/core/event-bus/contracts";
import type { Invokable} from "@daiso-tech/core/event-bus/contracts";
import { KeyAddedCacheEvent, type CacheEvents } from "@daiso-tech/core/cache/contracts";
const cache: IGroupableCache = new Cache({
adapter: new MemoryCacheAdapter({
rootGroup: "@global"
}),
});
const listener: Invokable<CacheEvents> = event => {
console.log(event);
};
await cache.addListenerMany([KeyAddedCacheEvent], listener);
await cache.add("a", 1);
The addMany method adds new keys. Returns true for the keys that where added otherwise false will be returned.
The asPromise method returns LazyPromise objecet that resolves once the BaseEvent is dispatched.
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.
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.
import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
const cache: IGroupableCache = new Cache({
adapter: new MemoryCacheAdapter({
rootGroup: "@global"
}),
});
const result1 = await cache.getAndRemove("a");
// Will print null
console.log(result1);
await cache.add("a", 2)
const result2 = await cache.getAndRemove("a");
// Will print 2
console.log(result2);
const result3 = await cache.get("a");
// Will print null
console.log(result3);
import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
const cache: IGroupableCache = new Cache({
adapter: new MemoryCacheAdapter({
rootGroup: "@global"
}),
});
// Will print "@global"
console.log(cache.getGroup());
The getMany returns the value for the keys that are found otherwise null will be returned.
import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
const cache: IGroupableCache = new Cache({
adapter: new MemoryCacheAdapter({
rootGroup: "@global"
}),
});
const result = await cache.getOr("a", -1);
// -1
console.log(result);
You can pass function as default value.
import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
const cache: IGroupableCache = new Cache({
adapter: new MemoryCacheAdapter({
rootGroup: "@global"
}),
});
const result = await cache.getOr("a", () => -1);
// -1
console.log(result);
You can pass async function as default value.
import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
const cache: IGroupableCache = new Cache({
adapter: new MemoryCacheAdapter({
rootGroup: "@global"
}),
});
const result = await cache.getOr("a", async () => -1);
// -1
console.log(result);
You can pass LazyPromise as default value.
import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
const cache: IGroupableCache = new Cache({
adapter: new MemoryCacheAdapter({
rootGroup: "@global"
}),
});
const result = await cache.getOr("a", new LazyPromise(async () => -1));
// -1
console.log(result);
Optional
ttl: TimeSpanimport type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
const cache: IGroupableCache = new Cache({
adapter: new MemoryCacheAdapter({
rootGroup: "@global"
}),
});
const result = await cache.getOrAdd("a", -1);
// -1
console.log(result);
You can pass function as default value.
import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
const cache: IGroupableCache = new Cache({
adapter: new MemoryCacheAdapter({
rootGroup: "@global"
}),
});
const result = await cache.getOrAdd("a", () => -1);
// -1
console.log(result);
You can pass async function as default value.
import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
const cache: IGroupableCache = new Cache({
adapter: new MemoryCacheAdapter({
rootGroup: "@global"
}),
});
const result = await cache.getOrAdd("a", async () => -1);
// -1
console.log(result);
You can pass LazyPromise as default value.
import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
const cache: IGroupableCache = new Cache({
adapter: new MemoryCacheAdapter({
rootGroup: "@global"
}),
});
const result = await cache.getOrAdd("a", new LazyPromise(async () => -1));
// -1
console.log(result);
import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
const cache: IGroupableCache = new Cache({
adapter: new MemoryCacheAdapter({
rootGroup: "@global"
}),
});
await cache.add("a", 1);
const result1 = await cache.getOrFail("a");
// Will print 1
console.log(result1);
await cache.remove("a");
// Will throw an error
await cache.getOrFail("a");
import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
const cache: IGroupableCache = new Cache({
adapter: new MemoryCacheAdapter({
rootGroup: "@global"
}),
});
const result = await cache.getOrMany({ a: -1 });
// { a: -1 }
console.log(result);
You can pass function as default value.
import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
const cache: IGroupableCache = new Cache({
adapter: new MemoryCacheAdapter({
rootGroup: "@global"
}),
});
const result = await cache.getOrMany({ a: () => -1 });
// { a: -1 }
console.log(result);
You can pass async function as default value.
import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
const cache: IGroupableCache = new Cache({
adapter: new MemoryCacheAdapter({
rootGroup: "@global"
}),
});
const result = await cache.getOrMany({ a: async () => -1 });
// { a: -1 }
console.log(result);
You can pass LazyPromise as default value.
import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
const cache: IGroupableCache = new Cache({
adapter: new MemoryCacheAdapter({
rootGroup: "@global"
}),
});
const result = await cache.getOrMany({ a: new LazyPromise(async () => - 1) });
// { a: -1 }
console.log(result);
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.
You can listen to different events of Cache class instance.
Refer to CacheEvents, to se all events dispatched by Cache class instance. Refer to IEventListenable for details on how the method works.
import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
import type { Invokable} from "@daiso-tech/core/event-bus/contracts";
import { KeyAddedCacheEvent, type CacheEvents } from "@daiso-tech/core/cache/contracts";
const cache: IGroupableCache = new Cache({
adapter: new MemoryCacheAdapter({
rootGroup: "@global"
}),
});
const listener: Invokable<CacheEvents> = event => {
console.log(event);
};
await cache.listenOnce(KeyAddedCacheEvent, listener);
await cache.add("a", 1);
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 can provide a ttl value for the replaced key. If null is passed, the item will not expires and null is the default value.
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.
You can listen to different events of Cache class instance.
Refer to CacheEvents, to se all events dispatched by Cache class instance. Refer to IEventListenable for details on how the method works.
import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
import type { Invokable} from "@daiso-tech/core/event-bus/contracts";
import { KeyAddedCacheEvent, type CacheEvents } from "@daiso-tech/core/cache/contracts";
const cache: IGroupableCache = new Cache({
adapter: new MemoryCacheAdapter({
rootGroup: "@global"
}),
});
const listener: Invokable<CacheEvents> = event => {
console.log(event);
};
await cache.addListener(KeyAddedCacheEvent, listener);
await cache.removeListener(KeyAddedCacheEvent, listener);
await cache.add("a", 1);
You can listen to different events of Cache class instance.
Refer to CacheEvents, to se all events dispatched by Cache class instance. Refer to IEventListenable for details on how the method works.
import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
import type { Invokable} from "@daiso-tech/core/event-bus/contracts";
import { KeyAddedCacheEvent, type CacheEvents } from "@daiso-tech/core/cache/contracts";
const cache: IGroupableCache = new Cache({
adapter: new MemoryCacheAdapter({
rootGroup: "@global"
}),
});
const listener: Invokable<CacheEvents> = event => {
console.log(event);
};
await cache.addListenerMany([KeyAddedCacheEvent], listener);
await cache.removeListenerMany(KeyAddedCacheEvent, listener);
await cache.add("a", 1);
The removeMany method removes keys. Returns true for the keys that are removed otherwise false is returned.
You can listen to different events of Cache class instance.
Refer to CacheEvents, to se all events dispatched by Cache class instance. Refer to IEventListenable for details on how the method works.
import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
import type { Invokable} from "@daiso-tech/core/event-bus/contracts";
import { KeyAddedCacheEvent, type CacheEvents } from "@daiso-tech/core/cache/contracts";
const cache: IGroupableCache = new Cache({
adapter: new MemoryCacheAdapter({
rootGroup: "@global"
}),
});
const listener: Invokable<CacheEvents> = event => {
console.log(event);
};
const unsubscribe = await cache.subscribe(KeyAddedCacheEvent, listener);
await cache.add("a", 1);
await unsubscribe();
You can listen to different events of Cache class instance.
Refer to CacheEvents, to se all events dispatched by Cache class instance. Refer to IEventListenable for details on how the method works.
import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
import type { Invokable} from "@daiso-tech/core/event-bus/contracts";
import { KeyAddedCacheEvent, type CacheEvents } from "@daiso-tech/core/cache/contracts";
const cache: IGroupableCache = new Cache({
adapter: new MemoryCacheAdapter({
rootGroup: "@global"
}),
});
const listener: Invokable<CacheEvents> = event => {
console.log(event);
};
const unsubscribe = await cache.subscribeMany([KeyAddedCacheEvent], listener);
await cache.add("a", 1);
await unsubscribe();
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.
import type { IGroupableCache, ICache } from "@daiso-tech/core/cache/contracts";
import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
const cache: IGroupableCache = new Cache({
adapter: new MemoryCacheAdapter({
rootGroup: "@global"
}),
});
// Will print "@global"
console.log(cache.getGroup());
const groupedCache: ICache = cache.withGroup("company-1");
// Will print "@global/company-1"
console.log(groupedCache.getGroup());
Cache class can be derived from any ICacheAdapter.
IMPORT_PATH:
"@daiso-tech/core/cache/implementations/derivables"