Type Alias ICacheAdapter<TType>

ICacheAdapter: {
    add(
        context: IReadableContext,
        key: string,
        value: TType,
        ttl: null | TimeSpan,
    ): Promise<boolean>;
    get(context: IReadableContext, key: string): Promise<null | TType>;
    getAndRemove(context: IReadableContext, key: string): Promise<null | TType>;
    increment(
        context: IReadableContext,
        key: string,
        value: number,
    ): Promise<boolean>;
    put(
        context: IReadableContext,
        key: string,
        value: TType,
        ttl: null | TimeSpan,
    ): Promise<boolean>;
    removeAll(context: IReadableContext): Promise<void>;
    removeByKeyPrefix(context: IReadableContext, prefix: string): Promise<void>;
    removeMany(context: IReadableContext, keys: string[]): Promise<boolean>;
    update(
        context: IReadableContext,
        key: string,
        value: TType,
    ): Promise<boolean>;
}

Low-level adapter contract for cache storage operations. Defines CRUD operations for key-value pairs with expiration support. This contract abstracts away the underlying cache storage technology (Redis, Memcached, database, etc.).

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

Type Parameters

  • TType = unknown

Type declaration

  • add:function
    • Creates a new cache entry, but only if the key does not already exist. Has no effect if the key already exists.

      Parameters

      • context: IReadableContext

        Readable execution context for the operation

      • key: string

        Cache key to add

      • value: TType

        Value to cache

      • ttl: null | TimeSpan

        Time-to-live duration for this entry. Pass null to cache without expiration.

      Returns Promise<boolean>

      true if the entry was created, false if the key already existed

  • get:function
  • getAndRemove:function
    • Retrieves a value by key and immediately removes it. Useful for one-time-use values (tokens, temporary states, etc.).

      Parameters

      • context: IReadableContext

        Readable execution context for the operation

      • key: string

        Cache key to retrieve and remove

      Returns Promise<null | TType>

      The cached value, or null if not found or expired

  • increment:function
    • Increments a numeric cache entry by a given amount. Useful for counters, rates, and statistics.

      Parameters

      • context: IReadableContext

        Readable execution context for the operation

      • key: string

        Cache key to increment

      • value: number

        Amount to increment by.

      Returns Promise<boolean>

      true if the entry was incremented, false if the key did not exist

      If the cached value is not a number

  • put:function
    • Creates a new cache entry or updates an existing one (upsert). Also updates the TTL when overwriting an existing entry.

      Parameters

      • context: IReadableContext

        Readable execution context for the operation

      • key: string

        Cache key to set

      • value: TType

        Value to cache

      • ttl: null | TimeSpan

        Time-to-live duration for this entry. Pass null to cache without expiration.

      Returns Promise<boolean>

      true if the entry was added, false if it was updated

  • removeAll:function
  • removeByKeyPrefix:function
  • removeMany:function
  • update:function
    • Updates an existing cache entry without changing its TTL. Has no effect if the key does not exist.

      Parameters

      • context: IReadableContext

        Readable execution context for the operation

      • key: string

        Cache key to update

      • value: TType

        New value to cache

      Returns Promise<boolean>

      true if the entry was updated, false if the key did not exist