UNPKG

@unito/integration-sdk

Version:

Integration SDK

68 lines (67 loc) 2.67 kB
import { FetchingFunction, CachableValue } from 'cachette'; /** * The Cache class provides caching capabilities that can be used across your integration. * It can be backed by a Redis instance (by passing it a URL to the instance) or a local cache. * * @see {@link Cache.create} */ export declare class Cache { private cacheInstance; private constructor(); /** * Get or fetch a value * * @param key The key of the value to get * @param ttl The time to live of the value in seconds. * @param fetchFn The function that can retrieve the original value * @param lockTtl Global distributed lock TTL (in seconds) protecting fetching. * If undefined, 0 or falsy, locking is not preformed * @param shouldCacheError A callback being passed errors, controlling whether * to cache or not errors. Defaults to never cache. * * @returns The cached or fetched value */ getOrFetchValue<F extends FetchingFunction = FetchingFunction>(key: string, ttl: number, fetcher: F, lockTtl?: number, shouldCacheError?: (err: Error) => boolean): Promise<ReturnType<F>>; /** * Get a value from the cache. * * @param key The key of the value to get. * * @return The value associated with the key, or undefined if * no such value exists. */ getValue(key: string): Promise<CachableValue>; /** * Set a value in the cache. * * @param key The key of the value to set. * @param value The value to set. * @param ttl The time to live of the value in seconds. * By default, the value will not expire * * @return true if the value was stored, false otherwise. */ setValue(key: string, value: CachableValue, ttl?: number): Promise<boolean>; /** * Delete a value from the cache. * @param key — The key of the value to set. */ delValue(key: string): Promise<void>; /** * Get the TTL of an entry, in ms * * @param key The key of the entry whose ttl to retrieve * * @return The remaining TTL on the entry, in ms. * undefined if the entry does not exist. * 0 if the entry does not expire. */ getTtl(key: string): Promise<number | undefined>; /** * Initializes a Cache backed by the Redis instance at the provided url if present, or a LocalCache otherwise. * * @param redisUrl - The redis url to connect to (optional). * @returns A cache instance. */ static create(redisUrl?: string): Cache; }