SpringCache

目录

SpringCache

引入

自动配置

自定义配置

注解

@Cacheable

@CacheEvict​​

[sync = true](#sync = true)


SpringCache

集成与管理缓存,它通过声明式注解和统一的 API,将缓存逻辑与业务逻辑解耦,支持多种缓存实现(如 Redis、Ehcache、Caffeine 等),并提供了灵活的配置选项。

引入

XML 复制代码
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
复制代码
@EnableCaching开启缓存

自动配置

复制代码
CacheAutoConfiguration 依据缓存类型,自动配置缓存。
复制代码
CacheConfigurations 映射 相应的缓存配置
java 复制代码
    static {
        Map<CacheType, Class<?>> mappings = new EnumMap(CacheType.class);
        mappings.put(CacheType.GENERIC, GenericCacheConfiguration.class);
        mappings.put(CacheType.EHCACHE, EhCacheCacheConfiguration.class);
        mappings.put(CacheType.HAZELCAST, HazelcastCacheConfiguration.class);
        mappings.put(CacheType.INFINISPAN, InfinispanCacheConfiguration.class);
        mappings.put(CacheType.JCACHE, JCacheCacheConfiguration.class);
        mappings.put(CacheType.COUCHBASE, CouchbaseCacheConfiguration.class);
        mappings.put(CacheType.REDIS, RedisCacheConfiguration.class);
        mappings.put(CacheType.CAFFEINE, CaffeineCacheConfiguration.class);
        mappings.put(CacheType.SIMPLE, SimpleCacheConfiguration.class);
        mappings.put(CacheType.NONE, NoOpCacheConfiguration.class);
        MAPPINGS = Collections.unmodifiableMap(mappings);
    }
复制代码
RedisCacheConfiguration 为每一个缓存"分区"提供初始化,并返回Manager
java 复制代码
    @Bean
    public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory, ResourceLoader resourceLoader) {
        RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.builder(redisConnectionFactory).cacheDefaults(this.determineConfiguration(resourceLoader.getClassLoader()));
        List<String> cacheNames = this.cacheProperties.getCacheNames();
        if (!cacheNames.isEmpty()) {
            builder.initialCacheNames(new LinkedHashSet(cacheNames));
        }

        return (RedisCacheManager)this.customizerInvoker.customize(builder.build());
    }
复制代码
Manager为每一个分区进行默认配置defaultCacheConfiguration
java 复制代码
        public RedisCacheManagerBuilder initialCacheNames(Set<String> cacheNames) {
            Assert.notNull(cacheNames, "CacheNames must not be null!");
            Map<String, RedisCacheConfiguration> cacheConfigMap = new LinkedHashMap(cacheNames.size());
            cacheNames.forEach((it) -> {
                RedisCacheConfiguration var10000 = (RedisCacheConfiguration)cacheConfigMap.put(it, this.defaultCacheConfiguration);
            });
            return this.withInitialCacheConfigurations(cacheConfigMap);
        }
复制代码
redisCacheConfiguration 若为空,这采用默认配置
java 复制代码
    private org.springframework.data.redis.cache.RedisCacheConfiguration determineConfiguration(ClassLoader classLoader) {
        if (this.redisCacheConfiguration != null) {
            return this.redisCacheConfiguration;
        } else {
            CacheProperties.Redis redisProperties = this.cacheProperties.getRedis();
            org.springframework.data.redis.cache.RedisCacheConfiguration config = org.springframework.data.redis.cache.RedisCacheConfiguration.defaultCacheConfig();
            config = config.serializeValuesWith(SerializationPair.fromSerializer(new JdkSerializationRedisSerializer(classLoader)));
            if (redisProperties.getTimeToLive() != null) {
                config = config.entryTtl(redisProperties.getTimeToLive());
            }

            if (redisProperties.getKeyPrefix() != null) {
                config = config.prefixKeysWith(redisProperties.getKeyPrefix());
            }

            if (!redisProperties.isCacheNullValues()) {
                config = config.disableCachingNullValues();
            }

            if (!redisProperties.isUseKeyPrefix()) {
                config = config.disableKeyPrefix();
            }

            return config;
        }
    }

自定义配置

容器中注入一个 RedisCacheConfiguration

java 复制代码
@EnableCaching
@Configuration
public class MyCacheConfig {
    @Autowired
    private CacheProperties cacheProperties;
    @Bean
    RedisCacheConfiguration redisCacheConfiguration(){
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();

        config = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));
        //Json序列化
        config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericFastJsonRedisSerializer()));
        //使配置文件生效
        CacheProperties.Redis redisProperties = this.cacheProperties.getRedis();
        if (redisProperties.getTimeToLive() != null) {
            config = config.entryTtl(redisProperties.getTimeToLive());
        }

        if (redisProperties.getKeyPrefix() != null) {
            config = config.prefixKeysWith(redisProperties.getKeyPrefix());
        }

        if (!redisProperties.isCacheNullValues()) {
            config = config.disableCachingNullValues();
        }

        if (!redisProperties.isUseKeyPrefix()) {
            config = config.disableKeyPrefix();
        }
        return config;
    }
}
R 复制代码
spring.cache.type=redis

#缓存空值,防止缓存穿透
spring.cache.redis.cache-null-values=true
#spring.cache.redis.time-to-live=3600000
#spring.cache.redis.key-prefix=CACHE_
spring.cache.redis.use-key-prefix=true

注解

@Cacheable

缓存方法返回值,若缓存命中则直接返回结果,否则执行方法并缓存结果。

键名 默认格式 缓存名::SimpleKey[param1, param2,...],支持 SpEL 表达式

java 复制代码
@Cacheable(key = "#id")                 // 使用参数 id 作为键
@Cacheable(key = "#user.id")           // 使用对象属性作为键
@Cacheable(key = "#root.methodName")   // 使用方法名作为键[1,3](@ref)

默认使用jdk序列化机制,可在自定义配置中使用Json序列化器

@CacheEvict​​

清除缓存数据

sync = true

同步锁,本地锁,仅@Cacheable支持

java 复制代码
@Cacheable(cacheNames = "category",key = "#root.methodName",sync = true)
相关推荐
颜如玉14 分钟前
🤲🏻🤲🏻🤲🏻临时重定向一定要能重定向🤲🏻🤲🏻🤲🏻
java·http·源码
程序员的世界你不懂1 小时前
【Flask】测试平台开发,新增说明书编写和展示功能 第二十三篇
java·前端·数据库
星空寻流年1 小时前
设计模式第一章(建造者模式)
java·设计模式·建造者模式
gb42152872 小时前
java中将租户ID包装为JSQLParser的StringValue表达式对象,JSQLParser指的是?
java·开发语言·python
曾经的三心草3 小时前
Python2-工具安装使用-anaconda-jupyter-PyCharm-Matplotlib
android·java·服务器
Metaphor6923 小时前
Java 高效处理 Word 文档:查找并替换文本的全面指南
java·经验分享·word
ChinaRainbowSea3 小时前
7. LangChain4j + 记忆缓存详细说明
java·数据库·redis·后端·缓存·langchain·ai编程
stormsha3 小时前
飞算JavaAI炫技赛电商系统商品管理模块的架构设计与实现
java·架构·鸿蒙系统
minh_coo3 小时前
Spring框架事件驱动架构核心注解之@EventListener
java·后端·spring·架构·intellij-idea
翻滚丷大头鱼3 小时前
Java 集合Collection—List
java·开发语言