跳至内容
Cache Caffeine Binder

Cache Caffeine Binder

基于 Caffeine 的本地 Spring Cache 实现。适合单实例开发和无需跨实例共享缓存的应用。

引入

<dependency>
  <groupId>io.github.loadup-cloud</groupId>
  <artifactId>loadup-components-kv-binder-caffeine</artifactId>
</dependency>

配置

loadup:
  cache:
    type: caffeine
    binder:
      caffeine:
        maximum-size: 10000

caffeine 是默认缓存类型;maximum-size 限制每个缓存实例的条目数。默认 TTL、按缓存名称的 TTL、随机过期和空值策略使用公共的 loadup.cache.* 配置,详见 Cache README。内部结构见 ARCHITECTURE.md。


设计与实现

职责与边界

缓存的具体后端适配模块;由集成方按需引入。

Maven 依赖边界

直接依赖的框架模块:

  • loadup-components-cache-api

直接依赖的外部坐标(不含测试与 provided scope):

  • org.springframework.boot:spring-boot-starter-cache
  • com.github.ben-manes.caffeine:caffeine

实现入口

主要入口文件:

分层与调用路径

Spring Cache 调用进入当前 CacheManager,由选中的 binder 处理缓存读写;切换后端不改变业务调用。

业务代码 → API/facade → 当前 binder → 第三方引擎或基础设施

binder 实现框架契约,把实现库及其配置隔离在业务 API 之外。

装配规则

  • CaffeineCacheAutoConfiguration 是自动配置入口。
    • @ConditionalOnClass(Caffeine.class)
    @ConditionalOnProperty( prefix = "loadup.cache", name = "type", havingValue = CacheBackendType.CAFFEINE, matchIfMissing = true)
    • @ConditionalOnMissingBean(CacheManager.class)

配置归属

设计取舍

选择独立 binder,使 Spring Cache 的实现依赖留在集成应用;替换底层实现时,业务侧仍使用 CacheManager 契约。

集成方式与配置示例见 README.md。