缓存之SpringCache整合redis

news/2024/10/16 4:34:13/

Spring Cache 是 Spring 框架提供的一种缓存抽象,在应用中可以方便地使用它来进行方法级别的缓存。结合 Redis 作为缓存存储后端,可以实现高性能的缓存服务。下面是一个简单的示例,演示了如何在 Spring Boot 项目中整合 Spring Cache 和 Redis。

首先,确保你的 Spring Boot 项目中引入了相关的依赖。你需要引入 Spring Boot Starter Cache 和 Spring Boot Starter Data Redis。

<!-- Maven 依赖 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

接下来,在 Spring Boot 的配置文件中配置 Redis 连接信息:

# Redis 配置
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
spring.redis.database=0

然后,在你的 Spring Boot 应用中,定义一个 Service 类,并在其中使用 Spring Cache 注解来实现方法级别的缓存

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;@Service
public class MyService {@Cacheable(value = "myCache", key = "#id")public String getDataById(int id) {// 模拟从数据库或其他数据源中获取数据的操作System.out.println("Fetching data from backend for id: " + id);return "Data for id " + id;}
}

在上面的代码中,@Cacheable 注解用于标记 getDataById 方法,指定了缓存名称为 “myCache”,并指定了缓存的 key 为方法的参数 id。当方法被调用时,如果缓存中已经存在对应 key 的数据,则直接返回缓存中的数据;如果缓存中不存在对应 key 的数据,则执行方法体中的代码,并将结果存入缓存

最后,在你的应用程序中,可以直接调用 MyService 中的方法来获取数据,Spring Cache 会自动处理缓存逻辑。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;@SpringBootApplication
@EnableCaching
public class Application {@Autowiredprivate MyService myService;public static void main(String[] args) {SpringApplication.run(Application.class, args);}public void exampleUsage() {// 调用 MyService 中的方法,数据会被缓存String data = myService.getDataById(1);System.out.println("Data: " + data);// 再次调用相同的方法,数据将直接从缓存中获取,不会执行方法体中的代码data = myService.getDataById(1);System.out.println("Data: " + data);}
}

通过以上配置,Spring Boot 就会自动使用 Redis 作为缓存存储后端,并且通过 Spring Cache 抽象来管理缓存


http://www.ppmy.cn/news/1426395.html

相关文章

人工智能大模型培训老师叶梓:探索AI的边界:本地化、定制化与隐私保护

随着人工智能技术的不断进步&#xff0c;AI商业智能工具正变得越来越重要。它们不仅提高了企业的决策效率&#xff0c;还为企业提供了前所未有的数据洞察力。在众多新兴工具中&#xff0c;AnythingLLM、LocalAI、VectorAdmin等平台以其独特的功能和优势&#xff0c;引领着AI商业…

NLP基础—jieba分词

jieba分词 支持四种分词模式 精确模式 试图将句子最精确地切开,适合文本分析;全模式 把句子中所有的可以成词的词语都扫描出来, 速度非常快,但是不能解决歧义;搜索引擎模式 在精确模式的基础上,对长词再次切分,提高召回率,适合用于搜索引擎分词。paddle模式 利用Paddle…

java+idea+mysql采用医疗AI自然语言处理技术的3D智能导诊导系统源码

javaideamysql采用医疗AI自然语言处理技术的3D智能导诊导系统源码 随着人工智能技术的快速发展&#xff0c;语音识别与自然语言理解技术的成熟应用&#xff0c;基于人工智能的智能导诊导医逐渐出现在患者的生活视角中&#xff0c;智能导诊系统应用到医院就医场景中&#xff0c…

IAM 统一身份认证与访问管理服务

即统一身份认证与访问管理服务&#xff0c;是云服务商提供的一套云上身份管理解决方案&#xff0c;可帮助企业安全地管理云上资源的访问权限。 在当今云计算时代&#xff0c;企业越来越依赖云服务来存储和处理敏感数据。然而&#xff0c;这也带来了新的安全挑战&#xff0c;即…

SQLite的PRAGMA 声明(二十三)

返回&#xff1a;SQLite—系列文章目录 上一篇&#xff1a;SQLite从出生到现在&#xff08;发布历史记录&#xff09;&#xff08;二十二&#xff09; 下一篇&#xff1a;用于 SQLite 的异步 I/O 模块&#xff08;二十四&#xff09; PRAGMA 语句是特定于 SQLite 的 SQL 扩…

解决jenkins运行sh报process apparently never started in XXX

个人记录 问题 process apparently never started in /var/jenkins_home/workspace/ks-springboot_mastertmp/durable-bbfe5f99(running Jenkins temporarily with -Dorg.jenkinsci.plugins.durabletask.BourneShellScript.LAUNCH_DIAGNOSTICStrue might make the problem cl…

MySQL高级(索引-性能分析-explain执行计划)

explain 或者 desc 命令获取 MySQL 如何执行 select 语句的信息&#xff0c;包括在 select 语句执行过程中表如何连接和连接的顺序。 -- 直接在 select 语句之前加上关键字 explain / desc explain select 字段列表 from 表名 where 条件 &#xff1b; explain select * …

CentOS-Stream-9升级openssh9.7p

CentOS Stream 9 ssh -V需要的RPM包 openssh-9.7p1-1.el9.x86_64.rpm openssh-clients-9.7p1-1.el9.x86_64.rpm openssh-server-9.7p1-1.el9.x86_64.rpm 编译openssh openssh官方只提供源码包&#xff0c;我们选择自己将源码编译为rpm包来升级环境的openssh&#xff0c;需要…