Spring 无需 Spring 启动即可缓存 Caffeine 集成

Spring cache Caffeine integration without SpringBoot

我想在我的 Spring 5 应用程序中集成 Caffeine Cache。我知道我需要 CaffeineCacheManager,但我没有 class。我找到的唯一方法是将 SpringBoot Starter Cache 包添加到 pom,但我不想使用 Spring Boot.

我的 pom 中有这个 Spring 包:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>5.2.8.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>5.2.8.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.2.8.RELEASE</version>
</dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.8.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-web -->
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-web</artifactId>
    <version>5.3.3.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-config -->
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-config</artifactId>
    <version>5.3.3.RELEASE</version>
</dependency>

有什么方法可以在 Spring 启动 的情况下使用 CaffeineCacheManager class?谢谢

实际上我正在使用这段代码来配置缓存:

@Bean
public CacheManager cacheManager() {
    
    SimpleCacheManager cacheManager = new SimpleCacheManager();
    cacheManager.setCaches(Arrays.asList(
        new ConcurrentMapCache(
                "categorias",
                Caffeine.newBuilder().expireAfterWrite(Duration.ofDays(1)).maximumSize(100).build().asMap(),
                false
        ),
        new ConcurrentMapCache(
                "ciudad",
                Caffeine.newBuilder().maximumSize(100).build().asMap(),
                false
        )
    ));
    
    return cacheManager;
}

主要的 spring 缓存实现位于 spring-context 依赖项中。您应该只设置该缓存管理器并使用 @Cacheable 注释。 有关示例,请参见:https://www.baeldung.com/spring-cache-tutorial 如果您需要使用 spring 缓存机制连接 Caffeine 缓存的代码示例,请告诉我。

CaffeineCacheManager 配置:

CaffeineCacheManager 位于

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>5.2.8.RELEASE</version>
</dependency>

和咖啡因依赖:

<dependency>
    <groupId>com.github.ben-manes.caffeine</groupId>
    <artifactId>caffeine</artifactId>
    <version>2.8.8</version>
</dependency>

配置:

@Configuration
@EnableCaching
public class Config {
    @Bean
    public Caffeine<Object, Object> caffeineConfig() {
        return Caffeine.newBuilder().expireAfterWrite(60, TimeUnit.MINUTES);
    }
    @Bean
    public CacheManager cacheManager(Caffeine<Object, Object> caffeine) {
        CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager();
        caffeineCacheManager.setCaffeine(caffeine);
        return caffeineCacheManager;
    }
}