本文最后更新于104 天前,其中的信息可能已经过时,如有错误请发送邮件到z18096561915@outlook.com
redis 导入坐标依赖
<!--redis依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
Redis配置类
package com.sky.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CachingConfigurerSupport;
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.StringRedisSerializer;
/**
* Redis配置类
* 如果不创建 框架会自己创建,这里只是配置一下
*/
@Configuration
@Slf4j
public class RedisConfiguration extends CachingConfigurerSupport {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
// 默认是redis Key的序列化器
redisTemplate.setKeySerializer(new StringRedisSerializer());
//设置redis的连接工厂对象
redisTemplate.setConnectionFactory(connectionFactory);
return redisTemplate;
}
}
application.yml配置文件
spring:
redis:
host: 127.0.0.1 # Redis服务器地址
port: 6379 # Redis服务器连接端口
password: 123456 # Redis服务器连接密码(默认为空)
database: 0 # Redis数据库索引(默认为0)