關於EnableCaching最簡單使用,我的感受只需提供一個CacheManager的一個實例就行了。springboot爲咱們提供了cache相關的自動配置。引入cache模塊,以下。html
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-redis</artifactId> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency>
本人也僅僅使用了redis、guava、ehcache。更多詳情請參考 spring cache官方文檔。java
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-caching.html
@Cacheable 觸發緩存填充redis
@CacheEvict 觸發緩存驅逐spring
@CachePut 更新緩存而不會干擾方法執行數組
@Caching 從新組合要在方法上應用的多個緩存操做緩存
@CacheConfig 在類級別共享一些常見的緩存相關設置安全
下表直接摘自Spring官方文檔:springboot
名字
|
位置
|
描述
|
示例
|
methodName
|
root對象
|
當前被調用的方法名
|
#root.methodName
|
method
|
root對象
|
當前被調用的方法
|
#root.method.name
|
target
|
root對象
|
當前被調用的目標對象
|
#root.target
|
targetClass
|
root對象
|
當前被調用的目標對象類
|
#root.targetClass
|
args
|
root對象
|
當前被調用的方法的參數列表
|
#root.args[0]
|
caches
|
root對象
|
當前方法調用使用的緩存列表(如@Cacheable(value={"cache1", "cache2"})),則有兩個cache
|
#root.caches[0].name
|
argument name
|
執行上下文
|
當前被調用的方法的參數,如findById(Long id),咱們能夠經過#id拿到參數
|
#user.id
|
result
|
執行上下文
|
方法執行後的返回值(僅當方法執行以後的判斷有效,如‘unless’,'cache evict'的beforeInvocation=false)
|
#result
|
@Configuration
@EnableCaching public class RedisCacheConfig extends CachingConfigurerSupport { @Autowired private RedisProperties redisProperties; @Bean public JedisConnectionFactory jedisConnectionFactory() { // 獲取服務器數組(這裏要相信本身的輸入,因此沒有考慮空指針問題) String[] serverArray = redisProperties.getClusterNodes().split(","); RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(Arrays.asList(serverArray)); JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); // 最大空閒鏈接數, 默認8個 jedisPoolConfig.setMaxIdle(100); // 最大鏈接數, 默認8個 jedisPoolConfig.setMaxTotal(500); // 最小空閒鏈接數, 默認0 jedisPoolConfig.setMinIdle(0); // 獲取鏈接時的最大等待毫秒數(若是設置爲阻塞時BlockWhenExhausted),若是超時就拋異常, 小於零:阻塞不肯定的時間, // 默認-1 jedisPoolConfig.setMaxWaitMillis(2000); // 設置2秒 // 對拿到的connection進行validateObject校驗 jedisPoolConfig.setTestOnBorrow(true); return new JedisConnectionFactory(redisClusterConfiguration ,jedisPoolConfig); } /** * 注入redis template * * @return */ @Bean @Qualifier("redisTemplate") public RedisTemplate redisTemplate( JedisConnectionFactoryjedisConnectionFactory , Jackson2JsonRedisSerializer jackson2JsonRedisSerializer) { RedisTemplate template = new RedisTemplate(); template.setConnectionFactory(jedisConnectionFactory); template.setKeySerializer(new JdkSerializationRedisSerializer()); template.setValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); return template; } /** * redis cache manager * * @return */ @Bean @Primary public RedisCacheManager redisCacheManager( JedisConnectionFactory jedisConnectionFactory , ObjectProvider<List<RedisCacheConfigurationProvider>> configurationProvider) { Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = Maps.newHashMap(); List<RedisCacheConfigurationProvider> configurations = configurationProvider.getIfAvailable(); if (!CollectionUtils.isEmpty(configurations)) { for (RedisCacheConfigurationProvider configuration : configurations) { redisCacheConfigurationMap.putAll(configuration.resolve()); } } RedisCacheManager cacheManager = RedisCacheManager. RedisCacheManagerBuilder.fromConnectionFactory(jedisConnectionFactory) .cacheDefaults(resovleRedisCacheConfiguration(Duration. ofSeconds(300), JacksonHelper.genJavaType(Object.class))) .withInitialCacheConfigurations(redisCacheConfigurationMap) .build(); return cacheManager; } private static RedisCacheConfiguration resovleRedisCacheConfiguration(Duration duration, JavaType javaType) { return RedisCacheConfiguration.defaultCacheConfig() .serializeKeysWith(RedisSerializationContext .SerializationPair .fromSerializer(new StringRedisSerializer())) .serializeValuesWith(RedisSerializationContext .SerializationPair.fromSerializer( new Jackson2JsonRedisSerializer<>(javaType))) .entryTtl(duration); } /** * 配置一個序列器, 將對象序列化爲字符串存儲, 和將對象反序列化爲對象 */ @Bean public Jackson2JsonRedisSerializer jackson2JsonRedisSerializer() { 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); return jackson2JsonRedisSerializer; } public static abstract class RedisCacheConfigurationProvider { // key = 緩存名稱, value = 緩存時間 和 緩存類型 protected Map<String, Pair<Duration, JavaType>> configs; protected abstract void initConfigs(); public Map<String, RedisCacheConfiguration> resolve() { initConfigs(); Assert.notEmpty(configs, "RedisCacheConfigurationProvider 配置不能爲空..."); Map<String, RedisCacheConfiguration> result = Maps.newHashMap(); configs.forEach((cacheName, pair) -> result.put(cacheName, resovleRedisCacheConfiguration(pair.getKey(), pair.getValue()))); return result; } } }
@Configuration @EnableCaching public class RedisCacheConfig extends CachingConfigurerSupport { @Autowired private RedisProperties redisProperties; @Bean public LettuceConnectionFactory lettuceConnectionFactory() { String[] serverArray = redisProperties.getClusterNodes().split(",");// 獲取服務器數組(這裏要相信本身的輸入,因此沒有考慮空指針問題) RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(Arrays.asList(serverArray)); GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig(); // 最大空閒鏈接數, 默認8個 poolConfig.setMaxIdle(100); // 最大鏈接數, 默認8個 poolConfig.setMaxTotal(500); // 最小空閒鏈接數, 默認0 poolConfig.setMinIdle(0); LettuceClientConfiguration lettuceClientConfiguration = LettucePoolingClientConfiguration.builder() .commandTimeout(Duration.ofSeconds(15)) .poolConfig(poolConfig) .shutdownTimeout(Duration.ZERO) .build(); return new LettuceConnectionFactory(redisClusterConfiguration, lettuceClientConfiguration); } /** * 注入redis template * * @return */ @Bean @Qualifier("redisTemplate") public RedisTemplate redisTemplate(LettuceConnectionFactory lettuceConnectionFactory, Jackson2JsonRedisSerializer jackson2JsonRedisSerializer) { RedisTemplate template = new RedisTemplate(); template.setConnectionFactory(lettuceConnectionFactory); template.setKeySerializer(new JdkSerializationRedisSerializer()); template.setValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); return template; } /** * redis cache manager * * @return */ @Bean @Primary public RedisCacheManager redisCacheManager(LettuceConnectionFactory lettuceConnectionFactory, ObjectProvider<List<RedisCacheConfigurationProvider>> configurationProvider) { Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = Maps.newHashMap(); List<RedisCacheConfigurationProvider> configurations = configurationProvider.getIfAvailable(); if (!CollectionUtils.isEmpty(configurations)) { for (RedisCacheConfigurationProvider configuration : configurations) { redisCacheConfigurationMap.putAll(configuration.resolve()); } } RedisCacheManager cacheManager = RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(lettuceConnectionFactory) .cacheDefaults(resovleRedisCacheConfiguration(Duration.ofSeconds(300), JacksonHelper.genJavaType(Object.class))) .withInitialCacheConfigurations(redisCacheConfigurationMap) .build(); return cacheManager; } private static RedisCacheConfiguration resovleRedisCacheConfiguration(Duration duration, JavaType javaType) { return RedisCacheConfiguration.defaultCacheConfig() .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())) .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new Jackson2JsonRedisSerializer<>(javaType))) .entryTtl(duration); } @Bean public RedisLockRegistry redisLockRegistry(LettuceConnectionFactory lettuceConnectionFactory) { return new RedisLockRegistry(lettuceConnectionFactory, "recharge-plateform", 60000 * 20); } /** * 配置一個序列器, 將對象序列化爲字符串存儲, 和將對象反序列化爲對象 */ @Bean public Jackson2JsonRedisSerializer jackson2JsonRedisSerializer() { 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); return jackson2JsonRedisSerializer; } public static abstract class RedisCacheConfigurationProvider { // key = 緩存名稱, value = 緩存時間 和 緩存類型 protected Map<String, Pair<Duration, JavaType>> configs; protected abstract void initConfigs(); public Map<String, RedisCacheConfiguration> resolve() { initConfigs(); Assert.notEmpty(configs, "RedisCacheConfigurationProvider 配置不能爲空..."); Map<String, RedisCacheConfiguration> result = Maps.newHashMap(); configs.forEach((cacheName, pair) -> result.put(cacheName, resovleRedisCacheConfiguration(pair.getKey(), pair.getValue()))); return result; } } }
Jedis 是直連模式,在多個線程間共享一個 Jedis 實例時是線程不安全的,若是想要在多線程環境下使用 Jedis,須要使用鏈接池,服務器
每一個線程都去拿本身的 Jedis 實例,當鏈接數量增多時,物理鏈接成本就較高了。網絡
Lettuce的鏈接是基於Netty的,鏈接實例能夠在多個線程間共享,
因此,一個多線程的應用可使用同一個鏈接實例,而不用擔憂併發線程的數量。固然這個也是可伸縮的設計,一個鏈接實例不夠的狀況也能夠按需增長鏈接實例。經過異步的方式可讓咱們更好的利用系統資源,而不用浪費線程等待網絡或磁盤I/O。
只在基於Lettuce的配置中,加入了RedisLockRegistry對應bean的配置,因爲在集羣的模式下,基於Jedis的配置下,經過RedisLockRegistry 獲取分佈式鎖的時候報錯:
EvalSha is not supported in cluster environment
具體的解決方案就是切換至基於Lettuce的配置,請參考
https://stackoverflow.com/questions/47092475/spring-boot-redistemplate-execute-sc
RedisCacheConfigurationProvider 做用爲不一樣的cache提供特定的緩存時間以及key、value序列化和反序列化的方式。具體使用方式以下。
@Component public class CouponRedisCacheConfigurationProvider extends RedisCacheConfig.RedisCacheConfigurationProvider { @Override protected void initConfigs() { this.configs = Maps.newHashMap(); this.configs.put(CouponConstants.COUPON_ALL_CACHE, new Pair<>(Duration.ofHours(1), JacksonHelper.genMapType(HashMap.class, Integer.class, Coupon.class))); this.configs.put(CouponConstants.COUPON_GOOD_CACHE, new Pair<>(Duration.ofHours(1), JacksonHelper.genCollectionType(List.class, String.class))); this.configs.put(CouponConstants.COUPON_HANDLE_TELEPHONE_STATUS_CACHE, new Pair<>(Duration.ofMinutes(10), JacksonHelper.genCollectionType(List.class, CouponHandle.class))); this.configs.put(CouponConstants.COUPON_HANDLE_TELEPHONE_GOOD_CACHE, new Pair<>(Duration.ofMinutes(10), JacksonHelper.genCollectionType(List.class, CouponHandle.class))); } }
JacksonHelper 做用是根據不一樣的類型的對象獲取對應的JavaType對象,在構造RedisTempalte序列化和反序列化器Jackson2JsonRedisSerializer對象須要。具體代碼以下。
public class JacksonHelper { private static Logger LOGGER = LoggerFactory.getLogger(JacksonHelper.class); private static final SimpleModule module = initModule(); private static final ObjectMapper objectMapper; private static final ObjectMapper prettyMapper; public JacksonHelper() { } private static SimpleModule initModule() { return (new SimpleModule()).addSerializer(BigDecimal.class, new BigDecimalSerializer()) .addSerializer(LocalTime.class, new LocalTimeSerializer()) .addDeserializer(LocalTime.class, new LocalTimeDeserializer()) .addSerializer(LocalDate.class, new LocalDateSerializer()) .addDeserializer(LocalDate.class, new LocalDateDeserializer()) .addSerializer(LocalDateTime.class, new LocalDateTimeSerializer()) .addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer()) .addSerializer(Date.class, new DateSerializer()) .addDeserializer(Date.class, new DateDeserializer()); } public static JavaType genJavaType(TypeReference<?> typeReference) { return getObjectMapper().getTypeFactory().constructType(typeReference.getType()); } public static JavaType genJavaType(Class<?> clazz) { return getObjectMapper().getTypeFactory().constructType(clazz); } public static JavaType genCollectionType(Class<? extends Collection> collectionClazz, Class<?> javaClazz) { return getObjectMapper().getTypeFactory().constructCollectionType(collectionClazz, javaClazz); } public static JavaType genMapType(Class<? extends Map> mapClazz, Class<?> keyClass, Class<?> valueClazz) { return getObjectMapper().getTypeFactory().constructMapType(mapClazz, keyClass, valueClazz); } public static ObjectMapper getObjectMapper() { return objectMapper; } public static ObjectMapper getPrettyMapper() { return prettyMapper; } static { objectMapper = (new ObjectMapper()).registerModule(module).configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true).configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); prettyMapper = objectMapper.copy().configure(SerializationFeature.INDENT_OUTPUT, true); } } class LocalDateDeserializer extends JsonDeserializer<LocalDate> { public LocalDateDeserializer() { } public LocalDate deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { String dateString = ((JsonNode) jp.getCodec().readTree(jp)).asText(); return LocalDate.parse(dateString, DateTimeFormatter.ISO_LOCAL_DATE); } } class DateDeserializer extends JsonDeserializer<Date> { public DateDeserializer() { } public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { String dateTimeStr = ((JsonNode) jp.getCodec().readTree(jp)).asText(); SimpleDateFormat sdf = new SimpleDateFormat(CouponConstants.DATE_TIME_FORMATER); ParsePosition pos = new ParsePosition(0); return sdf.parse(dateTimeStr, pos); } } class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> { public LocalDateTimeDeserializer() { } public LocalDateTime deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { String dateTimeStr = ((JsonNode) jp.getCodec().readTree(jp)).asText(); return LocalDateTime.parse(dateTimeStr, DateTimeFormatter.ISO_LOCAL_DATE_TIME); } } class LocalTimeDeserializer extends JsonDeserializer<LocalTime> { public LocalTimeDeserializer() { } public LocalTime deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { String dateString = ((JsonNode) jp.getCodec().readTree(jp)).asText(); return LocalTime.parse(dateString, DateTimeFormatter.ISO_LOCAL_TIME); } } class BigDecimalSerializer extends JsonSerializer<BigDecimal> { public BigDecimalSerializer() { } public void serialize(BigDecimal value, JsonGenerator jgen, SerializerProvider provider) throws IOException { jgen.writeString(value.toString()); } } class LocalDateSerializer extends JsonSerializer<LocalDate> { public LocalDateSerializer() { } public void serialize(LocalDate value, JsonGenerator jgen, SerializerProvider provider) throws IOException { jgen.writeString(DateTimeFormatter.ISO_LOCAL_DATE.format(value)); } } class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> { public LocalDateTimeSerializer() { } public void serialize(LocalDateTime value, JsonGenerator jgen, SerializerProvider provider) throws IOException { jgen.writeString(DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(value)); } } class DateSerializer extends JsonSerializer<Date> { public DateSerializer() { } public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider) throws IOException { SimpleDateFormat sdf = new SimpleDateFormat(CouponConstants.DATE_TIME_FORMATER); jgen.writeString(sdf.format(value)); } } class LocalTimeSerializer extends JsonSerializer<LocalTime> { public LocalTimeSerializer() { } public void serialize(LocalTime value, JsonGenerator jgen, SerializerProvider provider) throws IOException { jgen.writeString(DateTimeFormatter.ISO_LOCAL_TIME.format(value)); } }
@Component public class ServiceImpl { @Override @CacheEvict(cacheNames = CouponConstants.COUPON_HANDLE_TELEPHONE_STATUS_CACHE, key = "#telephone+'#'+#status", beforeInvocation = true) public void evictCouponHandles(String telephone, Integer status) { } @Override @Cacheable(cacheNames = CouponConstants.COUPON_HANDLE_TELEPHONE_STATUS_CACHE, key = "#telephone+'#'+#status", sync = true) public List<CouponHandle> searchCouponHandles(String telephone, Integer status) { } }
不一樣的緩存對應不一樣的存儲類型,不一樣的存儲類型對應着不一樣的序列化和反序列化器,這就保證了再調用注有@Cacheable註解的代碼時獲取到的對象不會發生類型轉換錯誤。關於設置不一樣的cache下過時時間以及序列化和反序列器,請參考下面更直接明瞭的例子。
@Configuration public class RedisCacheConfig { @Bean public KeyGenerator simpleKeyGenerator() { return (o, method, objects) -> { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(o.getClass().getSimpleName()); stringBuilder.append("."); stringBuilder.append(method.getName()); stringBuilder.append("["); for (Object obj : objects) { stringBuilder.append(obj.toString()); } stringBuilder.append("]"); return stringBuilder.toString(); }; } @Bean public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) { return new RedisCacheManager( RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory), this.getRedisCacheConfigurationWithTtl(600), // 默認策略,未配置的 cache 會使用這個 this.getRedisCacheConfigurationMap() // 指定 cache 策略 ); } private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() { Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>(); redisCacheConfigurationMap.put("UserInfoList", this.getRedisCacheConfigurationWithTtl(3000)); redisCacheConfigurationMap.put("UserInfoListAnother", this.getRedisCacheConfigurationWithTtl(18000)); return redisCacheConfigurationMap; } private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) { Jackson2JsonRedisSerializer<Object> 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); RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig(); redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith( RedisSerializationContext .SerializationPair .fromSerializer(jackson2JsonRedisSerializer) ).entryTtl(Duration.ofSeconds(seconds)); return redisCacheConfiguration; } }
@Cacheable(value = "UserInfoList", keyGenerator = "simpleKeyGenerator") // 3000秒 @Cacheable(value = "UserInfoListAnother", keyGenerator = "simpleKeyGenerator") // 18000秒 @Cacheable(value = "DefaultKeyTest", keyGenerator = "simpleKeyGenerator") // 600秒,未指定的cache,使用默認策略