redis:键值对数据库
导入坐标
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
配置文件配置类
@Configuration//配置类注解
public class RedisConfig {
@Bean//配置redis配置
public JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig config=new JedisPoolConfig();
config.setMaxIdle(10);//最大空闲数
config.setMinIdle(5);//最小空闲数
config.setMaxTotal(20);//最大连接数
return config;
}
@Bean//配置redis连接信息
public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig config) {
//spring2.3之后方式
//添加集群
Set<String> sentinelHostAndPorts=new HashSet<String>();
sentinelHostAndPorts.add("192.168.190.128:6379");
RedisSentinelConfiguration configuration=new RedisSentinelConfiguration("192.168.190.128", sentinelHostAndPorts);
JedisConnectionFactory factory=new JedisConnectionFactory(configuration,config);
//旧版本方式
//factory.setHostName("");
//factory.setPoolConfig(config);
return factory;
}
@Bean//用于执行redis的方法
public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory factory){
//添加redis处理机制:由于redis都是以字符串存储,需要进行转换
RedisTemplate<String, Object> redisTemplate=new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
//设置键序列化
redisTemplate.setKeySerializer(new StringRedisSerializer());
//设置值的序列化操作
redisTemplate.setValueSerializer(new StringRedisSerializer());
return redisTemplate;
}
}
二、使用properties配置配置信息
application.properties
spring.redis.pool.max-idle=10
spring.redis.pool.min-idle=5
spring.redis.pool.max-total=20
spring.redis.hostName=192.168.190.128
spring.redis.port=6379
redisConfig.java
@Configuration//配置类注解
public class RedisConfig {
@Bean//配置redis配置
@ConfigurationProperties("spring.redis.pool")
public JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig config=new JedisPoolConfig();
System.out.println("测试2:"+config.getMaxIdle());
System.out.println("测试2:"+config.getMinIdle());
System.out.println("测试2:"+config.getMaxTotal());
return config;
}
@Bean//配置redis连接信息
@ConfigurationProperties(prefix="spring.redis")
public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig config) {
System.out.println("测试1:"+config.getMaxIdle());
System.out.println("测试1:"+config.getMinIdle());
System.out.println("测试1:"+config.getMaxTotal());
JedisConnectionFactory factory=new JedisConnectionFactory();
factory.setPoolConfig(config);
return factory;
}
@Bean//用于执行redis的方法
public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory factory){
//添加redis处理机制:由于redis都是以字符串存储,需要进行转换
RedisTemplate<String, Object> redisTemplate=new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
//设置键序列化
redisTemplate.setKeySerializer(new StringRedisSerializer());
//设置值的序列化操作
redisTemplate.setValueSerializer(new StringRedisSerializer());
return redisTemplate;
}
}
三、使用redis存储对象
@Test
public void testSetUser() {
User user=new User();
user.setId(1);
user.setName("张三");
//设置对象序列化器,对象需要实现序列化接口
this.redisUser.setValueSerializer(new JdkSerializationRedisSerializer());
this.redisUser.opsForValue().set("user", user);
}
@Test
public void testGetUser() {
this.redisUser.setValueSerializer(new JdkSerializationRedisSerializer());
User value=(User) this.redisUser.opsForValue().get("user");
System.out.println(value.toString());
}
四、使用redis存储JSON
@Test
public void testSetJSON() {
User user=new User();
user.setId(2);
user.setName("李四");
//设置对象序列化器,对象需要实现序列化接口
this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(User.class));//需要向序列化器说明对象
this.redisTemplate.opsForValue().set("user_json", user);
}
@Test
public void testGetJSON() {
this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(User.class));//需要向序列化器说明对象
User value=(User) this.redisTemplate.opsForValue().get("user_json");
System.out.println(value.toString());
}