UNPKG

ioredis-cache

Version:

A promise-based cache package for Nodejs using IORedis

49 lines (48 loc) 2.21 kB
import Redis, { RedisKey, RedisOptions } from "ioredis"; interface Parser { parse: (text: string) => any; stringify: (value: any) => string; } interface CacheOptions { redis: RedisOptions | Redis; parser?: Parser; } declare type Key = RedisKey | number; declare type Map<T> = { [id: string]: T; }; declare type Awaitable<T> = T | Promise<T>; declare const NOT_FOUND_VALUE: undefined; declare type CacheReturn<T> = T | typeof NOT_FOUND_VALUE; declare class Cache { redis: Redis; protected prefix: string; protected parser: Parser; constructor(options: Redis | CacheOptions); static bindAll(target: Cache): Cache; cache<T>(key: Key, fn: () => Awaitable<T>, ttl?: number): Promise<T>; getCache<T>(key: Key): Promise<CacheReturn<T>>; setCache(key: Key, value: any, ttl?: number): Promise<"OK">; manyCache<K extends Key, T>(keys: K[], fn: (keys: K[]) => Awaitable<Map<T> | T[]>, prefix?: Key, ttl?: number): Promise<T[]>; getManyCache<T = any>(keys: Key[]): Promise<CacheReturn<T>[]>; setManyCache(valueMap: { [id: string]: any; }, ttl?: number): Promise<"OK" | [error: Error | null, result: unknown][] | null>; deleteCache(...keys: Key[]): Promise<number>; deletePattern(pattern: string, batch?: number): Promise<void>; hashCache<T>(key: Key, id: Key, fn: () => Awaitable<T>): Promise<T>; getHashCache<T>(key: Key, id: Key): Promise<CacheReturn<T>>; setHashCache(key: Key, id: Key, value: any): Promise<number>; hashManyCache<K extends Key, T>(key: Key, ids: K[], fn: (ids: K[]) => Awaitable<Map<T> | T[]>): Promise<T[]>; getHashManyCache<T>(key: Key, ids: Key[]): Promise<CacheReturn<T>[]>; setHashManyCache(key: Key, valueMap: { [id: string]: any; }): Promise<"OK" | never[]>; deleteHashCache(key: Key, ...ids: Key[]): Promise<number>; acquire<T>(key: Key, amount: number, fn: (current: number) => Awaitable<T>, float?: boolean): Promise<T>; hashAcquire<T>(key: Key, id: Key, amount: number, fn: (current: number) => Awaitable<T>, float?: boolean): Promise<T>; protected _buildSetParams: (valueMap: { [id: string]: any; }) => string[]; } export default Cache;