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

将Spring Boot与Redis集成

一、引言

1、SpringBoot:

Spring Boot是一个用于创建独立且可执行的Spring应用程序的框架。它简化了基于Spring框架的应用程序的开发过程,并提供了一种快速和简便的方式来构建Java应用程序。

Spring Boot提供了自动配置机制,通过引入适当的依赖项,可以自动配置各种Spring功能。它还提供了内嵌的HTTP服务器(如Tomcat、Jetty或Undertow),使得将应用程序打包为可执行的JAR文件变得非常容易。

使用Spring Boot,您可以快速搭建一个生产级别的应用程序,而无需进行复杂的配置。它提供了许多开箱即用的特性,例如自动配置、自动构建和部署、监控和运维工具等,从而大大简化了开发人员的工作。

Spring Boot还与其他Spring项目(如Spring Data、Spring Security和Spring Cloud)紧密集成,使得构建微服务架构变得更加容易。它有助于提高开发效率和团队协作能力,因此在企业级应用程序开发中非常受欢迎。

总之,Spring Boot是一个快速、简单且灵活的框架,旨在简化Spring应用程序的开发和部署过程,并提供了丰富的功能和生态系统支持。

2、Redis:

Redis是一个开源的内存数据结构存储系统,也被称为键值存储数据库。它支持多种数据结构,包括字符串(Strings)、哈希(Hashes)、列表(Lists)、集合(Sets)和有序集合(Sorted Sets)。Redis以高效地读写速度和灵活的数据模型而闻名。

Redis将数据存储在内存中,因此具有低延迟和高吞吐量的特点。它还提供持久化功能,可以将数据定期保存到磁盘上,以便在服务器重启后恢复数据。

除了常见的数据库操作之外,Redis还提供了一些其他功能,如发布/订阅、事务处理和Lua脚本执行。这些功能使得Redis非常适合用作缓存系统、消息队列、实时统计分析等场景。

由于其性能出色和丰富的功能,Redis被广泛应用于Web应用程序、移动应用程序和数据缓存等领域。

二、集成

1、添加Redis依赖:在您的pom.xml文件中添加以下依赖项以使用Redis客户端库:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

 2、配置Redis连接:在application.propertiesapplication.yml文件中配置Redis连接属性。例如:

spring.redis.host=127.0.0.1
spring.redis.port=6379

 3、创建Redis配置类:创建一个Java类来配置Redis连接,并使用@Configuration@EnableCaching注解标记它。这样Spring Boot就能够自动配置Redis缓存支持。

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;@Configuration
@EnableCaching
public class RedisConfig {@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(connectionFactory);template.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(new GenericJackson2JsonRedisSerializer());return template;}@Beanpublic RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {RedisCacheManager cacheManager = RedisCacheManager.create(connectionFactory);cacheManager.setTransactionAware(true);return cacheManager;}
}

 4、使用Redis:您可以通过自动装配RedisTemplate或使用Spring的@Cacheable注解来使用Redis进行缓存操作。

自动装配RedisTemplate

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;@Service
public class MyService {private final RedisTemplate<String, Object> redisTemplate;@Autowiredpublic MyService(RedisTemplate<String, Object> redisTemplate) {this.redisTemplate = redisTemplate;}public void setValue(String key, Object value) {redisTemplate.opsForValue().set(key, value);}public Object getValue(String key) {return redisTemplate.opsForValue().get(key);}
}

 使用@Cacheable注解:

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;@Service
public class MyService {@Cacheable("myCache")public String getData() {// 从数据库或其他数据源获取数据的逻辑return "data";}
}

现在您已经成功将Spring Boot与Redis集成。根据您的需求,您可以选择使用RedisTemplate直接访问Redis,或者使用Spring的缓存抽象来管理缓存。

http://www.lryc.cn/news/145191.html

相关文章:

  • vue echarts
  • idea上利用JDBC连接MySQL数据库(8.1.0版)
  • 【100天精通python】Day47:python网络编程_Web编程基础
  • DockerCompose介绍与使用
  • Windows Qt 5.12.10下载与安装
  • RustDesk最新版本编译与打包
  • Gin 框架入门实战系列(一)
  • 【测试】pywinauto的简单使用(安装、常用对象、元素控件、鼠标操作、键盘操作)
  • Java基础十八(正则表达式 + 日期时间)
  • Linux C 多进程编程(面试考点)
  • c++一级
  • Code Lab - 34
  • 后端返回文件流,前端怎么导出、下载(8种方法可实现)
  • 什么是 ThreadLocal?
  • CANOCO5.0实现冗余分析(RDA)最详细步骤
  • 【tkinter 专栏】掷骰子游戏
  • 19 NAT穿透|python高级
  • 2023常见前端面试题
  • 登录校验-JWT令牌-生成和校验
  • GIT 常用指令
  • 多目标优化
  • odoo的优势
  • Spring Boot(Vue3+ElementPlus+Axios+MyBatisPlus+Spring Boot 前后端分离)【三】
  • Kali 软件管理
  • 加油站【贪心算法】
  • java八股文面试[多线程]——死锁、活锁、饥饿
  • 设计模式——装饰器模式
  • ①matlab的命令掌握
  • MySQL----索引
  • 秒杀系统的业务流程以及优化方案(实现异步秒杀)