nodejs的缓存组件

缓存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)
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值