Java教程

springboot集成redis使用1

本文主要是介绍springboot集成redis使用1,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
修改springboot版本
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
导入依赖
<!-- redis -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- spring2.X集成redis所需commons-pool2 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>2.6.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

application.properties配置redis配置

#Redis服务器地址
spring.redis.host=114.116.246.5
#Redis服务器连接端口
spring.redis.port=6379
#Redis服务器连接密码
spring.redis.password = 123456
#Redis数据库索引(默认为0)
spring.redis.database= 0
#连接超时时间(毫秒)
spring.redis.timeout=1800000
#连接池最大连接数(使用负值表示没有限制)
spring.redis.lettuce.pool.max-active=20
#最大阻塞等待时间(负数表示没限制)
spring.redis.lettuce.pool.max-wait=-1
#连接池中的最大空闲连接
spring.redis.lettuce.pool.max-idle=5
#连接池中的最小空闲连接
spring.redis.lettuce.pool.min-idle=0

添加redis配置类(RedisConfig)

 1 package com.example.redisdemo.config;
 2 
 3 import com.fasterxml.jackson.annotation.JsonAutoDetect;
 4 import com.fasterxml.jackson.annotation.PropertyAccessor;
 5 import com.fasterxml.jackson.databind.ObjectMapper;
 6 import org.springframework.cache.CacheManager;
 7 import org.springframework.cache.annotation.CachingConfigurerSupport;
 8 import org.springframework.cache.annotation.EnableCaching;
 9 import org.springframework.context.annotation.Bean;
10 import org.springframework.context.annotation.Configuration;
11 import org.springframework.data.redis.cache.RedisCacheConfiguration;
12 import org.springframework.data.redis.cache.RedisCacheManager;
13 import org.springframework.data.redis.connection.RedisConnectionFactory;
14 import org.springframework.data.redis.core.RedisTemplate;
15 import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
16 import org.springframework.data.redis.serializer.RedisSerializationContext;
17 import org.springframework.data.redis.serializer.RedisSerializer;
18 import org.springframework.data.redis.serializer.StringRedisSerializer;
19 
20 import java.time.Duration;
21 
22 /**
23  * @Description: TODO
24  * @author: zhr
25  * @date: 2021年08月07日 17:20
26  */
27 @EnableCaching
28 @Configuration
29 public class RedisConfig extends CachingConfigurerSupport {
30 
31     @Bean
32     public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
33         RedisTemplate<String, Object> template = new RedisTemplate<>();
34         RedisSerializer<String> redisSerializer = new StringRedisSerializer();
35         Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
36         ObjectMapper om = new ObjectMapper();
37         om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
38         om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
39         jackson2JsonRedisSerializer.setObjectMapper(om);
40         template.setConnectionFactory(factory);
41 //key序列化方式
42         template.setKeySerializer(redisSerializer);
43 //value序列化
44         template.setValueSerializer(jackson2JsonRedisSerializer);
45 //value hashmap序列化
46         template.setHashValueSerializer(jackson2JsonRedisSerializer);
47         return template;
48     }
49 
50     @Bean
51     public CacheManager cacheManager(RedisConnectionFactory factory) {
52         RedisSerializer<String> redisSerializer = new StringRedisSerializer();
53         Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
54 //解决查询缓存转换异常的问题
55         ObjectMapper om = new ObjectMapper();
56         om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
57         om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
58         jackson2JsonRedisSerializer.setObjectMapper(om);
59 // 配置序列化(解决乱码的问题),过期时间600秒
60         RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
61                 .entryTtl(Duration.ofSeconds(600))
62                 .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
63                 .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
64                 .disableCachingNullValues();
65         RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
66                 .cacheDefaults(config)
67                 .build();
68         return cacheManager;
69     }
70 }
View Code

 

 

 

 

 


 

这篇关于springboot集成redis使用1的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!