Reputation: 566
I used in my project configuration bean for cache evict and it's not runs. I use this some class in another projects and works fine but I don't know where is the problem now.
@Configuration
@Slf4j
public class CacheConfig {
public static final String BANKCODE_CACHE_NAME = "cacheName";
@CacheEvict(allEntries = true, cacheNames = { CACHE_NAME })
@Scheduled(fixedRate = 5000)
public void cachePosEvict() {
log.info("Evicting cache: {}", CACHE_NAME);
}
}
Problem is probably somewhere else with this config bean, because also when I use:
@PostConstruct
void init() {
log.info("Init...");
}
Then is nothing in log. I looked into TRACE spring logs and there is no error, class is in the classpath. I don't know where can be a problem.
I have following dependencies in gradle:
plugins {
id 'org.springframework.boot' version '2.3.1.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
ext {
set('springCloudVersion', "Hoxton.SR6")
webfluxUiVersion = "1.3.9"
jacksonVersion = "2.10.1"
logbackJson = "0.1.5"
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-hystrix'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation "org.springdoc:springdoc-openapi-webflux-ui:${webfluxUiVersion}"
implementation "com.fasterxml.jackson.core:jackson-core:${jacksonVersion}"
implementation "com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}"
implementation "com.fasterxml.jackson.core:jackson-annotations:${jacksonVersion}"
implementation "com.fasterxml.jackson.dataformat:jackson-dataformat-xml:${jacksonVersion}"
implementation "ch.qos.logback.contrib:logback-json-classic:${logbackJson}"
implementation "ch.qos.logback.contrib:logback-jackson:${logbackJson}"
I use Java 11 with Main class:
@ConfigurationPropertiesScan
@SpringBootApplication
@EnableCaching
@EnableScheduling
public class MyApp{...}
EDIT: I found that problem is in my configuration:
main:
lazy-initialization: true
I thought that bean will be created when Scheduler is active.
Upvotes: 0
Views: 1660
Reputation: 566
Solution was in disable feature:
spring:
main:
lazy-initialization: false
Upvotes: 1
Reputation: 1
Looks like you are using a webflux, you might need to return a publisher (Mono or Flux) that can be subscribed to, for example:
public Mono<Void> cachePosEvict() {
log.info("Evicting cache: {}", CACHE_NAME);
}
Upvotes: 0
Reputation: 905
You periodically evict all entries from your cache. Either you have a very specific use case or you're doing it wrong.
I assume that you use Caffeine implementation. In this case consider configuring cache expiration through a dedicated property:
spring.cache.caffeine.spec=expireAfterWrite=5s
More information here:
Upvotes: 0