springboot整合redis
springboot整合redis
添加pom.xml依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>
参数设置
在application.properties加入设置
# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=localhost
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0
使用StringRedisTemplate测试访问
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootredisApplicationTests {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
public void stringRedisTest() throws Exception {
stringRedisTemplate.opsForValue().set("redisKey", "redisValue");
Assert.assertEquals("redisValue", stringRedisTemplate.opsForValue().get("redisKey"));
}
}
存储对象
除了String类型,实战中我们还经常会在Redis中存储对象,这时候我们就会想是否可以使用类似RedisTemplate<String, User>来初始化并进行操作。但是Spring Boot并不支持直接使用,需要我们自己实现RedisSerializer
1. 创建对象
没啥好说的,创建User.class
2. 实现对象的序列化接口
public class RedisObjectSerializer implements RedisSerializer<Object> {
private Converter<Object, byte[]> serializer = new SerializingConverter();
private Converter<byte[], Object> deserializer = new DeserializingConverter();
static final byte[] EMPTY_ARRAY = new byte[0];
@Override
public byte[] serialize(Object o) throws SerializationException {
if (o == null) {
return EMPTY_ARRAY;
}
try {
return serializer.convert(o);
} catch (Exception ex) {
return EMPTY_ARRAY;
}
}
@Override
public Object deserialize(byte[] bytes) throws SerializationException {
if (isEmpty(bytes)) {
return null;
}
try {
return deserializer.convert(bytes);
} catch (Exception ex) {
throw new SerializationException("Cannot deserialize", ex);
}
}
private boolean isEmpty(byte[] data) {
return (data == null || data.length == 0);
}}
3. 针对User做实例配置
@Configuration
public class RedisConfig {
@Bean
public JedisConnectionFactory jedisConnectionFactory(){
return new JedisConnectionFactory();
}
@Bean
public RedisTemplate<String,User> redisTemplate(){
RedisTemplate<String,User> template = new RedisTemplate<>();
template.setConnectionFactory(jedisConnectionFactory());
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new RedisObjectSerializer());
return template;
}
}
4. 测试用例
@Test
public void redisTest() throws Exception {
stringRedisTemplate.opsForValue().set("redisKey", "redisValue");
Assert.assertEquals("redisValue", stringRedisTemplate.opsForValue().get("redisKey"));
User user = new User("钢铁侠", 45);
redisTemplate.opsForValue().set(user.getName(), user);
user = new User("美国队长", 999);
redisTemplate.opsForValue().set(user.getName(), user);
Assert.assertEquals(45, redisTemplate.opsForValue().get("钢铁侠").getAge().intValue());
Assert.assertEquals(999, redisTemplate.opsForValue().get("美国队长").getAge().intValue());
}