UNPKG

@solomonai/cache

Version:

<div align="center"> <h1 align="center">@solomonai/cache</h1> <h5>Cache all the things</h5> </div>

132 lines (124 loc) 4.8 kB
import { C as CacheError, S as Store, E as Entry } from './interface-D4FnSgmS.js'; import { Result } from '@unkey/error'; export { M as Metrics } from './metrics-PJjLxZ-U.js'; interface Context { waitUntil: (p: Promise<unknown>) => void; } declare class DefaultStatefulContext implements Context { waitUntil<TPromise = unknown>(_p: Promise<TPromise>): void; } interface CacheNamespace<TValue> { /** * Return the cached value * * The response will be `undefined` for cache misses or `null` when the key was not found in the origin * */ get: (key: string) => Promise<Result<TValue | undefined, CacheError>>; /** * Sets the value for the given key. */ set: (key: string, value: TValue, opts?: { fresh: number; stale: number; }) => Promise<Result<void, CacheError>>; /** * Removes one or multiple keys from the cache. */ remove: (key: string | string[]) => Promise<Result<void, CacheError>>; /** * Pull through cache */ swr(key: string, refreshFromOrigin: (key: string) => Promise<TValue | undefined>): Promise<Result<TValue | undefined, CacheError>>; } type Cache<TNamespaces extends Record<string, unknown>> = { [TName in keyof TNamespaces]: CacheNamespace<TNamespaces[TName]>; }; /** * Internal cache implementation for an individual namespace */ declare class SwrCache<TNamespace extends string, TValue> { private readonly ctx; private readonly store; private readonly fresh; private readonly stale; /** * To prevent concurrent revalidation of the same data, all revalidations are deduplicated using * this map. */ private readonly revalidating; constructor(ctx: Context, store: Store<TNamespace, TValue | undefined>, fresh: number, stale: number); /** * Return the cached value * * The response will be `undefined` for cache misses or `null` when the key was not found in the origin */ get(namespace: TNamespace, key: string): Promise<Result<TValue | undefined, CacheError>>; private _get; /** * Set the value */ set(namespace: TNamespace, key: string, value: TValue | undefined, opts?: { fresh: number; stale: number; }): Promise<Result<void, CacheError>>; /** * Removes the key from the cache. */ remove(namespace: TNamespace, key: string): Promise<Result<void, CacheError>>; swr(namespace: TNamespace, key: string, loadFromOrigin: (key: string) => Promise<TValue | undefined>): Promise<Result<TValue | undefined, CacheError>>; /** * Deduplicating the origin load helps when the same value is requested many times at once and is * not yet in the cache. If we don't deduplicate, we'd create a lot of unnecessary load on the db. */ private deduplicateLoadFromOrigin; } declare class Namespace<TValue> extends SwrCache<string, TValue> { constructor(ctx: Context, opts: { stores: Array<Store<string, TValue>>; fresh: number; stale: number; }); } declare function createCache<TNamespaces extends Record<string, unknown>, TNamespace extends keyof TNamespaces = keyof TNamespaces>(namespaces: { [TName in keyof TNamespaces]: Namespace<TNamespaces[TName]>; }): Cache<TNamespaces>; /** * TieredCache is a cache that will first check the memory cache, then the zone cache. */ declare class TieredStore<TNamespace extends string, TValue> implements Store<TNamespace, TValue> { private ctx; private readonly tiers; readonly name = "tiered"; /** * Create a new tiered store * Stored are checked in the order they are provided * The first store to return a value will be used to populate all previous stores * * * `stores` can accept `undefined` as members to allow you to construct the tiers dynamically * @example * ```ts * new TieredStore(ctx, [ * new MemoryStore(..), * process.env.ENABLE_X_STORE ? new XStore(..) : undefined * ]) * ``` */ constructor(ctx: Context, stores: (Store<TNamespace, TValue> | undefined)[]); /** * Return the cached value * * The response will be `undefined` for cache misses or `null` when the key was not found in the origin */ get(namespace: TNamespace, key: string): Promise<Result<Entry<TValue> | undefined, CacheError>>; /** * Sets the value for the given key. */ set(namespace: TNamespace, key: string, value: Entry<TValue>): Promise<Result<void, CacheError>>; /** * Removes the key from the cache. */ remove(namespace: TNamespace, key: string): Promise<Result<void, CacheError>>; } export { type Cache, CacheError, type Context, DefaultStatefulContext, Namespace, TieredStore, createCache };