@Override public Map<String, List<Catelog2Vo>> getCatelogJson() { //加入缓存逻辑 ValueOperations<String, String> ops = stringRedisTemplate.opsForValue(); String json = ops.get("CatalogJSON"); if(StringUtils.isEmpty(json)){ //缓存没有,从数据库中查询 Map<String, List<Catelog2Vo>> catalogJsonFromDb = getCatalogJsonFromDb(); //将查出的对象转为JSON放在数据库 :存json的好处,json是跨语言跨平台兼容的 ops.set("CatalogJSON", JSON.toJSONString(catalogJsonFromDb)); return catalogJsonFromDb; } //视频中是这样转然后返回的 Map<String, List<Catelog2Vo>> object = JSON.parseObject(json, new TypeReference<Map<String, List<Catelog2Vo>>>() {}); return (Map<String, List<Catelog2Vo>>) JSON.parse(json); } // @Cacheable(value = "category", key = "#root.methodName") //从数据库查询并封装数据 public Map<String, List<Catelog2Vo>> getCatalogJsonFromDb() { List<CategoryEntity> entityList = baseMapper.selectList(null); // 查询所有一级分类 List<CategoryEntity> level1 = getCategoryEntities(entityList, 0L); Map<String, List<Catelog2Vo>> parent_cid = level1.stream() .collect(Collectors.toMap( k -> k.getCatId().toString(), v -> { // 拿到每一个一级分类 然后查询他们的二级分类 List<CategoryEntity> entities = getCategoryEntities(entityList, v.getCatId()); List<Catelog2Vo> catelog2Vos = null; if (entities != null) { catelog2Vos = entities.stream().map(l2 -> { Catelog2Vo catelog2Vo = new Catelog2Vo(v.getCatId().toString(), l2.getName(), l2.getCatId().toString(), null); // 找当前二级分类的三级分类 List<CategoryEntity> level3 = getCategoryEntities(entityList, l2.getCatId()); // 三级分类有数据的情况下 if (level3 != null) { List<Catalog3Vo> 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.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <exclusions> <exclusion> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency>
这样就可以解决这个问题了。