Spring cache支持多种类型缓存(事务敏感缓存)
Posted boonya
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring cache支持多种类型缓存(事务敏感缓存)相关的知识,希望对你有一定的参考价值。
目录
在线文档
spring包org.springframework.cache:
Java doc 请参考: Cache (Spring Framework 5.3.18 API)
注意:早期还支持Guava. 本篇仅介绍事务提交的缓存处理。
事务敏感缓存
这里以redis缓存为例。
代码:
核心redis事务管理:
public class TransactionAwareRedisCacheManager extends RedisCacheManager
public TransactionAwareRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration)
super(cacheWriter, defaultCacheConfiguration);
/**
* 是否使用事务敏感缓存
* @param name
* @param transactionAware
* @return
*/
public Cache getCache(String name, boolean transactionAware)
if(transactionAware)
return new TransactionAwareCacheDecorator(getCache(name));
else
return getCache(name);
config:
@Configuration
public class RedisConfig
@Bean
public CacheProperties cacheProperties()
return new CacheProperties();
@Bean
@Primary
public RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties)
CacheProperties.Redis redisProperties = cacheProperties.getRedis();
org.springframework.data.redis.cache.RedisCacheConfiguration config = org.springframework.data.redis.cache.RedisCacheConfiguration
.defaultCacheConfig();
// //指定缓存序列化方式为json
// 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);
//
// config = config.serializeValuesWith(
// RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer(om)));
//下面的这四个配置,如果我们不写在这里的话,将读取不到。默认的配置也会有的,但是我们自己写配置类的话,就需要加上去,
//设置配置文件中的各项配置,如过期时间
if (redisProperties.getTimeToLive() != null)
config = config.entryTtl(redisProperties.getTimeToLive());
//key的前缀
if (redisProperties.getKeyPrefix() != null)
config = config.prefixKeysWith(redisProperties.getKeyPrefix());
//是否缓存空值
if (!redisProperties.isCacheNullValues())
config = config.disableCachingNullValues();
//是否使用key的前缀
if (!redisProperties.isUseKeyPrefix())
config = config.disableKeyPrefix();
return config;
@Bean
public RedisCacheWriter redisCacheWriter(RedisConnectionFactory connectionFactory,MyBatchStrategy myBatchStrategy)
return new MyRedisCacheWriter(connectionFactory, Duration.ZERO,myBatchStrategy);
@Bean
public TransactionAwareRedisCacheManager transactionAwareRedisCacheManager(RedisConnectionFactory connectionFactory, MyBatchStrategy batchStrategy,RedisCacheConfiguration redisCacheConfiguration)
return new TransactionAwareRedisCacheManager(redisCacheWriter(connectionFactory, batchStrategy),redisCacheConfiguration);
service:
@Slf4j
@Service
public class TransactionAwareRedisCacheService
@Autowired
TransactionAwareRedisCacheManager transactionAwareRedisCacheManager;
@Transactional
public void put(String cacheName,String key,String value)
Cache cache = transactionAwareRedisCacheManager.getCache(cacheName,true);
Object object = cache.get(key).get();
log.info("====== object ====> ",(null== object? "":object.toString()));
cache.put(key,value);
调试过程:
项目地址:daily-code-cache: Spring cache的相关使用。
以上是关于Spring cache支持多种类型缓存(事务敏感缓存)的主要内容,如果未能解决你的问题,请参考以下文章