我想在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;
}
1条答案
按热度按时间7cjasjjr1#
主spring缓存实现位于spring上下文依赖中。您应该只设置该缓存管理器并使用@cacheable注解。有关示例,请参见:https://www.baeldung.com/spring-cache-tutorial 让我知道如果你需要连接缓存与Spring缓存机制的代码示例。
CaffineCacheManager配置:
CaffeeneCacheManager位于
以及咖啡因依赖:
配置: