quick.cache 缓存
封装库 cache
缓存, 会检查当前环境是否支持storage
存储, 比如苹果的无痕浏览器,不支持storage
存储,将自动转为cookie
存储
quick.cache 使用
// 项目引入
import { Cache } from 'quick.lib';
1
2
2
>{}
/**
* new Cache 构造
* @param {json} _opts 初始化配置 {mode:1_标准,2_cookie,key:应用的key,name:存储的主键名}
*/
const cache = new Cache({
// 存储前缀
pre: '$docs',
// 存储应用的key
key: '123',
// 存储的主键key名
name: 'test',
});
// 默认不配置参数构造
//const cache = new Cache();
let data = {
id: 10192,
name: '测试存储',
};
let obj = {
// 取出当前缓存对象的配置参数
// opts: cache.getOpts(),
// 当前支持的存储方式
mode: cache.getOpts().mode === 1 ? 'h5存储' : 'cookie存储',
set: cache.setCache(data),
val: cache.getCache(),
};
// 读取存储 true:取出存储的格式内容,默认为空,只取出存储内容
//obj.all: cache.getCache(true),
// 删除当前存储
//obj.del=cache.removeCache();
return obj;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
cache cookie localStorage 存储
- iPhone 11 以下手机 wifi 重定向弹出浏览器不支持 localStorage
/**
* new Cache 构造
* @param {json} _opts 初始化配置 {mode:1 localStorage,2 cookie,key:应用的key,name:存储的主键名}
*/
const cache = new Cache({
// 指定存储到 1:localStorage,2:cookie
mode: 2,
// 存储前缀
pre: '$docs',
// 存储应用的key
key: '123',
// 存储的主键key名
name: 'test',
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
getOpts
读取缓存配置参数
/**
* 当前缓存的参数
*/
cache.getOpts() {}
1
2
3
4
2
3
4
setOpts
更新缓存配置参数
/**
* 更新缓存参数
* @param {*} _opts
*/
cache.setOpts(_opts) {}
1
2
3
4
5
2
3
4
5
setCache
写入缓存数据
/**
* 缓存格式写入
* @param {object} data 写入内容
* @param {object} type 存储类型,为空默认getType判断
* 存储的格式化{data:'存储的内容',type:'存储内容的类型',ts:存储的时间戳}
*/
cache.setCache(data, type) {}
1
2
3
4
5
6
7
2
3
4
5
6
7
getCache
读取缓存内容
/**
* 取出存储内容
* @param {boolean} all 为空只取出格式化内容,或取出全部存储格式化内容
*/
cache.getCache(all) {}
1
2
3
4
5
2
3
4
5
removeCache
删除指定内容
/**
* 删除指定内容
* @param {*} key 自定义key,为空默认opts.name
*/
cache.removeCache(key) {}
1
2
3
4
5
2
3
4
5
setSession
写入缓存会话
/**
* 写入当前会话
* @param {object} data 写入内容
* @param {object} type 存储类型,默认为空
* 存储的格式化{key,{data:'实际存储的内容',t:存储的时间戳}}
*/
cache.setSession(data, type) {}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
removeSession()
删除缓存会话
/**
* 删除当前会话
* @param {*} key 自定义key,为空默认opts.name
*/
cache.removeSession(key) {}
1
2
3
4
5
2
3
4
5
getSession()
读取缓存会话
/**
* 取出当前会话
* @param {boolean} all 只取出实际内容,或取出存储格式全部
*/
cache.getSession(all) {}
1
2
3
4
5
2
3
4
5