1.0.1 • Published 3 years ago
ag-cache v1.0.1
ag-cache
基于TypeScript实现,可应用于React,Angular,Vue,JavaScript等web环境的缓存模块。更好的管理您的缓存信息,灵活的更换缓存所处的位置。
安装
yarn
yarn add ag-cache
or npm
npm install ag-cache
基础文档
实例化 CacheModule
new Cache(CacheOptions)
CacheOptions
属性 | 类型 | 默认值 | 描述 |
---|---|---|---|
key | string | 关联该cache的唯一值,例如可以是用户ID | |
defaultExpires | number | 200 | 毫秒单位,指定默认的缓存超时时间,0则不超时 |
defaultLevel | 'RAM', 'COOKIE', 'SESSION_STORAGE', 'LOCAL_STORAGE' | 'RAM' | 默认的缓存级别 |
defaultCollectorInterval | number | 30000 | 垃圾回收的执行间隔,只回收超时缓存 |
maximum | CacheMaximum | 可选 | 设置各级缓存最大内存占用,0则为不限制 |
debug | boolean | false | 启用debug调试模式 |
实例方法
set 设置缓存
使用:set(key, value, option): Promise<void>
参数名 | 类型 | 默认值 | 描述 |
---|---|---|---|
key | string | 缓存对应的唯一key | |
value | CacheValue | 缓存值 | |
option | {level, expires} | 默认为CacheOptions所对应的defaultExpires及defaultLevel | 设置缓存的级别与超时时间 |
get 读取缓存
使用:get<T = any>(key, level): T
参数名 | 类型 | 默认值 | 描述 |
---|---|---|---|
key | string | 缓存对应的key | |
level | 'RAM', 'COOKIE', 'SESSION_STORAGE', 'LOCAL_STORAGE' | 可选 | 如果没有指定则会从所有级别缓存内找到首个匹配项返回,如果指定则从指定级别缓存内查询 |
delete 移除指定key的缓存信息
使用:delete(key, level): void
参数名 | 类型 | 默认值 | 描述 |
---|---|---|---|
key | string | 缓存对应的key | |
level | 'RAM', 'COOKIE', 'SESSION_STORAGE', 'LOCAL_STORAGE' | 可选 | 如果没有指定则会从所有级别缓存内移除首个匹配项,如果指定则从指定级别缓存内移除匹配项 |
clear 清除指定级别的缓存信息
使用: clear(level: CacheLevel): void
参数名 | 类型 | 默认值 | 描述 |
---|---|---|---|
level | 'RAM', 'COOKIE', 'SESSION_STORAGE', 'LOCAL_STORAGE' | 清空指定级别缓存内的所有缓存信息 |
clearAll 清除所有缓存信息
使用: clearAll(): void