spring缓存集成

n1bvdmb6  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(374)

我想在Spring5应用程序中集成咖啡因缓存。我知道我需要咖啡馆经理,但我没有课。我找到的唯一方法是将springbootstarter缓存包添加到pom中,但我不想使用springboot。
我的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>

有没有办法让caffeenecachemanager类没有Spring Boot?谢谢。
实际上,我正在使用以下代码配置缓存:

@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;
}
7cjasjjr

7cjasjjr1#

主spring缓存实现位于spring上下文依赖中。您应该只设置该缓存管理器并使用@cacheable注解。有关示例,请参见:https://www.baeldung.com/spring-cache-tutorial 让我知道如果你需要连接缓存与Spring缓存机制的代码示例。
CaffineCacheManager配置:
CaffeeneCacheManager位于

<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;
    }
}

相关问题