当前位置: 首页 > news >正文

Spring Boot整合Redis缓存并使用注解

Spring Boot整合Redis缓存并使用注解

在Spring Boot应用程序中,您可以使用Spring Cache库与Redis缓存进行集成,以提高应用程序的性能和响应速度。Spring Cache库提供了一组注解,包括@Cacheable@CachePut@CacheEvict,可以方便地将方法的返回值缓存到Redis中,并根据需要进行刷新和清除。

本篇博客将向您展示如何在Spring Boot项目中整合Redis缓存,并使用注解来管理缓存操作。

步骤1:添加依赖项

首先,在您的Spring Boot项目的pom.xml文件中添加必要的依赖项以使用Redis和Spring Cache:

<dependencies><!-- 其他依赖项 --><!-- Redis依赖项 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><!-- Spring Cache依赖项 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency>
</dependencies>

步骤2:配置Redis连接

接下来,您需要在application.propertiesapplication.yml配置文件中添加Redis连接的相关配置信息:

spring.redis.host=your-redis-host
spring.redis.port=your-redis-port
spring.redis.password=your-redis-password (如果有密码)

步骤3:启用缓存和Redis支持

在您的Spring Boot应用程序主类上添加@EnableCaching注解,以启用缓存支持:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;@SpringBootApplication
@EnableCaching
public class YourApplication {public static void main(String[] args) {SpringApplication.run(YourApplication.class, args);}}

步骤4:在方法上使用缓存注解

现在,您可以在需要缓存的方法上使用@Cacheable@CachePut@CacheEvict注解。

@Cacheable

@Cacheable注解用于缓存方法的返回值,并在后续调用时从缓存中获取结果,而不再执行实际的方法体。

示例:

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;@Service
public class YourService {@Cacheable("books")public Book findBookById(String id) {// 从数据库或其他数据源获取书籍信息return book;}}

在上述示例中,findBookById方法的返回值将被缓存到名为"books"的缓存中。当再次调用该方法时,将从缓存中获取结果,而不会执行方法体。

@CachePut

@CachePut注解用于将方法的返回值存储到缓存中,类似于@Cacheable注解,但它每次都会执行方法体。

示例:

import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;@Service
public class YourService {@CachePut("books")public Book updateBook(Book book) {// 更新数据库或其他数据源中的书籍信息return book;}}

在上述示例中,每次调用updateBook方法时,都会执行方法体,并将返回的书籍信息存储到名为"books"的缓存中。

@CacheEvict

@CacheEvict注解用于从缓存中移除指定的条目,可以在方法调用之前、之后或同时触发。

示例:

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;@Service
public class YourService {@CacheEvict("books")public void deleteBook(String id) {// 从数据库或其他数据源中删除书籍信息}}

在上述示例中,每次调用deleteBook方法时,都会从名为"books"的缓存中移除相应的条目。

步骤5:使用缓存注解进行方法缓存

在步骤4中,我们已经介绍了@Cacheable@CachePut@CacheEvict注解的基本用法。现在,让我们更详细地了解这些注解的使用方法。

@Cacheable

@Cacheable注解可用于方法级别,用于指定方法的返回值应该被缓存起来。可以使用value属性指定要使用的缓存名称,还可以使用key属性来定义缓存的键。

示例:

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;@Service
public class YourService {@Cacheable(value = "books", key = "#id")public Book findBookById(String id) {// 从数据库或其他数据源获取书籍信息return book;}}

在上述示例中,findBookById方法的返回值将被缓存到名为"books"的缓存中,并且使用id作为缓存的键。

@CachePut

@CachePut注解可用于方法级别,用于将方法的返回值存储到缓存中。与@Cacheable注解不同的是,@CachePut注解每次都会执行方法体。

示例:

import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;@Service
public class YourService {@CachePut(value = "books", key = "#book.id")public Book updateBook(Book book) {// 更新数据库或其他数据源中的书籍信息return book;}}

在上述示例中,每次调用updateBook方法时,都会执行方法体,并将返回的书籍信息存储到名为"books"的缓存中,使用book.id作为缓存的键。

@CacheEvict

@CacheEvict注解可用于方法级别,用于从缓存中移除指定的条目。可以使用value属性指定要清除的缓存名称,还可以使用key属性来定义要清除的缓存键。

示例:

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;@Service
public class YourService {@CacheEvict(value = "books", key = "#id")public void deleteBook(String id) {// 从数据库或其他数据源中删除书籍信息}}

在上述示例中,每次调用deleteBook方法时,将从名为"books"的缓存中移除具有给定id的条目。

结论

通过使用@Cacheable@CachePut@CacheEvict注解,您可以方便地使用Redis缓存来提高Spring Boot应用程序的性能和响应速度。这些注解使得方法的结果可以被缓存、更新或清除,从而减少对后端资源的访问。

希望

本篇博客对您有所帮助!如果您想深入了解更多关于Spring Boot和Redis缓存的内容,建议您查阅官方文档和其他相关资源。

参考链接:

  • Spring Boot官方文档
  • Spring Framework官方文档 - Caching
http://www.lryc.cn/news/70166.html

相关文章:

  • 通知可以根据切入点表达式来进行增强,也可以根据自己的注解值来进行增强
  • <Python实际应用>做一个简单的签到投屏系统
  • 时序预测 | MATLAB实现BO-CNN-GRU贝叶斯优化卷积门控循环单元时间序列预测
  • Baumer工业相机堡盟工业相机使用BGAPISDK将工业相机设为Burst模式以及该模式的优势以及行业应用(C++)
  • BERT输入以及权重矩阵形状解析
  • 3 个令人惊艳的 ChatGPT 项目,开源了!
  • 一、12.C++内存管理
  • ensp实践dhcp服务
  • 【王道·计算机网络】第六章 应用层
  • 【论文解读】(如何微调BERT?) How to Fine-Tune BERT for Text Classification?
  • 工程师是怎样对待开源
  • Spring Boot日志系统大揭秘:从零开始学习Spring Boot日志:常见问题解答和最佳实践
  • 【06】Nginx之反向代理
  • TCP是面向字节流的协议
  • 读书笔记——《when breath becomes air》《超越自卑》
  • CMD与DOS脚本编程【第二章】
  • 面试字节,过关斩将直接干到 3 面,结果被吊打了?
  • OpenCV在iOS端的集成及Mat和UIImage互相转化(附源码)
  • 5月跳槽会有风险,不跳也会有?
  • 【小白版】最简单的 goland package 教程包括自定义包的使用
  • IMX6ULL的I2C驱动详细分析
  • 日志迁移到 logback
  • 开源字节 CRM 系统
  • 七、Spring Cloud Alibaba-Sentinel
  • 机器学习与深度学习——通过knn算法分类鸢尾花数据集iris求出错误率并进行可视化
  • 【MySQL】MySQL基础知识详解
  • RabbitMQ日常使用小结
  • ​​​​​​​博物馆文物馆藏环境空气质量无线监控系统方案
  • 测试理论----Bug的严重程度(Severity)和优先级(Priority)的分类
  • 斯坦福、Nautilus Chain等联合主办的 Hackathon 活动,现已接受报名