Spring Boot(11)——使用Spring Cache

本文涉及的产品
云原生内存数据库 Tair,内存型 2GB
云数据库 Redis 版,标准版 2GB
推荐场景:
搭建游戏排行榜
简介:

使用Spring Cache

Spring提供了Cache抽象,它允许我们声明哪些bean的哪些方法的外部调用需要使用Cache。方法调用使用了Cache后,在调用真实方法前会先从缓存中获取结果,缓存中如果没有则会调用真实方法,这也是基于AOP实现的。关于Spring Cache的介绍不是本文的重点,如有需要可以参考笔者写的http://elim.iteye.com/blog/2123030

在Spring Boot应用中使用Spring Cache需要在@SpringBootConfiguration标注的Class上添加@EnableCaching,这样就启用了Spring Cache。Spring Boot将根据Classpath下提供的Spring Cache实现类选择合适的实现者进行自动配置,支持的实现有基于Ehcache的实现、基于Redis的实现等,详情可参考org.springframework.boot.autoconfigure.cache.CacheConfiguration的源码。如果没有找到,则会使用基于ConcurrentMap的实现。

下面的代码中就启用了Spring Cache。

@SpringBootApplication
@EnableCaching
public class Application {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(Application.class);
        app.setAddCommandLineProperties(false);
        app.run(args);
    }

}

然后就可以在bean中使用Spring Cache提供的注解进行缓存的定义了,下面的代码中就定义了getTime()将使用名称为cacheName1的缓存。

@Component
public class SimpleService {

    @Cacheable("cacheName1")
    public long getTime() {
        return System.currentTimeMillis();
    }
    
}

简单的验证缓存生效的单元测试如下。

