你整天都在讨论使用SpringBoot,可居然连缓存都不清楚?


前言

缓存技术是一个让所有开发人员又爱又恨的技术,我们爱缓存是因为缓存能给我们带来数量级的响应和流量,但是最迷人的反而最危险,如果缓存用不好也是灾难级别的,特别是一些涉及到公司主要现金流的业务,如果因为我们使用缓存不当,而带给公司一定的损失,不亚于删库跑路的那个大兄弟,那今天我们就来看一下springboot的缓存都有那些东西,学习嘛,一点点地来,慢慢积累自己的经验,才能厚积薄发

一、JSR107缓存规范

为了缓存开发规范的统一,以及提升系统的扩展性,J2EE发布了JSR107缓存规范。 主要是Java Caching定义了5个接口,分别是CachingProvider、CacheManager、Cache、Entry、Expiry。

下面我们分开详细地展开看一下

你整天都在讨论使用SpringBoot,可居然连缓存都不清楚?

image

简单总结一下就是:一个应用里面可以有多个缓存提供者(CachingProvider),一个缓存提供者可以获取到多个缓存管理器(CacheManager),一个缓存管理器管理着不同的缓存(Cache),缓存中是一个个的缓存键值对(Entry),每个entry都有一个有效期(Expiry)。缓存管理器和缓存之间的关系有点类似于数据库中连接池和连接的关系。

二、SpringBoot缓存抽象

在我自己看来,没有源码所有的理论讲解,都是空谈,或者说就是扯淡,所以我们来看一下,缓存的源码级操作

Spring从3.1版本开始定义了org.springframework.cache.CacheManagerorg.springframework.cache.Cache接口来统一不同的缓存技术,并支持使用JSR-107注解简化开发。 在IDEA中,使用Spring Initializr快速创建Spring Boot项目时,勾选中Cache后,在配置文件中配置debug=true,可以查看Spring Boot的自动配置项。 其中关于缓存的配置类如下:

org.springframework.boot.autoconfigure.cache.GenericCacheConfiguration
org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration
org.springframework.boot.autoconfigure.cache.EhCacheCacheConfiguration
org.springframework.boot.autoconfigure.cache.HazelcastCacheConfiguration
org.springframework.boot.autoconfigure.cache.InfinispanCacheConfiguration
org.springframework.boot.autoconfigure.cache.CouchbaseCacheConfiguration
org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration
org.springframework.boot.autoconfigure.cache.CaffeineCacheConfiguration
org.springframework.boot.autoconfigure.cache.GuavaCacheConfiguration
org.springframework.boot.autoconfigure.cache.SimpleCacheConfiguration
org.springframework.boot.autoconfigure.cache.NoOpCacheConfiguration

启动项目后,可以在控制台看到匹配到的只有SimpleCacheConfiguration这个自动配置类,而在SimpleCacheConfiguration类中,使用@Bean注解给容器中注册了一个CacheManager,由此可看Spring Boot默认的CacheManager是ConcurrentMapCacheManager。

 SimpleCacheConfiguration matched:
      - Cache org.springframework.boot.autoconfigure.cache.SimpleCacheConfiguration automatic cache type (CacheCondition)
      - @ConditionalOnMissingBean (types: org.springframework.cache.CacheManager; SearchStrategy: all) did not find any beans (OnBeanCondition)

同样的,我们通过一张图形象地展示一下看看

你整天都在讨论使用SpringBoot,可居然连缓存都不清楚?

image

几个重要概念和缓存注解:

你整天都在讨论使用SpringBoot,可居然连缓存都不清楚?

进入@Caching的源码可以看到,在组合注解内可以使用cacheable、put、evict

public @interface Caching {
    Cacheable[] cacheable() default {};

    CachePut[] put() default {};

    CacheEvict[] evict() default {};
}

@Caching的使用

 @Caching(
            cacheable = {
                    @Cacheable(key = "#name")
            },
            put = {
                    @CachePut(key = "#result.id"),
                    @CachePut(key = "#result.cNo")
            }
    )

@Cacheable、@CachePut、@CacheEvict中的主要参数

key

#缓存的key,可以为空,也可以使用SpEL表达式编写
例:@Cacheable(value=“stu”,key=“userName”)

condition

#缓存的条件,可以为空,也可以使用SpEL表达式编写,只有为true才缓存/清除缓存,
#不管方法执行前后都可以判断
例:@Cacheable(value=“stu”,condition=“userName.length()>2”)

unless

#用于否定缓存,只在方法执行之后判断,也可以使用SpEL表达式编写
#true不缓存,false才缓存
例:@Cacheable(value=“stu”,unless=“userName == null”)

展开阅读全文

页面更新:2024-03-14

标签:新和   缓存   注解   表达式   提供者   条目   管理器   源码   有效期   定义   参数   代表   方法   数据   技术   科技

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top