目录
- 前言
- 在 Spring Boot 项目中配置和使用 Redis
- 步骤 1:添加 Redis 依赖
- 步骤 2:配置 Redis 连接信息
- 使用 application.properties 配置文件:
- 使用 application.yml 配置文件:
- 步骤 3:创建 Redis 配置类
- 步骤 4:使用 RedisTemplate 进行操作
- 总结
前言
当在 Java 项目中使用 Redis 时,特别是在 Spring Boot 项目中使用 Redis,下面是一个详细的教程,涵盖了 Redis 的配置和使用。
Redis是当前比较热门的NOSQL系统之一,它是一个开源的使用ANSI c语言编写的key-value存储系统(区别于MySQL的二维表格的形式存储。)。和Memcache类似,但很大程度补偿了Memcache的不足。和Memcache一样,Redis数据都是缓存在计算机内存中,不同的是,Memcache只能将数据缓存到内存中,无法自动定期写入硬盘,这就表示,一断电或重启,内存清空,数据丢失。所以Memcache的应用场景适用于缓存无需持久化的数据。而Redis不同的是它会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,实现数据的持久化。
Redis的特点:
1,Redis读取的速度是110000次/s,写的速度是81000次/s;
2,原子 。Redis的所有操作都是原子性的,同时Redis还支持对几个操作全并后的原子性执行。
3,支持多种数据结构:string(字符串);list(列表);hash(哈希),set(集合);zset(有序集合)
4,持久化,集群部署
5,支持过期时间,支持事务,消息订阅
在 Spring Boot 项目中配置和使用 Redis
步骤 1:添加 Redis 依赖
在你的 Spring Boot 项目的 pom.xml 文件中,添加 Redis 相关的依赖项:
<dependencies>
<!-- 其他依赖项 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
这将添加 Spring Boot Redis Starter 依赖项,以便在项目中使用 Redis。
步骤 2:配置 Redis 连接信息
在 Spring Boot 项目中,可以通过在 application.properties 或 application.yml 文件中配置 Redis 连接信息。
使用 application.properties 配置文件:
在 application.properties 文件中添加以下配置:
# Redis 连接信息 spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password=your_password
使用 application.yml 配置文件:
在 application.yml 文件中添加以下配置:
# Redis 连接信息
spring:
redis:
host: 127.0.0.1
port: 6379
password: your_password
请确保将上述配置中的 your_password 替换为你实际的 Redis 密码。如果 Redis 服务器没有设置密码,则可以省略 spring.redis.password 配置。
步骤 3:创建 Redis 配置类
创建一个名为 RedisConfig 的配置类,用于配置 RedisTemplate 和连接工厂。
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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
public class RedisConfig {
// 从配置文件中读取Redis主机信息
@Value("${spring.redis.host}")
private String redisHost;
// 从配置文件中读取Redis端口信息
@Value("${spring.redis.port}")
private int redisPort;
// 配置Redis连接工厂
@Bean
public RedisConnectionFactory redisConnectionFactory() {
// 创建Redis的单机配置
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(redisHost, redisPort);
// 返回Lettuce连接工厂
return new LettuceConnectionFactory(config);
}
// 配置RedisTemplate
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
// 创建RedisTemplate实例
RedisTemplate<String, Object> template = new RedisTemplate<>();
// 设置连接工厂
template.setConnectionFactory(connectionFactory);
// 设置默认的序列化器为GenericJackson2JsonRedisSerializer,用于序列化键和值为JSON格式
template.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
// 设置键的序列化器为StringRedisSerializer
template.setKeySerializer(new StringRedisSerializer());
// 设置值的序列化器为GenericJackson2JsonRedisSerializer
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
// 返回配置好的RedisTemplate实例
return template;
}
}
上述配置类使用 Lettuce 作为 Redis 连接工厂,并配置了 RedisTemplate,使用 JSON 序列化器来序列化键和值。
步骤 4:使用 RedisTemplate 进行操作
在你的代码中,你可以使用 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;
@Autowired
public MyService(RedisTemplate<String, Object> redisTemplate) {
this.redisTemplate = redisTemplate;
}
public void setValue(String key, Object value) {
// 使用RedisTemplate的opsForValue()方法获取ValueOperations接口实例,然后调用set()方法存储键值对
redisTemplate.opsForValue().set(key, value);
}
public Object getValue(String key) {
// 使用RedisTemplate的opsForValue()方法获取ValueOperations接口实例,然后调用get()方法根据键获取值
return redisTemplate.opsForValue().get(key);
}
public void deleteKey(String key) {
// 调用RedisTemplate的delete()方法根据键删除对应的键值对
redisTemplate.delete(key);
}
}
上述示例代码展示了一个名为 MyService 的服务类,它使用 RedisTemplate 进行键值对的存储、获取和删除操作。
请确保在你的代码中使用适当的注解(如 @Service、@Autowired 等)来注入 RedisTemplate 实例并进行相应的操作。
总结
到此这篇关于springboot项目中配置redis的文章就介绍到这了,更多相关springboot项目配置redis内容请搜索本网站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本网站!
您可能感兴趣的文章:
- 详解springboot配置多个redis连接
- 在SpringBoot中添加Redis及配置方法
- springboot配置redis过程详解
- springboot 排除redis的自动配置操作
- Springboot项目中使用redis的配置详解