java 安排 Spring 缓存驱逐?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42688020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Schedule Spring cache eviction?
提问by Philippe Gioseffi
Is it possible to schedule spring cache eviction to everyday at midnight?
是否可以将春季缓存驱逐安排到每天午夜?
I've read Springs Cache Docsand found nothing about scheduled cache eviction.
我已经阅读了Springs Cache Docs并没有发现任何关于计划缓存驱逐的信息。
I need to evict cache daily and recache it in case there were some changes outside my application.
我需要每天清除缓存并重新缓存,以防我的应用程序外部发生一些变化。
回答by Oleksandr Bondarchuk
Try to use @Scheduled Example:
尝试使用@Scheduled 示例:
@Scheduled(fixedRate = ONE_DAY)
@CacheEvict(value = { CACHE_NAME })
public void clearCache() {
}
You can also use cron expression with @Scheduled.
您还可以将 cron 表达式与 @Scheduled 一起使用。
回答by Johannes Di
If you use @Cacheable on methods with parameters, you should NEVER forget the allEntries=trueannotation property on the @CacheEvict, otherwise your call will only evict the key parameter you give to the clearCache() method, which is nothing => you will not evict anything from the cache.
如果您在带参数的方法上使用@Cacheable,则永远不要忘记@CacheEvict 上的 allEntries=true注释属性,否则您的调用只会驱逐您提供给 clearCache() 方法的关键参数,这没什么=>不要从缓存中驱逐任何东西。
回答by Bond - Java Bond
Spring cache framework is event driven i.e. @Cacheable
or @CacheEvict
will be triggered only when respective methods are invoked.
Spring 缓存框架是事件驱动的,即@Cacheable
或@CacheEvict
仅在调用相应方法时才会触发。
However you can leverage the underlying cache provider (remember the Spring cache framework is just an abstraction and does not provide a cache solution by itself) to invalidate the cache by itself. For instance EhCache has a property viz. timeToLiveSeconds
which dictates the time till the cache be active. But this won't re-populate the cache for you unless the @Cacheable
annotated method is invoked.
但是,您可以利用底层缓存提供程序(记住 Spring 缓存框架只是一个抽象,本身不提供缓存解决方案)来使缓存本身无效。例如 EhCache 有一个属性即。timeToLiveSeconds
这决定了缓存处于活动状态之前的时间。但这不会为您重新填充缓存,除非@Cacheable
调用了带注释的方法。
So for cache eviction and re-population at particular time (say midnight as mentioned) consider implementing a background scheduled servicein Spring which will trigger the cache eviction and re-population as desired. The expected behavior is not provided out-of-box.
因此,对于特定时间的缓存驱逐和重新填充(如提到的午夜),请考虑在 Spring 中实现后台计划服务,这将根据需要触发缓存驱逐和重新填充。预期的行为不是开箱即用的。
Hope this helps.
希望这可以帮助。
回答by bugsbuRny
I know this question is old, but I found a better solution that worked for me. Maybe that will help others.
我知道这个问题很老,但我找到了一个对我有用的更好的解决方案。也许这会帮助其他人。
So, it is indeed possible to make a scheduled cache eviction. Here is what I did in my case.
因此,确实可以进行预定的缓存逐出。这是我在我的案例中所做的。
Both annotations @Scheduled and @CacheEvict do not seem to work together. You must thus split apart the scheduling method and the cache eviction method. But since the whole mechanism is based on proxies, only external calls to public methods of your class will trigger the cache eviction. This because internal calls between to methods of the same class do not go through the Spring proxy.
注释@Scheduled 和@CacheEvict 似乎不能一起工作。因此,您必须将调度方法和缓存逐出方法分开。但是由于整个机制是基于代理的,因此只有对类的公共方法的外部调用才会触发缓存驱逐。这是因为同一类的方法之间的内部调用不通过 Spring 代理。
I managed to fixed it the same way as Celebes (see comments), but with an improvement to avoid two components.
我设法以与 Celebes 相同的方式修复它(见评论),但进行了改进以避免两个组件。
@Component
class MyClass
{
@Autowired
MyClass proxiedThis; // store your component inside its Spring proxy.
// A cron expression to define every day at midnight
@Scheduled(cron ="0 0 * * *")
public void cacheEvictionScheduler()
{
proxiedThis.clearCache();
}
@CacheEvict(value = { CACHE_NAME })
public void clearCache()
{
// intentionally left blank. Or add some trace info.
}
}