缓存数据统一调用模块,支持不同实例使用不同的缓存类型,目前支持 localStorage、sessionStorage、内存缓存。
type缓存类型,可直接传入类型值或者通过import {CacheType} from "@x-drive/cache"后使用CacheType中的名称指定,默认使用sessionStorage0localStorage1sessionStorage2内存。内存型的缓存在第一次被实例化的时候才会被注册到模块中,因此使用者可以在一开始的时候使用register注册自己的内存型缓存,默认内存缓存的取值是2
expires全局过期时间prefix缓存key前缀maxStack限制上限
set(key: string, value: any, conf?: DataConf): this;
key数据键值value数据conf数据缓存配置expires单条数据过期时间conditions缓存生效条件
Cache.set("test", 123456);get(key: string): any;
key存储数据的键值
Cache.get("test");del(key: string): this;
key存储数据的键值
Cache.del("test");once(key: string, value: any, conf?: DataConf): this;
配置中的 once 字段会被强制设置为 true
key数据键值value数据conf数据缓存配置expires单条数据过期时间conditions缓存生效条件
Cache.once("test", 123456);has(key: string, validity: boolean = false): boolean;
key存储数据的键值validity是否进行数据有效性校验
Cache.has("test");tidy(): this;
Cache.tidy();模块提供了 register 方法用于注册新的缓存模块
import Cache, { CacheType, register } from "@x-drive/cache";
// 业务使用的特殊缓存
import TaroCache from "@components/cache/@mods/t-cache";
// 注册缓存
register("taro", TaroCache);
// 使用
const TaroCache = new Cache({
// 通过 CacheType 指定使用的缓存类型
"type": CacheType.taro
});