@SpringBootTest(classes=Application.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class CacheTest {

    @Autowired
    private SimpleService simpleService;
    
    @Test
    public void testGetTime() throws Exception {
        long time1 = this.simpleService.getTime();
        TimeUnit.MILLISECONDS.sleep(100);
        long time2 = this.simpleService.getTime();
        Assert.assertEquals(time1, time2);
    }
    
    
}

使用Ehcache实现

需要使用Spring Cache的Ehcache实现,首先需要在pom.xml中加入如下依赖。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
</dependency>

然后最简单的方式是在Classpath的根路径下放一个ehcache.xml文件,这样Spring Boot默认就会启用Spring Cache的Ehcache实现了。Ehcache的自动配置由EhCacheCacheConfiguration定义。如果Ehcache的配置文件不是存放在Classpath根路径,则可以通过spring.cache.ehcache.config来指定,比如下面的代码指定了在Classpath下的config目录下寻找ehcache.xml文件作为Ehcache的配置文件。

spring.cache.ehcache.config=classpath:/config/ehcache.xml

Spring Cache的自动配置的属性定义类是CacheProperties,参考其API文档或源码可以查看选择的Spring Cache的实现可以指定的详细的配置信息。

SimpleService的getTime()指定了使用的Cache是名称为cacheName1的Cache。所以在ehcache.xml中需要定义一个名为cacheName1的Cache,配置如下:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
    maxBytesLocalHeap="100M">
    <defaultCache maxElementsInMemory="10000" eternal="false"
        timeToIdleSeconds="120" timeToLiveSeconds="120"
        maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap" />
    </defaultCache>

    <cache name="cacheName1" />

</ehcache>

如果应用中应用了很多不同名称的Cache,如果都把它们在ehcache.xml中定义一次,可能你会觉得太麻烦。可能你想把一些主要的Cache在ehcache.xml中定义,进行充分的自定义配置。然后其它的则能够在使用的时候自动创建,并使用默认的默认的缓存配置,则可以定义自己的EhCacheCacheManager实现bean,实现getMissingCache()的逻辑为不存在则创建。这样Spring Boot将不再自动创建EhCacheCacheManager bean。下面的代码是一个简单的示例。

@Component
public class MyEhCacheCacheManager extends EhCacheCacheManager {

    @Override
    protected Cache getMissingCache(String name) {
        Cache cache = super.getMissingCache(name);
        if (cache == null) {
            Ehcache ehcache = super.getCacheManager().addCacheIfAbsent(name);
            cache = new EhCacheCache(ehcache);
        }
        return cache;
    }

}

使用Redis实现

需要使用Redis实现需要在pom.xml中添加spring-boot-starter-data-redis依赖,这样Spring Boot将自动创建RedisTemplate bean,有了RedisTemplate bean后Spring Boot将自动创建基于Redis实现的Spring Cache的CacheManager,RedisCacheManager。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

Spring Boot默认创建的RedisTemplate将使用localhost作为服务地址,端口号是6379,数据库索引为0,可以通过spring.redis.host指定Redis服务IP,spring.redis.port指定Redis服务监听端口号,spring.redis.database指定需要使用的数据库索引号。下面的代码中就指定了使用的是10.10.10.3这台主机上的Redis,数据库索引号是1。关于Redis的更多配置信息可以参考org.springframework.boot.autoconfigure.data.redis.RedisProperties的API文档。

spring.redis.host=10.10.10.3
spring.redis.database=1

默认Spring Cache的Redis实现存入Redis中的Key是cacheName::cacheKey的形式,其中cacheName是当前Spring Cache的name,cacheKey是Spring Cache传递过来的Key。可以通过spring.cache.redis.key-prefix指定存入Redis中的Key的前缀,当指定了该属性时,生成的Key将是该前缀加上Spring Cache传递过来的Key。比如下面的代码中指定了前缀是test-spring-cache::,如果Spring Cache传递的Key是key1,则最终存入Redis中的Key将是test-spring-cache::key1

spring.cache.redis.key-prefix=test-spring-cache::

可以通过spring.cache.redis.timeToLive指定Redis缓存的Key的存活时间。下面的代码中就指定了缓存的有效时间是60秒。

spring.cache.redis.timeToLive=60s

关于Spring Cache的Redis实现的更多配置可以参考org.springframework.boot.autoconfigure.cache.CacheProperties.Redis的API文档。

指定Cache实现

当Classpath下同时存放了多个Spring Cache的实现类,并且同时有多个Spring Cache实现类可以满足启用条件时,Spring Boot将按照一定的顺序进行选择,一旦应用了某个类型的Spring Cache实现后,其它类型的Spring Cache实现将是非启用的,因为这些自动启用的前提都要求当前环境中尚未有Spring Cache的CacheManager类型的bean。自动启用的顺序是按照org.springframework.boot.autoconfigure.cache.CacheType这个枚举中定义的顺序来的,它的定义如下。

    /**
     * Generic caching using 'Cache' beans from the context.
     */
    GENERIC,

    /**
     * JCache (JSR-107) backed caching.
     */
    JCACHE,

    /**
     * EhCache backed caching.
     */
    EHCACHE,

    /**
     * Hazelcast backed caching.
     */
    HAZELCAST,

    /**
     * Infinispan backed caching.
     */
    INFINISPAN,

    /**
     * Couchbase backed caching.
     */
    COUCHBASE,

    /**
     * Redis backed caching.
     */
    REDIS,

    /**
     * Caffeine backed caching.
     */
    CAFFEINE,

    /**
     * Simple in-memory caching.
     */
    SIMPLE,

    /**
     * No caching.
     */
    NONE

所以一旦Classpath下同时拥有Ehcache和Redis的Spring Cache实现时,将优先使用Ehcache的实现,如果想使用Redis的实现,可以通过spring.cache.type=redis指定使用Redis的实现。

指定Cache实现仅针对于自动配置生效,如果是自己定义了CacheManager实现,则该配置无效。

CacheManagerCustomizer

CacheManagerCustomizer是用来对CacheManager进行自定义扩展的,其定义如下:

@FunctionalInterface
public interface CacheManagerCustomizer<T extends CacheManager> {

    /**
     * Customize the cache manager.
     * @param cacheManager the {@code CacheManager} to customize
     */
    void customize(T cacheManager);

}

当需要对某个CacheManager实现进行一些自定义时,可以实现CacheManagerCustomizer接口,指定泛型为需要进行自定义的CacheManager实现类,然后把它定义为一个Spring bean。下面的代码就对Ehcache实现的CacheManager进行了一些自定义,其在EhCacheCacheManager初始化后采用默认的Cache配置创建了一些Cache。

@Component
public class EhcacheCacheManagerCustomizer implements CacheManagerCustomizer<EhCacheCacheManager> {

    @Override
    public void customize(EhCacheCacheManager cacheManager) {
        List<String> cacheNames = Arrays.asList("cacheName1", "cacheName2", "cacheName3", "cacheName4", "cacheName5", "cacheName6");
        cacheNames.forEach(cacheManager.getCacheManager()::addCacheIfAbsent);
    }

}

关于Spring Cache的自动配置可以参考org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration的源码或API文档。

(注:本文是基于Spring Boot 2.0.3所写)

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
24天前
|
缓存 NoSQL Java
SpringBoot的三种缓存技术(Spring Cache、Layering Cache 框架、Alibaba JetCache 框架)
Spring Cache 是 Spring 提供的简易缓存方案,支持本地与 Redis 缓存。通过添加 `spring-boot-starter-data-redis` 和 `spring-boot-starter-cache` 依赖,并使用 `@EnableCaching` 开启缓存功能。JetCache 由阿里开源,功能更丰富,支持多级缓存和异步 API,通过引入 `jetcache-starter-redis` 依赖并配置 YAML 文件启用。Layering Cache 则提供分层缓存机制,需引入 `layering-cache-starter` 依赖并使用特定注解实现缓存逻辑。
127 1
SpringBoot的三种缓存技术(Spring Cache、Layering Cache 框架、Alibaba JetCache 框架)
|
12天前
|
Java 微服务 Spring
SpringBoot+Vue+Spring Cloud Alibaba 实现大型电商系统【分布式微服务实现】
文章介绍了如何利用Spring Cloud Alibaba快速构建大型电商系统的分布式微服务,包括服务限流降级等主要功能的实现,并通过注解和配置简化了Spring Cloud应用的接入和搭建过程。
SpringBoot+Vue+Spring Cloud Alibaba 实现大型电商系统【分布式微服务实现】
|
2月前
|
监控 Java 应用服务中间件
什么是 Spring Boot,及为什么要用 Spring Boot
**Spring Boot**: 2013年起研,简化Spring笨重配置,集成常用库,开箱即用,少代码配置,专注业务。 **为何选Spring Boot?** 出色基因,快速搭建;单一依赖替多;Java Config简化配置;内嵌Tomcat,简化部署;监控REST化;微服务友好,趋势之选。
85 27
|
16天前
|
安全 Java 数据安全/隐私保护
基于SpringBoot+Spring Security+Jpa的校园图书管理系统
本文介绍了一个基于SpringBoot、Spring Security和JPA开发的校园图书管理系统,包括系统的核心控制器`LoginController`的代码实现,该控制器处理用户登录、注销、密码更新、角色管理等功能,并提供了系统初始化测试数据的方法。
23 0
基于SpringBoot+Spring Security+Jpa的校园图书管理系统
|
18天前
|
安全 Java 数据库
|
18天前
|
JSON 安全 Java
|
2月前
|
Java Spring 容器
Spring Boot 启动源码解析结合Spring Bean生命周期分析
Spring Boot 启动源码解析结合Spring Bean生命周期分析
67 11
|
6天前
|
Java Spring
【Azure 事件中心】Spring Boot 集成 Event Hub(azure-spring-cloud-stream-binder-eventhubs)指定Partition Key有异常消息
【Azure 事件中心】Spring Boot 集成 Event Hub(azure-spring-cloud-stream-binder-eventhubs)指定Partition Key有异常消息
|
2月前
|
XML JSON Java
spring,springBoot配置类型转化器Converter以及FastJsonHttpMessageConverter,StringHttpMessageConverter 使用
spring,springBoot配置类型转化器Converter以及FastJsonHttpMessageConverter,StringHttpMessageConverter 使用
211 1
|
18天前
|
前端开发 Java Spring
Java 新手入门:Spring Boot 轻松整合 Spring 和 Spring MVC!
Java 新手入门:Spring Boot 轻松整合 Spring 和 Spring MVC!
37 0
下一篇
云函数