当前位置: 首页 > article >正文

接口优化:查询企业额度代码优化

方案一、20分钟内没请求则会重新查询一次;20分钟内有请求则会走最小5分钟的异步更新

    public long getleadsSurplusCountFast(String companyId) {
    	// 锁key
        String lockKey = String.format("getleadsSurplusCountFastLock:%s", companyId);
        // 缓存值key
        String key = String.format("getleadsSurplusCountFast:%s", companyId);
        // 异步更新key
        String asyncKey = String.format("getleadsSurplusCountFastAsync:%s", companyId);
        RLock lock = redissonClient.getLock(lockKey);
        try {
            lock.lock();
            String cacheValue = stringRedisTemplate.opsForValue().get(key);
            if (stringRedisTemplate.hasKey(asyncKey)) {
                if (StrUtil.isNotBlank(cacheValue)) {
                    return Long.parseLong(cacheValue);
                } else {
                    log.info("getleadsSurplusCountFast直接通过数据库查询:{}", companyId);
                    return cacheSurplusCount(companyId, key, asyncKey);
                }
            }else {
                if (StrUtil.isBlank(cacheValue)) {
                    return cacheSurplusCount(companyId, key, asyncKey);
                }
                // 异步更新缓存
                String requestId = MDC.get(MDC_REQUEST_ID);
                queryUserUpperLimitPool.submit(
                        () -> {
                            MDC.put(MDC_REQUEST_ID, requestId);
                            log.info("getleadsSurplusCountFast异步通过数据库查询更新:{}", companyId);
                            cacheSurplusCount(companyId, key, asyncKey);
                            MDC.remove(MDC_REQUEST_ID);
                        }
                );
                return Long.parseLong(cacheValue);
            }
        } finally {
            lock.forceUnlock();
        }
    }

缓存代码

    private long cacheSurplusCount(String companyId, String key, String asyncKey) {
    	// 查询企业总额度
        long leadsSurplusCount = this.leadsSurplusCount(companyId);
        stringRedisTemplate.opsForValue().set(key, String.valueOf(leadsSurplusCount), Duration.ofMinutes(20));
        stringRedisTemplate.opsForValue().set(asyncKey, "0", Duration.ofMinutes(5));
        return leadsSurplusCount;
    }

方案二、查询企业总额度除以100,当额度小于100时为实时查询,当计算出的缓存时间小于5秒时至少缓存5秒

    public long getleadsSurplusCountByReduce(String companyId){
        String lockKey = String.format("getleadsSurplusCountByReduceLock:%s", companyId);
        String valueKey = String.format("getleadsSurplusCountByReduce:%s", companyId);
        RLock lock = redissonClient.getLock(lockKey);
        try {
            lock.lock();
            String cacheValue = stringRedisTemplate.opsForValue().get(valueKey);
            if (StrUtil.isNotBlank(cacheValue)) {
                return Long.parseLong(cacheValue);
            } else {
                long leadsSurplusCount = this.leadsSurplusCount(companyId);
                if (leadsSurplusCount < 100) {
                    log.info("企业剩余额度告警:{},剩余额度:{}", companyId, leadsSurplusCount);
                    return leadsSurplusCount;
                }
                long residueCount = leadsSurplusCount / 100;
                if (residueCount < 5) {
                    residueCount = 5;
                }
                log.info("企业剩余额度缓存:{},额度:{},缓存时间(单位s):{}", companyId, leadsSurplusCount, residueCount);
                stringRedisTemplate.opsForValue().set(valueKey, String.valueOf(leadsSurplusCount), Duration.ofSeconds(residueCount));
                return leadsSurplusCount;
            }

        } finally {
            lock.forceUnlock();
        }
    }

http://www.kler.cn/a/272851.html

相关文章:

  • 01.17周五F34-Day58打卡
  • 51单片机——DS18B20温度传感器
  • excel仅复制可见单元格,仅复制筛选后内容
  • 网管平台(进阶篇):路由器的管理实践
  • 【Unity】unity3D 调用LoadSceneAsync 场景切换后比较暗 部门材质丢失
  • 闲谭SpringBoot--ShardingSphere分布式事务探究
  • 做老阳推荐的蓝海项目有成功的吗?
  • 【nfs报错】rpc mount export: RPC: Unable to receive; errno = No route to host
  • 英语单词--痛苦
  • web高可用集群(lvs负载均衡+keepalved高可用)
  • OpenvSwitch 配置 Trunk 端口实验
  • 人工智能需要的数学基础有哪些?
  • opc客户端
  • DARTS: DIFFERENTIABLE ARCHITECTURE SEARCH
  • linux系统kubernetes的pod的状态
  • 【vue baidu-map】实现百度地图展示基地,鼠标悬浮标注点展示详细信息
  • C语言深入理解指针(4)
  • KD-Tree的原理及其在KNN中的应用(附Python代码)
  • flask 继续学习
  • 微信小程序小案例实战
  • c语言大小写字母的转换
  • 用C语言链表实现图书管理
  • 23.1 微服务理论基础
  • 本人遇到大数据面试题和参考答案(超过1万字精华版)
  • LLM大模型统一封装接口解决方案
  • 论文笔记:Llama 2: Open Foundation and Fine-Tuned Chat Models