缓存:改造三级分类业务





@Override
    public Map> getCatelogJson() {
        //加入缓存逻辑
        ValueOperations ops = stringRedisTemplate.opsForValue();
        String json = ops.get("CatalogJSON");
        if(StringUtils.isEmpty(json)){
            //缓存没有,从数据库中查询
            Map> catalogJsonFromDb = getCatalogJsonFromDb();
            //将查出的对象转为JSON放在数据库  :存json的好处,json是跨语言跨平台兼容的
            ops.set("CatalogJSON", JSON.toJSONString(catalogJsonFromDb));
            return catalogJsonFromDb;
        }

        //视频中是这样转然后返回的
        Map> object
                = JSON.parseObject(json, new TypeReference>>() {});

        return (Map>) JSON.parse(json);
    }



    //    @Cacheable(value = "category", key = "#root.methodName")

    //从数据库查询并封装数据
    public Map> getCatalogJsonFromDb() {



        List entityList = baseMapper.selectList(null);
        // 查询所有一级分类
        List level1 = getCategoryEntities(entityList, 0L);

        Map> parent_cid
                = level1.stream()
                .collect(Collectors.toMap(
                        k -> k.getCatId().toString(),
                        v -> {
                            // 拿到每一个一级分类 然后查询他们的二级分类
                            List entities = getCategoryEntities(entityList, v.getCatId());

                            List catelog2Vos = null;
                            if (entities != null) {
                                catelog2Vos = entities.stream().map(l2 -> {
                                    Catelog2Vo catelog2Vo = new Catelog2Vo(v.getCatId().toString(), l2.getName(), l2.getCatId().toString(), null);
                                    // 找当前二级分类的三级分类
                                    List level3 = getCategoryEntities(entityList, l2.getCatId());
                                    // 三级分类有数据的情况下
                                    if (level3 != null) {
                                        List catalog3Vos
                                                = level3.stream()
                                                .map(l3 -> new Catalog3Vo(l3.getCatId().toString(), l3.getName(), l2.getCatId().toString()))
                                                .collect(Collectors.toList());
                                        catelog2Vo.setCatalog3List(catalog3Vos);
                                    }
                                    return catelog2Vo;
                                }).collect(Collectors.toList());
                            }
                            return catelog2Vos;
                        }));
        return parent_cid;
    }

redis客户端查看也能查到数据

浏览器访问很正常,但是jmeter性能压测就出现问题了,产生堆外内存溢出异常。

====================================

重新更改依赖

  <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-redisartifactId>
            <exclusions>
                <exclusion>
                    <groupId>io.lettucegroupId>
                    <artifactId>lettuce-coreartifactId>
                exclusion>
            exclusions>
        dependency>

        <dependency>
            <groupId>redis.clientsgroupId>
            <artifactId>jedisartifactId>
        dependency>

这样就可以解决这个问题了。