1.2. 将 Data Grid 用作 Spring Cache 供应商


@EnableCaching 注释添加到您的其中一个配置类,然后添加 @Cacheable@CacheEvict 注释以使用远程或嵌入式缓存。

先决条件

  • 将 Data Grid 依赖项添加到应用程序项目中。
  • 使用 Data Grid Server 部署,请创建所需的远程缓存并配置 Hot Rod 客户端属性。

流程

  1. 使用以下方法之一在应用程序上下文中启用缓存注解:

    声明

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:cache="http://www.springframework.org/schema/cache"
        xmlns:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
    
            <cache:annotation-driven />
    
    </beans>
    Copy to Clipboard Toggle word wrap

    programmatic

    @EnableCaching @Configuration
    public class Config {
    }
    Copy to Clipboard Toggle word wrap

  2. 注解具有 @Cacheable 的方法,以缓存返回值。

    提示

    要直接引用缓存中的条目,您必须包含 key 属性。

  3. 使用 @CacheEvict 注解方法,从缓存中删除旧条目。
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2026 Red Hat
返回顶部