UNPKG

iweb-cache

Version:

轻量且强悍的 Cache库, 为您暴露丰富而统一的 API, 使得业务调用更加简便.

102 lines (98 loc) 2.89 kB
declare type storageType = 'localStorage' | 'sessionStorage'; declare type cacheNotValueType = false; declare type typeStr = 'number' | 'null' | 'undefined' | 'boolean' | 'array' | 'string' | 'object' | 'bigint' | 'symbol' | 'function' | 'regexp'; declare type mergeType<T> = T; declare type getType<T> = mergeType<T | cacheNotValueType | string | undefined>; interface IglobalVariableData { [key: string]: ICacheData; } interface IcacheOptions { /** * @description 使用哪种方式进行数据存储, 默认值 localStorage * @type { string } */ storageType?: storageType; /** * @description 储存数据的前缀 * @params { string } iweb-cache */ prefix?: string; /** * @description 全局过期时间 * @params { number } 单位毫秒 */ expires?: number; } interface ICacheData { /** * @description 存储的数据 * @params { string | number | object } */ value: string; /** * @description 单个数据的过期时间 * @params { number } 单位秒 */ expires?: number; /** * @description 储存数据的原始类型 */ valueType: typeStr; } interface IwebCache { appData: IglobalVariableData; /** * @description 设置缓存 */ save<T = any>(key: string, value: T, expires?: number): boolean; /** * @description 覆盖缓存数据,但不更新过期时间 */ cover(key: string, value: any): boolean; /** * @description 根据指定key获取缓存数据 */ get<T = any>(key: string): getType<T>; /** * @description 获取多条缓存数据 * @example * const [ token,token1 ] = cache.getAll('token','token1') */ getAll(...arg: string[]): [any]; /** * @description 获取缓存内容的大小 */ getCacheSize(): number | boolean; /** * @description 根据key删除指定缓存数据 */ del(key: string): boolean; /** * @description 清空全部缓存数据 */ clear(): boolean; } interface IclientStore { storage: Storage; options: IcacheOptions; save(value: IglobalVariableData): void; get(key: string): string | null; del(key: string): void; clear(): void; getCacheData(): IglobalVariableData; } declare class WebCache implements IwebCache { appData: IglobalVariableData; private clientStore; constructor(clientStore: IclientStore); save(key: string, value: any, expires?: number): boolean; get<T = any>(key: string): getType<T>; getAll(...arg: string[]): [any]; del(key: string): boolean; clear(): boolean; cover(key: string, value: any): boolean; getExpires(expires?: number): number; getCacheSize(): number | false; } declare function export_default(options?: IcacheOptions): WebCache; export default export_default;