SpringBoot整合Redis
-
引入依賴
<!-- redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- spring2.X集成redis所需common-pool2 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.6.0</version> </dependency>
-
添加redis配置類
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.CachingConfigurerSupport; 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.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializationContext; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.time.Duration; @EnableCaching // 開啟緩存 @Configuration // 配置類 public class RedisConfig extends CachingConfigurerSupport { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); RedisSerializer<String> redisSerializer = new StringRedisSerializer(); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); template.setConnectionFactory(factory); //key序列化方式 template.setKeySerializer(redisSerializer); //value序列化 template.setValueSerializer(jackson2JsonRedisSerializer); //value hashmap序列化 template.setHashValueSerializer(jackson2JsonRedisSerializer); return template; } @Bean public CacheManager cacheManager(RedisConnectionFactory factory) { RedisSerializer<String> redisSerializer = new StringRedisSerializer(); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); //解決查詢緩存轉(zhuǎn)換異常的問題 ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); // 配置序列化(解決亂碼的問題),過期時(shí)間600秒 RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig() .entryTtl(Duration.ofSeconds(600)) .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer)) .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)) .disableCachingNullValues(); RedisCacheManager cacheManager = RedisCacheManager.builder(factory) .cacheDefaults(config) .build(); return cacheManager; } }
-
在接口中添加redis緩存
-
緩存@Cacheable:根據(jù)方法對其返回結(jié)果進(jìn)行緩存,下次請求時(shí),如果緩存存在,則直接讀取緩存數(shù)據(jù)返回,如果不存在,則執(zhí)行方法,并把返回的結(jié)果存入緩存中,一般用在查詢方法上
屬性/方法 解釋 value 緩存名,必填,它指定了緩存存放在哪塊命名空間 cacheNames 與value差不多 二選一即可 key 可選屬性,可以使用SpEL,標(biāo)簽自定義緩存的key -
緩存@CachePut:使用該注解標(biāo)志的方法,每次都會(huì)執(zhí)行,并將結(jié)果存入指定的緩存中。其他方法可以直接從響應(yīng)的緩存中讀取緩存數(shù)據(jù),而不需要再去查詢數(shù)據(jù)庫。一般用在新增方法上
屬性/方法 解釋 value 緩存名,必填。它指定了緩存存放在哪塊命名空間 cacheNames 與value差不多 二選一即可 key 可選屬性,可以使用SpEL,標(biāo)簽自定義緩存的key -
緩存@CacheEvict:使用該注解標(biāo)志的方法,會(huì)清空指定的緩存,一般用在更新或者刪除方法上
屬性/方法 解釋 value 緩存名,必填。它指定了緩存存放在哪塊命名空間 cacheNames 與value差不多 二選一即可 key 可選屬性,可以使用SpEL,標(biāo)簽自定義緩存的key allEntries 是否清空所有緩存,默認(rèn)為false。如果指定為true,則方法調(diào)用后將立即清空所有緩存 beforeInvocation 是否在執(zhí)行前就清空,默認(rèn)為false,如果指定為true,則在方法執(zhí)行前就會(huì)清空緩存
-
-
在配置文件中配置redis地址
spring.redis.host=118.178.59.51 spring.redis.port=6379 spring.redis.database=0 spring.redis.timeout=1800000 spring.redis.lettuce.pool.max-active=20 spring.redis.lettuce.pool.max-wait=-1 # 最大阻塞等待時(shí)間(復(fù)數(shù)表示沒有限制) spring.redis.lettuce.pool.max-idle=5 spring.redis.lettuce.pool.min-idle=0