基于注解的Redis缓存实现

 5.2.2 基于注解的Redis缓存实现


​        在Spring Boot默认缓存管理的基础上引入Redis缓存组件,使用基于注解的方式讲解Spring Boot整合Redis缓存的具体实现 


   (1)添加Spring Data Redis依赖启动器。在pom.xml文件中添加Spring Data Redis依赖启动器 


```xml

       org.springframework.boot

       spring-boot-starter-data-redis

```


当我们添加进redis相关的启动器之后,

SpringBoot会使用`RedisCacheConfigratioin`当做生效的自动配置类进行缓存相关的自动装配,容器中使用的缓存管理器是

`RedisCacheManager`, 这个缓存管理器创建的Cache为 `RedisCache`, 进而操控redis进行数据的缓存


(2)Redis服务连接配置 


```properties

# Redis服务地址

spring.redis.host=127.0.0.1

# Redis服务器连接端口

spring.redis.port=6379

# Redis服务器连接密码(默认为空)

spring.redis.password=

```


(3)对CommentService类中的方法进行修改使用@Cacheable、@CachePut、@CacheEvict三个注解定制缓存管理,分别进行缓存存储、缓存更新和缓存删除的演示 


```java

@Service

public class CommentService {


   @Autowired

   private CommentRepository commentRepository;


   @Cacheable(cacheNames = "comment",unless ="#result==null")

   public Comment findCommentById(Integer id){

       Optional comment = commentRepository.findById(id);

       if(comment.isPresent()){

           Comment comment1 = comment.get();

           return comment1;

       }

       return null;

    }


   @CachePut(cacheNames = "comment",key = "#result.id")

   public Comment updateComment(Comment comment) {

      commentRepository.updateComment(comment.getAuthor(), comment.getaId());

       return comment;

    }


   @CacheEvict(cacheNames = "comment")

   public void deleteComment(int comment_id) {

       commentRepository.deleteById(comment_id);

    }


}

```


   以上使用@Cacheable、@CachePut、@CacheEvict注解在数据查询、更新和删除方法上进行了缓存管理。


   其中,查询缓存@Cacheable注解中没有标记key值,将会使用默认参数值comment_id作为key进行数据保存,在进行缓存更新时必须使用同样的key;同时在查询缓存@Cacheable注解中,定义了“unless = "#result==null"”表示查询结果为空不进行缓存 


(4)  基于注解的Redis查询缓存测试 


![image-20191231141712756](./images/image-20191231141712756.png)


​        可以看出,查询用户评论信息Comment时执行了相应的SQL语句,但是在进行缓存存储时出现了IllegalArgumentException非法参数异常,提示信息要求对应Comment实体类必须实现序列化(“DefaultSerializer

requires a Serializable payload but received an object of type”)。 


(5)将缓存对象实现序列化。


src="./images/image-20191231141845520.png"

alt="image-20191231141845520" style="zoom:67%;" />


(6)再次启动测试


​        访问“http://localhost:8080/findCommentById?id=1”查询id为1的用户评论信息,并重复刷新浏览器查询同一条数据信息,查询结果 


![image-20191231142056554](./images/image-20191231142056554.png)




 查看控制台打印的SQL查询语句 


![image-20191231142114105](./images/image-20191231142114105.png)


还可以打开Redis客户端可视化管理工具Redis Desktop Manager连接本地启用的Redis服务,查看具体的数据缓存效果 


![image-20191231142353290](./images/image-20191231142353290.png)




​     执行findById()方法查询出的用户评论信息Comment正确存储到了Redis缓存库中名为comment的名称空间下。其中缓存数据的唯一标识key值是以“名称空间comment::+参数值(comment::1)”的字符串形式体现的,而value值则是经过JDK默认序列格式化后的HEX格式存储。这种JDK默认序列格式化后的数据显然不方便缓存数据的可视化查看和管理,所以在实际开发中,通常会自定义数据的序列化格式 




(7) 基于注解的Redis缓存更新测试。


先通过浏览器访问“http://localhost:8080/update/1/shitou”更新id为1的评论作者名为shitou;


接着,继续访问“http://localhost:8080/get/1”查询id为1的用户评论信息 


src="./images/image-20191231144329586.png"

alt="image-20191231144329586" style="zoom:67%;" />




![image-20191231144533512](./images/image-20191231144533512.png)


​        可以看出,执行updateComment()方法更新id为1的数据时执行了一条更新SQL语句,后续调用findById()方法查询id为1的用户评论信息时没有执行查询SQL语句,且浏览器正确返回了更新后的结果,表明@CachePut缓存更新配置成功 




(8)基于注解的Redis缓存删除测试


​        通过浏览器访问“http://localhost:8080/deleteComment?id=1”删除id为1的用户评论信息;


![image-20191231145925740](./images/image-20191231145925740.png)


​        执行deleteComment()方法删除id为1的数据后查询结果为空,之前存储在Redis数据库的comment相关数据也被删除,表明@CacheEvict缓存删除成功实现 


​        通过上面的案例可以看出,使用基于注解的Redis缓存实现只需要添加Redis依赖并使用几个注解可以实现对数据的缓存管理。另外,还可以在Spring

Boot全局配置文件中配置Redis有效期,示例代码如下: 


```properties

对基于注解的Redis缓存数据统一设置有效期为1分钟,单位毫秒

spring.cache.redis.time-to-live=60000

```


​        上述代码中,在Spring Boot全局配置文件中添加了“spring.cache.redis.time-to-live”属性统一配置Redis数据的有效期(单位为毫秒),但这种方式相对来说不够灵活

刚学了拉勾教育的《Java工程师高薪训练营》,看到刚学到的点就回答了。希望拉勾能给我推到想去的公司,目标:字节!!

你可能感兴趣的:(基于注解的Redis缓存实现)