class CacheService
implements ICacheStore
Since 0.1.0

Service layer that delegates to a backend CacheStore while applying:

  • Key prefix: Prepended to all keyed operations (get/set/delete/ has). The prefix is also passed to the backend at construction so that clear() can scope to it.
  • Default TTL: Used when set() is called without ttlSeconds.

Constructors

CacheService(
backend: CacheStore,
prefix: string,
defaultTtl?: number
)
Parameters
backend: CacheStore

The CacheStore backend implementation

prefix: string

Key prefix prepended to all keyed operations. Also passed to the backend constructor for clear() scoping.

optional
defaultTtl: number

Default TTL in seconds applied when set() omits ttlSeconds

Methods

clear(): Promise<void>

Delegates to the backend's clear(), which uses the construction-time prefix to scope the deletion. CacheService does not prepend a key here since clear() takes no key argument.

delete(key: string): Promise<boolean>

Removes a cached value.

get<T>(key: string): Promise<T | null>

Reads a cached value.

Since 0.5.0
getOrSet<T>(
key: string,
factory: () => Promise<T>,
ttlSeconds?: number
): Promise<T>

Reads a cached value or produces and stores it once for all concurrent callers of this service and key.

The in-flight registry is cleared after either success or failure. A rejected factory therefore leaves no poisoned entry and the next caller gets a fresh attempt. The factory result is written with the same explicit or default TTL resolution as set.

has(key: string): Promise<boolean>

Reports whether a live entry exists.

set<T>(
key: string,
value: T,
ttlSeconds?: number
): Promise<void>

Stores a value.

Usage

import { CacheService } from "cache-plugin/src/index.ts";