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

Accessors

Methods

  • The add method adds a key with given value when key doesn't exists.

    Parameters

    • key: string
    • value: TType
    • Optionalsettings: CacheWriteSettings
      • Optionaljitter?: number

        Random jitter factor (0-1) to add variance to expiration times. Prevents thundering herd problems when many entries expire simultaneously. A value of 0.1 adds ±10% randomness to the TTL.

      • Optionalttl?: ITimeSpan | null

        Time-to-live (TTL) duration for cached entries. When set, entries will automatically expire after this duration. Pass null to cache entries without automatic expiration.

    Returns Promise<boolean>

    Returns true when key doesn't exists otherwise false will be returned.

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

  • 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 put methods upsert the given key and replaces the ttl when updated.

    Parameters

    • key: string
    • value: TType
    • Optionalsettings: CacheWriteSettings
      • Optionaljitter?: number

        Random jitter factor (0-1) to add variance to expiration times. Prevents thundering herd problems when many entries expire simultaneously. A value of 0.1 adds ±10% randomness to the TTL.

      • Optionalttl?: ITimeSpan | null

        Time-to-live (TTL) duration for cached entries. When set, entries will automatically expire after this duration. Pass null to cache entries without automatic expiration.

    Returns Promise<boolean>

    Returns true if the key where replaced otherwise false is returned.

  • 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: Iterable<string, any, any>

      The param items can be a string or an Iterable of strings. If the param items are an Iterable, it will be joined into a single string. Think of an Iterable as representing a path.

    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.