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 { Namespace } from "@daiso-tech/core/namespace";
    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 add method adds a key with given value when key doesn't exists.

    Parameters

    • key: string

      can be a string or an Iterable of strings. If it's an Iterable, it will be joined into a single string. Think of an Iterable as representing a path.

    • value: TType
    • ttl: null | ITimeSpan = ...

      If null is passed, the item will not expire.

    Returns Task<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 key is not a number.

    Parameters

    • key: string

      can be a string or an Iterable of strings. If it's an Iterable, it will be joined into a single string. Think of an Iterable as representing a path.

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

      If not defined then it will be defaulted to 1.

    Returns Task<boolean>

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

    TypeError

  • The exists method returns true when key is found otherwise false will be returned.

    Parameters

    • key: string

      can be a string or an Iterable of strings. If it's an Iterable, it will be joined into a single string. Think of an Iterable as representing a path.

    Returns Task<boolean>

  • The get method returns the value when key is found otherwise null will be returned.

    Parameters

    • key: string

      can be a string or an Iterable of strings. If it's an Iterable, it will be joined into a single string. Think of an Iterable as representing a path.

    Returns Task<null | TType>

  • 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

      can be a string or an Iterable of strings. If it's an Iterable, it will be joined into a single string. Think of an Iterable as representing a path.

    Returns Task<null | TType>

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

    Parameters

    • key: string

      can be a string or an Iterable of strings. If it's an Iterable, it will be joined into a single string. Think of an Iterable as representing a path.

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

      If not defined then it will be defaulted to 1.

    Returns Task<boolean>

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

    TypeError

  • The missing method returns true when key is not found otherwise false will be returned.

    Parameters

    • key: string

      can be a string or an Iterable of strings. If it's an Iterable, it will be joined into a single string. Think of an Iterable as representing a path.

    Returns Task<boolean>

  • The put method replaces th given key with the given value and ttl if the key exists othwerwise it will add the given value with the given ttl.

    Parameters

    • key: string

      can be a string or an Iterable of strings. If it's an Iterable, it will be joined into a single string. Think of an Iterable as representing a path.

    • value: TType
    • ttl: null | ITimeSpan = ...

      If null is passed, the item will not expire.

    Returns Task<boolean>

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

  • The remove method removes the given key.

    Parameters

    • key: string

      can be a string or an Iterable of strings. If it's an Iterable, it will be joined into a single string. Think of an Iterable as representing a path.

    Returns Task<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 Task<boolean>

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

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

    Parameters

    • key: string

      can be a string or an Iterable of strings. If it's an Iterable, it will be joined into a single string. Think of an Iterable as representing a path.

    • value: TType

    Returns Task<boolean>

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