Spring boot 如何删除整个缓存,然后预填充缓存?
有人能告诉我下面的实现有什么问题吗。我试图删除整个缓存,其次,我想预填充/初始化缓存。但是,我下面介绍的只是在执行这两个方法时删除两个缓存,而不是预填充/启动缓存。有什么想法吗Spring boot 如何删除整个缓存,然后预填充缓存?,spring-boot,spring-cache,ehcache-3,Spring Boot,Spring Cache,Ehcache 3,有人能告诉我下面的实现有什么问题吗。我试图删除整个缓存,其次,我想预填充/初始化缓存。但是,我下面介绍的只是在执行这两个方法时删除两个缓存,而不是预填充/启动缓存。有什么想法吗 import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cachin
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
@Cacheable(cacheNames = "cacheOne")
List<User> cacheOne() throws Exception {...}
@Cacheable(cacheNames = "cacheOne")
List<Book> cacheTwo() throws Exception {...}
@Caching (
evict = {
@CacheEvict(cacheNames = "cacheOne", allEntries = true),
@CacheEvict(cacheNames = "CacheTwo", allEntries = true)
}
)
void clearAndReloadEntireCache() throws Exception
{
// Trying to reload cacheOne and cacheTwo inside this method
// Is this even possible? if not what is the correct approach?
cacheOne();
cacheTwo();
}
import org.springframework.cache.annotation.cacheexecute;
导入org.springframework.cache.annotation.Cacheable;
导入org.springframework.cache.annotation.Caching;
@可缓存(cacheNames=“cacheOne”)
List cacheOne()引发异常{…}
@可缓存(cacheNames=“cacheOne”)
List cacheTwo()引发异常{…}
@缓存(
逐出={
@cacheexecute(cacheNames=“cacheOne”,allEntries=true),
@cacheexecute(cacheNames=“CacheTwo”,allEntries=true)
}
)
void clearanderLoadEntireCache()引发异常
{
//正在尝试在此方法内重新加载cacheOne和Cache2
//这可能吗?如果不可能,正确的方法是什么?
cacheOne();
cacheTwo();
}
我开发了spring boot应用程序(v1.4.0),更重要的是,它利用了以下依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>1.0.0</version>
</dependency>
org.springframework.boot
springbootstarter缓存
org.ehcache
ehcache
3.3.0
javax.cache
缓存api
1.0.0
如果调用clearAndReloadEntireCache()
方法,缓存拦截器将只处理此方法。调用同一对象的其他方法:cacheOne()
和cacheTwo()
将不会在运行时导致缓存拦截,尽管它们都用@Cacheable
注释
通过使用如下所示的两个方法调用重新加载cacheOne和cacheTwo可以实现所需的功能:
@Caching(evict = {@CacheEvict(cacheNames = "cacheOne", allEntries = true, beforeInvocation = true)},
cacheable = {@Cacheable(cacheNames = "cacheOne")})
public List<User> cleanAndReloadCacheOne() {
return cacheOne();
}
@Caching(evict = {@CacheEvict(cacheNames = "cacheTwo", allEntries = true, beforeInvocation = true)},
cacheable = {@Cacheable(cacheNames = "cacheTwo")})
public List<Book> cleanAndReloadCacheTwo() {
return cacheTwo();
}
@Caching(execute={@cacheexecute(cacheNames=“cacheOne”,allEntries=true,beforecocation=true)},
cacheable={@cacheable(cacheNames=“cacheOne”)}
公共列表CleanLoadCacheOne(){
返回cacheOne();
}
@缓存(execute={@cacheexecute(cacheNames=“cacheTwo”,allEntries=true,beforeivocation=true)},
cacheable={@cacheable(cacheNames=“cacheTwo”)}
公共列表clean和loadcachetwo(){
返回cacheTwo();
}