SpringBoot2.x整合Redis 
-  
引入maven依赖   <dependency>
 ? <groupId>org.springframework.boot</groupId>
 ? <artifactId>spring-boot-starter-data-redis</artifactId>
 </dependency>
   -  
配置  spring:
  redis:
    host: 127.0.0.1
    port: 6379
   -  
注入RedisTemplate  @Autowired
private StringRedisTemplate redisTemplate;
   -  
redis的使用  public Map<String, List<Category2Vo>> getCatalogJson() {
 ? ? ? ?// 给缓存中放入json字符串,拿出的json字符串,还能逆转为能用的对象类型【序列化与反序列化】
       ?// 加入缓存逻辑
 ? ? ? ?String catalogJson = redisTemplate.opsForValue().get("catalogJson");
?
 ? ? ? ?if (StringUtils.isEmpty(catalogJson)) {
 ? ? ? ? ? ?// 查询数据库
 ? ? ? ? ? ?Map<String, List<Category2Vo>> catalogJsonFromDb = getCatalogJsonFromDb();
 ? ? ? ? ? ?// 将数据转换为Json加入到缓存中
 ? ? ? ? ? ?String jsonString = JSON.toJSONString(catalogJsonFromDb);
 ? ? ? ? ? ?redisTemplate.opsForValue().set("catalogJson", jsonString);
 ? ? ? ? ? ?System.out.println("查询了数据库...");
 ? ? ? ? ? ?return catalogJsonFromDb;
 ? ? ?  }
        
        // 转换为指定的对象
 ? ? ? ?Map<String, List<Category2Vo>> listMap = JSON.parseObject(catalogJson, new TypeReference<Map<String, List<Category2Vo>>>() {});
 ? ? ? ?return listMap;
 ?  }
   -  
注意:使用JMeter压测时产生堆外内存溢出问题:OutOfDirectMemoryError  
     
                
                
                
        
        
    
 
 |