• 如何给多参数接口添加缓存(根据方法名字+参数名生成对应的key)


    在我们日常开发过程中经常用到缓存,像一些简单的数据不经常改变,而且数据模型较为单一,我们可以直接调用Redis的方法即可进行缓存。

    但是。不是所有的查询接口都是简单的,也有很多复杂的参数以及携带分页的。这类数据我们如何通过Redis进行缓存呢?

    这里博主仅分享自已的方法,大家有更好的方法意见可以分享到评论区!

    这里我采用的方法是:根据方法名字+参数名生成对应的key进行缓存


    因为我们大多数参数都是中文的,为了防止放入Redis中乱码,我们需要把它先转为拼音。大家可以参考 Java汉字转拼音(解决方案) ,并在项目中引用该jar包以及方法。再进行后续的编码工作。

    写好上面的转换方法后我们编写一下key的生成规则 根据方法名字+参数名(拼音)生成对应的key

        /**
         * 根据方法名字+参数名生成对应的key
         *
         * @param map
         * @param method
         * @return
         */
        public static String createRediskey(Map<String, Object> map, String method) {
            String cacheKey;
            cacheKey = method;
            for (String key : map.keySet()) {
                if (null != map.get(key)) { //如果value不为空则拼接缓存名称
                    try {
                        if (isChinese(map.get(key).toString())){ //如果为汉字true
                            cacheKey = cacheKey + changeChinesePinyin(map.get(key).toString()).get("fullPinyin");
                        }else {
                            cacheKey = cacheKey + map.get(key).toString();
                        }
                    } catch (BadHanyuPinyinOutputFormatCombination e) {
                        throw new RuntimeException(e);
                    }
                }
            }
            return cacheKey;
        }
        
        /**
         * 为了防止除了汉字之外的字符出现我们通过正则过滤一下
         * @param str
         * @return
         */
        public static boolean isChinese(String str) {
            String regEx = "[\u4e00-\u9fa5]";
            Pattern pat = Pattern.compile(regEx);
            Matcher matcher = pat.matcher(str);
            boolean flg = false;
            if (matcher.find())
                flg = true;
    
            return flg;
        }
    
    • 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
    • 38
    • 39
    • 40
    • 41

    接下来我们直接再方法接口中调用该方法生成对应的key并进行缓存即可。

    • parameterMap 对应的是我们接口的参数有多少个参数对应put多少次
    • method 是我们的方法名字
        /**
         * 首页公告接口
         * @return
         */
        @GetMapping("/indexArticle")
        @RequiresAuthentication
        @ApiImplicitParams({@ApiImplicitParam(name = TOKEN_TAG, value = TOKEN_TAG, paramType = "header")})
        public Object indexArticle(String secondColumn , @RequestParam(value="current", required = false,defaultValue = "1") Integer current , @RequestParam(value="number", required = false,defaultValue = "10") Integer number){
    
            Map parameterMap =new HashMap();
            parameterMap.put("current",current);
            parameterMap.put("number",number);
            parameterMap.put("secondColumn",secondColumn);
    
            String key = createRediskey(parameterMap, "indexArticle");
            System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++缓存key值为:"+key);
            Map ca = (Map) RedisUtil.get(key);
            if (ca != null){
                System.out.println("已读取到缓存数据++++++++++++++++++++++++++++++++++++++++++++++++++++");
                return ApiResult.success(ca);
            }
            Map map= consultationsService.findList(current,number,secondColumn);
            redisUtil.set(key,map,18000); //五小时过期
            return ApiResult.success(map);
        }
    
    • 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

    经过测试,我们的接口调用后就会根据参数信息生成的key进行缓存。再次调用传相同的参数就会从redis中进行读取。

    在这里插入图片描述

  • 相关阅读:
    我们来聊聊锁升级吧
    工厂方法演进
    鸿蒙路由出错
    MySQL中常见错误详解
    LeetCode·76.最小覆盖子串·滑动窗口
    【MicroPython ESP32】ssd1306 0.96“OLED+气象数据中文显示
    展会预告 | 图扑邀您共聚 IOTE 国际物联网展·深圳站
    【疫情动态条形图】用Python开发全球疫情排名动态条形图bar_chart_race
    HDMI2.1与HDMI2.0的区别以及转换PD信号。
    【机器学习】——驱动智能制造的青春力量,优化生产、预见故障、提升质量
  • 原文地址:https://blog.csdn.net/weixin_45692705/article/details/127784830