Type Alias ICacheAdapter<TType>

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

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

      • context: IReadableContext

        Readable execution context for the operation

      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

      • key: string

        Cache key to retrieve and remove

      • context: IReadableContext

        Readable execution context for the operation

      Returns Promise<null | TType>

      The cached value, or null if not found or expired

  • getOrAdd:function
    • 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 | TimeSpan

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

      • context: IReadableContext

        Readable execution context for the operation

      Returns Promise<TType>

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

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

      Parameters

      • key: string

        Cache key to increment

      • value: number

        Amount to increment by.

      • context: IReadableContext

        Readable execution context for the operation

      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

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

      • context: IReadableContext

        Readable execution context for the operation

      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

      • key: string

        Cache key to update

      • value: TType

        New value to cache

      • context: IReadableContext

        Readable execution context for the operation

      Returns Promise<boolean>

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