1、引入依赖
需引入两组pom依赖
<!-- 引入spring data redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- 引入jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
2、创建RedisConfig配置类
初始化redis连接池和连接工厂,并创建redisTemplate操作模板。
说明
:@ConfigurationProperties(prefix = "spring.redis.pool")
与@ConfigurationProperties(prefix = "spring.redis")
分别对应application.properties中的配置信息,它们会将指定的前缀相同各个配置实例化为一个配置对象。
package com.example.demo.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
@Configuration
public class RedisConfig {
/**
* 连接池配置
* @return
*/
@Bean
@ConfigurationProperties(prefix = "spring.redis.pool")
public JedisPoolConfig jedisPoolConfig(){
return new JedisPoolConfig();
}
/**
* 连接工厂配置
* @param cfg
* @return
*/
@Bean
@ConfigurationProperties(prefix = "spring.redis")
public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig cfg){
JedisConnectionFactory factory = new JedisConnectionFactory();
factory.setPoolConfig(cfg);
return factory;
}
/**
* RedisTemplate操作模板
* @param factory
* @return
*/
@Bean
public RedisTemplate<String,Object> redisTemplate(JedisConnectionFactory factory){
RedisTemplate<String, Object> rt = new RedisTemplate<>();
rt.setConnectionFactory(factory);
rt.setKeySerializer(new StringRedisSerializer());//key序列化
rt.setValueSerializer(new StringRedisSerializer());//value序列化
return rt;
}
}
3、添加配置信息
在application.properties中添加如下配置
# redis配置信息
# 分别为最小空闲连接、最大空闲连接、最大连接数、ip、端口、指定库(默认0)
spring.redis.pool.min-idle=5
spring.redis.pool.max-idle=10
spring.redis.pool.max-total=20
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.database=1
4、Junit测试(举例3种存/取值类型)
1、字符串存/取
@Autowired
private RedisTemplate<String, Object> redisTemplate;
/**
* redis存值(字符串)
*/
@Test
public void redisSetStr(){
this.redisTemplate.opsForValue().set("movieName","无间道");
}
/**
* redis取值(字符串)
*/
@Test
public void redisGetStr(){
System.out.println("get str: "+this.redisTemplate.opsForValue().get("movieName"));
}
2、对象存/取
/**
* redis存值(对象)
*/
@Test
public void redisSetObj(){
MovieInfo movie = new MovieInfo();
movie.setId(1L);
movie.setName("当幸福来敲门");
movie.setRole("威尔史密斯");
movie.setArea("美国");
this.redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
this.redisTemplate.opsForValue().set("movie",movie);
}
/**
* redis取值(对象)
*/
@Test
public void redisGetObj(){
this.redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
MovieInfo movie = (MovieInfo) this.redisTemplate.opsForValue().get("movie");
System.out.println("get obj: "+movie);
}
3、Json对象存/取
/**
* redis存值(json格式对象)
*/
@Test
public void redisSetJsonObj(){
MovieInfo movie = new MovieInfo();
movie.setId(1L);
movie.setName("当幸福来敲门");
movie.setRole("威尔史密斯");
movie.setArea("美国");
this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(MovieInfo.class));
this.redisTemplate.opsForValue().set("movie_json",movie);
}
/**
* redis取值(json格式对象)
*/
@Test
public void redisGetJsonObj(){
this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(MovieInfo.class));
MovieInfo movie = (MovieInfo) this.redisTemplate.opsForValue().get("movie_json");
System.out.println("get json obj: "+movie);
}
效果如下