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的相关使用。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值