Class Cache<TType>

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

Type Parameters

  • TType = unknown

Implements

Constructors

  • Type Parameters

    • TType = unknown

    Parameters

    Returns Cache<TType>

    import { KyselyCacheAdapter } from "@daiso-tech/core/cache/kysely-cache-adapter";
    import { Serde } from "@daiso-tech/core/serde";
    import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/super-json-serde-adapter"
    import Sqlite from "better-sqlite3";
    import { Cache } from "@daiso-tech/core/cache";
    import { Kysely, SqliteDialect } from "kysely";

    const database = new Sqlite("local.db");
    const serde = new Serde(new SuperJsonSerdeAdapter());
    const cacheAdapter = new KyselyCacheAdapter({
    kysely: new Kysely({
    dialect: new SqliteDialect({
    database,
    }),
    }),
    serde,
    });
    // You need initialize the adapter once before using it.
    await cacheAdapter.init();

    const cache = new Cache({
    adapter: cacheAdapter,
    });

Methods

  • The decrement method decrements the given key with given value. An error will thrown if the value is not a number.

    Parameters

    • key: string
    • value: Extract<TType, number> = ...

      If not defined then it will be defaulted to 1.

    Returns Promise<boolean>

    Returns true if the key where decremented otherwise false will be returned.

  • The decrementOrFail method decrements the given key with given value. An error will thrown if the value is not a number or if the key is not found.

    Parameters

    • key: string
    • Optionalvalue: Extract<TType, number>

      If not defined then it will be defaulted to 1.

    Returns Promise<void>

  • Checks if a key exists in the cache.

    Parameters

    • key: string

      The cache key to check

    Returns Promise<boolean>

    true if the key exists, false otherwise

  • The getAndRemove method returns the value when key is found otherwise null will be returned. The key will be removed after it is returned.

    Parameters

    • key: string

    Returns Promise<null | TType>

  • The getOrAdd method retrieves the value for the given key if it exists, otherwise adds the valueToAdd to the cache and returns it.

    Parameters

    • key: string

      The cache key to retrieve or add.

    • valueToAdd: TType

      The value to store if the key is not found.

    • ttl: null | ITimeSpan = ...

      Optional time-to-live for the cached item. If null is passed, the item will not expire.

    Returns Promise<TType>

    The cached value if the key exists, or the newly added value.

  • Retrieves a cached value by key, throwing an error if not found.

    Parameters

    • key: string

      The cache key to retrieve

    Returns Promise<TType>

    The cached value

    If the key is not found or has expired

  • The increment method increments the given key with given value. An error will thrown if the value is not a number.

    Parameters

    • key: string
    • value: Extract<TType, number> = ...

      If not defined then it will be defaulted to 1.

    Returns Promise<boolean>

    Returns true if the key where incremented otherwise false will be returned.

  • The incrementOrFail method increments the given key with given value. An error will thrown if the value is not a number or if the key is not found.

    Parameters

    • key: string
    • Optionalvalue: Extract<TType, number>

      If not defined then it will be defaulted to 1.

    Returns Promise<void>

  • Checks if a key does not exist in the cache.

    Parameters

    • key: string

      The cache key to check

    Returns Promise<boolean>

    true if the key is missing, false if it exists

  • The remove method removes the given key.

    Parameters

    • key: string

    Returns Promise<boolean>

    Returns true if the key is found otherwise false is returned.

  • The removeMany method removes many keys.

    Parameters

    • keys: string[]

    Returns Promise<boolean>

    Returns true if one of the keys where deleted otherwise false is returned.

  • The removeOrFail method removes the given key. Throws an error if the key is not found.

    Parameters

    • key: string

    Returns Promise<void>

  • The update method updates the given key with given value.

    Parameters

    Returns Promise<boolean>

    Returns true if the key where updated otherwise false will be returned.

  • The updateOrFail method updates the given key with given value. Thorws error if the key is not found.

    Parameters

    Returns Promise<void>