Spring 带预定注释的缓存逐出
Spring cache eviction with scheduled annotation
在下面的代码中缓存有效,但驱逐无效,你能接近我吗?
我已阅读以下内容 link:
@Component
@RequiredArgsConstructor
public class CacheInvalidator {
@Scheduled(fixedRate = 1000L)
public void evictCache() {
clearCache();
}
@CacheEvict(value = "count", allEntries = true)
public void clearCache() {
}
}
@Component
@RequiredArgsConstructor
public class ARepositoryImpl implements ARepository {
@Cacheable(value = "count")
public Integer count() {
return jdbcTemplate.queryForObject("...", Integer.class);
}
}
@SpringBootApplication
@EnableMongoRepositories
@EnableScheduling
@EnableCaching
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
在pom.xml中:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
它不起作用,因为 CacheInvalidator
中的方法 evictCache
调用 clearCache
不是在 Spring 的代理上,而是在原始对象上,并且该方法尽管被注释但什么也不做@CacheEvict(value = "count", allEntries = true)
。
而不是:
@Component
@RequiredArgsConstructor
public class CacheInvalidator {
@Scheduled(fixedRate = 1000L)
public void evictCache() {
clearCache();
}
@CacheEvict(value = "count", allEntries = true)
public void clearCache() {
}
}
尝试:
@Component
@RequiredArgsConstructor
public class CacheInvalidator {
@Scheduled(fixedRate = 1000L)
@CacheEvict(value = "count", allEntries = true)
public void clearCache() {
}
}
在下面的代码中缓存有效,但驱逐无效,你能接近我吗?
我已阅读以下内容 link:
@Component
@RequiredArgsConstructor
public class CacheInvalidator {
@Scheduled(fixedRate = 1000L)
public void evictCache() {
clearCache();
}
@CacheEvict(value = "count", allEntries = true)
public void clearCache() {
}
}
@Component
@RequiredArgsConstructor
public class ARepositoryImpl implements ARepository {
@Cacheable(value = "count")
public Integer count() {
return jdbcTemplate.queryForObject("...", Integer.class);
}
}
@SpringBootApplication
@EnableMongoRepositories
@EnableScheduling
@EnableCaching
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
在pom.xml中:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
它不起作用,因为 CacheInvalidator
中的方法 evictCache
调用 clearCache
不是在 Spring 的代理上,而是在原始对象上,并且该方法尽管被注释但什么也不做@CacheEvict(value = "count", allEntries = true)
。
而不是:
@Component
@RequiredArgsConstructor
public class CacheInvalidator {
@Scheduled(fixedRate = 1000L)
public void evictCache() {
clearCache();
}
@CacheEvict(value = "count", allEntries = true)
public void clearCache() {
}
}
尝试:
@Component
@RequiredArgsConstructor
public class CacheInvalidator {
@Scheduled(fixedRate = 1000L)
@CacheEvict(value = "count", allEntries = true)
public void clearCache() {
}
}