Class CacheFactory<TAdapters>

The CacheFactory class is immutable.

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

Type Parameters

  • TAdapters extends string = string

Implements

Constructors

  • Type Parameters

    • TAdapters extends string = string

    Parameters

    Returns CacheFactory<TAdapters>

    import { CacheFactory } from "@daiso-tech/core/cache";
    import { MemoryCacheAdapter, RedisCacheAdapter } from "@daiso-tech/core/cache/adapters";
    import { Serde } from "@daiso-tech/core/serde";
    import type { ISerde } from "@daiso-tech/core/serde/contracts";
    import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/adapters";
    import { Namespace } from "@daiso-tech/core/utilities";
    import Redis from "ioredis"

    const serde = new Serde(new SuperJsonSerdeAdapter());
    const cacheFactory = new CacheFactory({
    namespace: new Namespace("cache"),
    adapters: {
    memory: new MemoryCacheAdapter(),
    redis: new RedisCacheAdapter({
    database: new Redis("YOUR_REDIS_CONNECTION"),
    serde,
    }),
    },
    defaultAdapter: "memory",

Methods

  • Type Parameters

    • TType = unknown

    Parameters

    Returns ICache<TType>

    import { CacheFactory } from "@daiso-tech/core/cache";
    import { MemoryCacheAdapter, RedisCacheAdapter } from "@daiso-tech/core/cache/adapters";
    import { Serde } from "@daiso-tech/core/serde";
    import type { ISerde } from "@daiso-tech/core/serde/contracts";
    import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/adapters";
    import { Namespace, TimeSpan } from "@daiso-tech/core/utilities";
    import Redis from "ioredis"

    const serde = new Serde(new SuperJsonSerdeAdapter());
    const cacheFactory = new CacheFactory({
    namespace: new Namespace("cache"),
    adapters: {
    memory: new MemoryCacheAdapter(),
    redis: new RedisCacheAdapter({
    database: new Redis("YOUR_REDIS_CONNECTION"),
    serde,
    }),
    },
    defaultAdapter: "memory",
    });

    // Will add key to cache using the default adapter which is MemoryCacheAdapter
    await cacheFactory
    .use()
    .add("a", 1);

    // Will add key to cache using the redis adapter which is RedisCacheAdapter
    await cacheFactory
    .use("redis")
    .add("a", 1);

    // You can change the default settings of the returned Cache instance.
    await cacheFactory
    .setDefaultTtl(TimeSpan.fromMinutes(2))
    .use("sqlite")
    .add("a", 1);