SpringBoot2.0+整合redis,使用 RedisTemplate操作redis
本文中使用RedisTemplate操作redis
springboot2.0整合redis在cacheManager的配置中和一些配置参数,有了些许差异,在此做一个简单的整理
Maven引入配置依赖
<!--只需要引入这一个依赖即可-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
Application.properties
# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=*****
# Redis服务器连接端口
spring.redis.port=6379
spring.redis.password=******
# 连接池最大连接数(使用负值表示没有限制) 2.0区别 1.0+使用的是 spring.redis.pool.max-acitive
spring.redis.jedis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制) 2.0区别
spring.redis.jedis.pool.max-wait=-1
# 连接池中的最大空闲连接 2.0区别
spring.redis.jedis.pool.max-idle=8
# 连接池中的最小空闲连接 2.0区别
spring.redisjedis..pool.min-idle=0
配置redisConfig
这里的区别在于springboot2.x的RedisCacheManager的构造方法产生了变化,所以在此做了修改.
CacheManage的配置是为了配合注解使用redis而配置的,然而在我的开发使用中不太习惯使用注解,
首先注解确实可以更方便,但是复杂的操作和异常无法处理,这就使的灵活性有所下降,本人还是更喜欢使用RedisTemplate,当然一些简单的处理就完全可以配合使用注解了.在下面的测试中,我将使用RedisTemplate进行操作
package com.novacloud.information.common.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.time.Duration;
/**
* redis配置类
*
* @author zcc ON 2018/3/19
**/
@Configuration
@EnableCaching//开启注解
public class RedisConfig {
@Bean
//如使用注解的话需要配置cacheManager
//此处为与1.x最大的不同
CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
//初始化一个RedisCacheWriter
RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig();
//设置默认超过期时间是1天
defaultCacheConfig.entryTtl(Duration.ofDays(1));
//初始化RedisCacheManager
RedisCacheManager cacheManager = new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
return cacheManager;
}
// 以下两种redisTemplate自由根据场景选择
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
serializer.setObjectMapper(mapper);
template.setValueSerializer(serializer);
//使用StringRedisSerializer来序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
template.afterPropertiesSet();
return template;
}
@Bean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(factory);
return stringRedisTemplate;
}
}
测试
这里只是对redis进行了简单的String类型的set和get操作,如果需要后续可以写一篇使用redisTemplate操作redis各种数据类型的方法
package com.neopte;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class NeopteApplicationTests {
@Autowired
RedisTemplate redisTemplate;
@Test
public void contextLoads() {
//这里相当于redis对String类型的set操作
redisTemplate.opsForValue().set("test","helloworld");
//这里相当于redis对String类型的get操作
String test = (String)redisTemplate.opsForValue().get("test");
System.out.println(test);
}
}
本文中的代码已经上传至github:https://github.com/NeoChenDev/NeoPte.git
转载于:https://my.oschina.net/u/3555772/blog/2874532