UNPKG

@soundxyz/response-cache

Version:

Heavily inspired by @envelop/response-cache

82 lines (81 loc) 3 kB
import type { Redis } from "ioredis"; import type RedLock from "redlock"; import type { Settings } from "redlock"; import type { Cache } from "./plugin"; export type BuildRedisEntityId = (typename: string, id: number | string) => string; export type BuildRedisOperationResultCacheKey = (responseId: string) => string; export declare const Events: { readonly REDIS_GET: "REDIS_GET"; readonly REDIS_GET_TIMED_OUT: "REDIS_GET_TIMED_OUT"; readonly REDIS_SET: "REDIS_SET"; readonly INVALIDATE_KEY_SCAN: "INVALIDATE_KEY_SCAN"; readonly INVALIDATED_KEYS: "INVALIDATED_KEYS"; readonly CONCURRENT_CACHED_CALL_HIT: "CONCURRENT_CACHED_CALL_HIT"; readonly REDLOCK_ACQUIRED: "REDLOCK_ACQUIRED"; readonly REDLOCK_RELEASED: "REDLOCK_RELEASED"; readonly REDLOCK_GET_AFTER_ACQUIRE: "REDLOCK_GET_AFTER_ACQUIRE"; }; export type Events = (typeof Events)[keyof typeof Events]; export type LogEventArgs = { message: string; code: Events; params: EventParamsObject; }; export type LoggedEvents = Partial<Record<Events, string | boolean | null | ((args: LogEventArgs) => void)>>; export type EventParamsObject = Record<string, string | number | boolean | null | undefined>; export type RedisCacheParameter = { /** * Redis instance * @see Redis.Redis https://github.com/luin/ioredis */ redis: Redis; /** * Enable and customize redlock */ redlock?: { client: RedLock; /** * @default 5000 ms */ duration?: number; settings?: Partial<Settings>; } | null; /** * Customize how the cache entity id is built. * By default the typename is concatenated with the id e.g. `User:1` */ buildRedisEntityId?: BuildRedisEntityId; /** * Customize how the cache key that stores the operations associated with the response is built. * By default `operations` is concatenated with the responseId e.g. `operations:arZm3tCKgGmpu+a5slrpSH9vjSQ=` */ buildRedisOperationResultCacheKey?: BuildRedisOperationResultCacheKey; /** * Debug TTL of existing cache results * * @default false */ debugTtl?: boolean; /** * Enable event logging */ logEvents?: { events: LoggedEvents; /** * @default console.log */ log?: (args: LogEventArgs) => void; }; /** * Set a maximum amount of milliseconds for redis gets to wait for the GET redis response */ GETRedisTimeout?: number; onError?: (err: unknown) => void; /** * @default 20 */ concurrencyLimit?: number; }; export declare const createRedisCache: ({ redis: store, redlock, debugTtl, logEvents, buildRedisEntityId, buildRedisOperationResultCacheKey, GETRedisTimeout, onError, concurrencyLimit, }: RedisCacheParameter) => Cache; export declare const defaultBuildRedisEntityId: BuildRedisEntityId; export declare const defaultBuildRedisOperationResultCacheKey: BuildRedisOperationResultCacheKey;