method CacheService.prototype.getOrSet
Since 0.5.0
CacheService.prototype.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.

Type Parameters

Value type associated with key

Parameters

key: string

Cache key without the service prefix

factory: () => Promise<T>

Work that produces a value when the key is absent

optional
ttlSeconds: number

Optional TTL override for the produced value

Return Type

Promise<T>

The cached or newly-produced value

Usage

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