Class Cache<TType>

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

Type Parameters

  • TType = unknown

Implements

Constructors

  • Type Parameters

    • TType = unknown

    Parameters

    Returns Cache<TType>

    import { SqliteCacheAdapter } from "@daiso-tech/core/cache/adapters";
    import { Serde } from "@daiso-tech/core/serde";
    import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/adapters"
    import Sqlite from "better-sqlite3";
    import { Cache } from "@daiso-tech/core/cache";
    import { KeyPrefixer } from "@daiso-tech/core/utilities";

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

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

Methods

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

    Parameters

    • key: OneOrMore<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 | TimeSpan = ...

      If null is passed, the item will not expire.

    Returns LazyPromise<boolean>

    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: OneOrMore<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 LazyPromise<boolean>

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

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

    Parameters

    • key: OneOrMore<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 LazyPromise<boolean>

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

  • 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: OneOrMore<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 | TimeSpan = ...

      If null is passed, the item will not expire.

    Returns LazyPromise<boolean>

    true if the key where replaced otherwise false is returned.

  • The removeMany method removes many keys.

    Parameters

    • keys: Iterable<OneOrMore<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 LazyPromise<boolean>

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

MMNEPVFCICPMFPCPTTAAATR