UNPKG

@hz-9/a5-cache

Version:

Cache module for the @hz-9/a5-* series of repositories, based on cache-manager.

184 lines (172 loc) 4.21 kB
/** * @packageDocumentation * A5 Cache Module - Cache management for A5 framework based on cache-manager */ import { A5CacheConstructorOptions as A5CacheConstructorOptions_2 } from '../interfaces'; import type { Cache as Cache_2 } from 'cache-manager'; import { ConfigurableModuleCls } from '@nestjs/common'; import type { Keyv } from 'keyv'; /** * @public */ export declare const A5_CACHE_MODULE_OPTIONS: string | symbol; /** * A5 缓存服务类 * * 提供统一的缓存操作接口,基于 cache-manager * * @public */ export declare class A5Cache { private readonly instance; constructor(options: A5CacheConstructorOptions); /** * 获取缓存值 * * @param key - 缓存键 * @returns 缓存值或 undefined */ get<T = unknown>(key: string): Promise<T | undefined>; /** * 设置缓存值 * * @param key - 缓存键 * @param value - 缓存值 * @param ttl - 过期时间(毫秒) */ set<T = unknown>(key: string, value: T, ttl?: number): Promise<void>; /** * 删除缓存 * * @param key - 缓存键 * @returns 是否删除成功 */ del(key: string): Promise<boolean>; /** * 批量删除缓存 * * @param keys - 缓存键数组 * @returns 是否删除成功 */ mdel(keys: string[]): Promise<boolean>; /** * 清空所有缓存 * * @returns 是否清空成功 */ clear(): Promise<boolean>; /** * 包装函数,自动处理缓存 * * 如果缓存存在则返回缓存值,否则执行函数并缓存结果 * * @param key - 缓存键 * @param fn - 获取值的函数 * @param options - 包装选项 * @returns 缓存值或函数执行结果 */ wrap<T>(key: string, fn: () => T | Promise<T>, options?: A5CacheWrapOptions<T>): Promise<T>; /** * 获取原始缓存实例 * * @returns 缓存实例 */ getInstance(): A5CacheInstance; } /** * @public */ export declare const A5CacheConfigurableModule: ConfigurableModuleCls<A5CacheConstructorOptions_2, "forRoot", "createOptions", { isGlobal: boolean; }>; /** * A5 缓存模块配置接口 * * @public */ export declare interface A5CacheConstructorOptions { /** * 缓存配置 * * 可以是单个存储配置或多个存储配置(用于多层缓存) */ stores?: Keyv[]; /** * 缓存 TTL(毫秒) */ ttl?: number; /** * 刷新阈值(毫秒) */ refreshThreshold?: number; /** * 是否刷新所有存储 * * @defaultValue false */ refreshAllStores?: boolean; /** * 是否非阻塞模式 * * @defaultValue false */ nonBlocking?: boolean; /** * 缓存 ID */ cacheId?: string; } /** * A5 缓存实例接口 * * 基于 cache-manager 的 Cache 类型 * * @public */ export declare type A5CacheInstance = Cache_2; /** * @public */ export declare class A5CacheModule extends A5CacheConfigurableModule { } /** * A5 缓存模块异步配置选项接口 * * @public */ export declare interface A5CacheModuleAsyncOptions { /** * 工厂函数,返回 A5CacheModuleOptions 配置对象 */ useFactory?: (...args: unknown[]) => Promise<A5CacheModuleOptions> | A5CacheModuleOptions; /** * 需要注入到 useFactory 函数中的依赖项 */ inject?: unknown[]; /** * 需要导入的模块列表 */ imports?: unknown[]; } /** * A5 缓存模块配置选项 * * @public */ export declare type A5CacheModuleOptions = A5CacheConstructorOptions; /** * A5 缓存包装选项接口 * * @public */ export declare interface A5CacheWrapOptions<T = unknown> { /** * 过期时间(毫秒)或计算函数 */ ttl?: number | ((value: T) => number); /** * 刷新阈值(毫秒)或计算函数 */ refreshThreshold?: number | ((value: T) => number); } export { }