Spring Boot 2整合Redis做缓存
既然是要用Redis做缓存,自然少不了安装了。但是本文主要讲Spring Boot与Redis整合。安装教程请另行百度!
1.首先是我们的Redis配置类
1 package com.tyc; 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.annotation.EnableCaching; 7 import org.springframework.context.annotation.Bean; 8 import org.springframework.context.annotation.Configuration; 9 import org.springframework.data.redis.connection.RedisConnectionFactory; 10 import org.springframework.data.redis.core.RedisTemplate; 11 import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; 12 import org.springframework.data.redis.serializer.StringRedisSerializer; 13 14 /** 15 * @Description: Redis配置类 16 * @Author: wushumin 17 * @CreateDate: 2019/9/12 16:00 18 * @Version: 1.0 19 */ 20 @Configuration 21 public class RedisConfig { 22 @Bean 23 @SuppressWarnings("all") 24 public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { 25 RedisTemplate<String, Object> template = new RedisTemplate<String, Object>(); 26 template.setConnectionFactory(factory); 27 Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); 28 ObjectMapper om = new ObjectMapper(); 29 om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); 30 om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); 31 jackson2JsonRedisSerializer.setObjectMapper(om); 32 StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); 33 // key采用String的序列化方式 34 template.setKeySerializer(stringRedisSerializer); 35 // hash的key也采用String的序列化方式 36 template.setHashKeySerializer(stringRedisSerializer); 37 // value序列化方式采用jackson 38 template.setValueSerializer(jackson2JsonRedisSerializer); 39 // hash的value序列化方式采用jackson 40 template.setHashValueSerializer(jackson2JsonRedisSerializer); 41 template.afterPropertiesSet(); 42 return template; 43 } 44 }
package com.tyc.annotation; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.data.redis.RedisProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachingConfigurerSupport; 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.RedisOperations; import org.springframework.data.redis.core.StringRedisTemplate; import java.net.UnknownHostException; import java.time.Duration; import java.util.HashMap; import java.util.Map; @Configuration @ConditionalOnClass(RedisOperations.class) @EnableConfigurationProperties(RedisProperties.class) public class RedisAutoConfiguration extends CachingConfigurerSupport {//缓存管理器 @Bean public CacheManager cacheManager(RedisConnectionFactory connectionFactory) { //缓存配置 RedisCacheConfiguration userCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() .entryTtl(Duration.ofSeconds(60)).disableCachingNullValues(); Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
//手动指定缓存模块 redisCacheConfigurationMap.put("legal", userCacheConfiguration); redisCacheConfigurationMap.put("fuzzySearch", userCacheConfiguration); //初始化一个RedisCacheWriter RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory); RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig(); //设置默认超过期时间是半小时 defaultCacheConfig.entryTtl(Duration.ofSeconds(60 * 30)); // //初始化RedisCacheManager RedisCacheManager cacheManager = new RedisCacheManager(redisCacheWriter, defaultCacheConfig, redisCacheConfigurationMap); return cacheManager; } }
@Cacheable(cacheNames = "fuzzySearch", key = "targetClass+methodName+#fuzzySearchName") public Map fuzzySearch(String fuzzySearchName) { //返回结果集合 Map resultMap = new LinkedHashMap(); return resultMap }
到这就可以测试了。由于时间原因就先不贴测试截图了。