SpringBoot整合Redis
导入Maven
<!--redis lettuce连接池,需要依赖commons-pool2--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.9.0</version> </dependency> <!--redis 依赖包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
application.yml
spring:
redis:
host: 192.168.111.129
port: 6379
password: 123456
timeout: 1000ms # 连接超时时间
database: 12 #数据库索引,redis有0-15个,总共16个数据库
lettuce:
pool:
# 连接池最大连接数(使用负值表示没有限制)
max-active: 1000
# 连接池中的最大空闲连接
max-idle: 80
# 连接池中的最小空闲连接
min-idle: 10
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: 100ms
Config
import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; 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.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration public class RedisConfig { @Bean public RedisTemplate redisTemplate(RedisConnectionFactory factory) { RedisTemplate template = new RedisTemplate<>(); // 配置连接工厂 template.setConnectionFactory(factory); //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式) Jackson2JsonRedisSerializer jacksonSerial = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); // 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); // 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常 om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jacksonSerial.setObjectMapper(om); // 值采用json序列化 template.setValueSerializer(jacksonSerial); //解决key乱码问题,也就是redis中的符号前缀,redis是会默认在数值前面添加一些前缀的 template.setKeySerializer(new StringRedisSerializer()); template.setHashKeySerializer(new StringRedisSerializer()); // 设置hash key 和value序列化模式 template.setHashKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(jacksonSerial); template.afterPropertiesSet(); return template; } }
可以定义一个Redis的相关工具类
/** * redis操作工具类. * (基于RedisTemplate) */ @Component public class RedisUtils { @Autowired private RedisTemplate<String, String> redisTemplate; /** * 读取缓存 * * @param key * @return */ public String get(final String key) { return redisTemplate.opsForValue().get(key); } /** * 写入缓存 */ public boolean set(final String key, String value) { boolean result = false; try { redisTemplate.opsForValue().set(key, value); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 写入缓存,并设置过期时间,单位为s */ public boolean setTime(final String key, Object value, long time) { boolean result = false; try { redisTemplate.opsForValue().set(key, String.valueOf(value), time ,TimeUnit.SECONDS); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 更新缓存 */ public boolean getAndSet(final String key, String value) { boolean result = false; try { redisTemplate.opsForValue().getAndSet(key, value); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 删除缓存 */ public boolean delete(final String key) { boolean result = false; try { redisTemplate.delete(key); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } }
其他说明:
opsForHash --> hash操作
opsForList --> list操作
opsForSet --> set操作
opsForValue --> string操作
opsForZSet --> ZSet操作
测试
@SpringBootTest class Demo2ApplicationTests { @Autowired private RedisUtils redisUtils; @Test void contextLoads() { redisUtils.setTime("333",123,10); } }