Class Cache<TType>

Cache class can be derived from any ICacheAdapter.

IMPORT_PATH: "@daiso-tech/core/cache/implementations/derivables"

Type Parameters

  • TType = unknown

Implements

Constructors

  • Type Parameters

    • TType = unknown

    Parameters

    Returns Cache<TType>

    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"
    }),
    });

Methods

  • 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.

    Parameters

    • key: string
    • value: TType
    • ttl: null | TimeSpan = ...
      null
      

    Returns LazyPromise<boolean>

  • 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.

    Type Parameters

    Parameters

    Returns LazyPromise<void>

    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.

    Type Parameters

    Parameters

    Returns LazyPromise<void>

    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);
  • Parameters

    • key: string

    Returns LazyPromise<null | TType>

    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);
  • Returns string

    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());
  • Parameters

    Returns LazyPromise<TType>

    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);
  • Parameters

    Returns LazyPromise<TType>

    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 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);
  • Parameters

    • key: string

    Returns LazyPromise<TType>

    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");
  • Type Parameters

    • TKeys extends string

    Parameters

    Returns LazyPromise<Record<TKeys, TType>>

    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);
  • 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.

    Type Parameters

    Parameters

    Returns LazyPromise<void>

    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 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.

    Parameters

    • key: string
    • value: TType
    • ttl: null | TimeSpan = ...
      null
      

    Returns LazyPromise<boolean>

  • 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.

    Type Parameters

    Parameters

    Returns LazyPromise<void>

    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.

    Type Parameters

    Parameters

    Returns LazyPromise<void>

    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);
  • 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.

    Type Parameters

    Parameters

    Returns LazyPromise<Unsubscribe>

    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.

    Type Parameters

    Parameters

    Returns LazyPromise<Unsubscribe>

    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();
  • Parameters

    Returns ICache<TType>

    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());