缓存remotepinggu中相关的方法
-
安装依赖:npm install ds
-
ps:接口remotepinggu涉及的list接口
增删改查
if(metadata.rowCount>0){
let cacheKey = "diancihuanjing_key_"+params.jichang_name;
//--无法保证顺序,没有在内部做排序。
// 发生修改的方法:
cacheUtil.updateValueByList(cacheKey,"id",params.id,params);
// 发生删除的方法
cacheUtil.removeByValueKey(cacheKey,"id",params.id)
// 发生新增的方法
cacheUtil.addValueByList(cacheKey,params)
// 另一种方式(直接清除对应的键,让list方法里面自己去重新缓存):
cacheUtil.remove(cacheKey);
this.list(params.jichang_name);
}
list接口
// 判断有无缓存
let cacheKey = "diancihuanjing_key_"+jichang_name;
if(cacheUtil.get(cacheKey)!=null){
return cacheUtil.get(cacheKey)
}
// 查询数据
var sql = `select * from public.diancihuanjing where jichang_name = '${jichang_name}' order by name `;
var [results, metadata] = await sequelize.query(sql);
// 结果存入缓存
cacheUtil.set(cacheKey,results,0);
return results;
增加
if(results[0].id){
let cacheKey = "diancihuanjing_key_"+params.jichang_name;
// 发生新增的方法
cacheUtil.addValueByList(cacheKey,params)
}
if(metadata.rowCount == 1){
let cacheKey = "diancihuanjing_key_"+params.jichang_name;
// 发生删除的方法
cacheUtil.removeByValueKey(cacheKey,'id',id)
}
if(metadata.rowCount>0){
let cacheKey = "diancihuanjing_key_"+params.jichang_name;
// 发生修改的方法:
cacheUtil.updateValueByList(cacheKey,"id",params.id,params);
}
// 判断有无缓存
let cacheKey = "diancihuanjing_key_"+jichang_name;
let ss = cacheUtil.get(cacheKey);
if(cacheUtil.get(cacheKey)!=null){
return cacheUtil.get(cacheKey)
}
// 结果存入缓存
cacheUtil.set(cacheKey,results,0);
清除对应键的缓存
新增操作
if(results[0].id){
let cacheKey = "diancihuanjing_key_"+params.jichang_name;
// 清除指定key的缓存
cacheUtil.remove(cacheKey)
}
修改删除
if(metadata.rowCount == 1){
let cacheKey = "diancihuanjing_key_"+params.jichang_name;
// 清除指定key的缓存
cacheUtil.remove(cacheKey)
}