美文网首页
自定义Spring Cache的key

自定义Spring Cache的key

作者: Clang的技术博客 | 来源:发表于2021-09-14 13:13 被阅读0次

    1、继承RedisCacheManager

    public class RedisAutoCacheManager extends RedisCacheManager {
        public RedisAutoCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration) {
            super(cacheWriter, defaultCacheConfiguration);
        }
    
        /**
         * 从上下文中获取租户ID,重写@Cacheable value值
         * @param name
         * @return
         */
        @Override
        public Cache getCache(String name) {
            return super.getCache( name + StrUtil.COLON + TenantContextHolder.getTenantId());
        }
    }
    

    2、在Spring Boot启动类中注入容器

    @Bean
    public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
        // 初始化一个RedisCacheWriter
        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
    
        // 设置CacheManager的值序列化方式为json序列化
        RedisSerializer<Object> jsonSerializer = new GenericFastJsonRedisSerializer();
        RedisSerializationContext.SerializationPair<Object> pair = RedisSerializationContext.SerializationPair.fromSerializer(jsonSerializer);
        RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(pair);
    
        // 初始化RedisCacheManager
        return new RedisAutoCacheManager(redisCacheWriter, defaultCacheConfig);
    }
    

    3、租户类

    public class TenantContextHolder {
    
        private static final ThreadLocal<Integer> CONTEXT = new ThreadLocal<>();
    
        public static void setTenantId(Integer tenantId) {
            CONTEXT.set(tenantId);
        }
    
        public static Integer getTenantId() {
            return CONTEXT.get();
        }
    
        public static void clear() {
            CONTEXT.remove();
        }
    }
    

    Clang的技术博客:https://www.clang.asia

    相关文章

      网友评论

          本文标题:自定义Spring Cache的key

          本文链接:https://www.haomeiwen.com/subject/ivjcgltx.html