[英]Spring Cache: How to specify global @CacheConfig for application
I am working on setting up Spring Cache support for my Java application. 我正在为我的Java应用程序设置Spring Cache支持。 I want a way to set a global @CacheConfig for my entire project because I want all cache operations in the application to use the exact same cache name and custom KeyGenerator implementation . 我想要一种为整个项目设置全局@CacheConfig的方法,因为我希望应用程序中的所有缓存操作都使用完全相同的缓存名称和自定义KeyGenerator实现 。 I was hoping that I could annotate some global configuration class or my CacheManager
with @CacheConfig
, but it looks like @CacheConfig
is only meant as configuration for the class of the method with the Cache Annotation. 我希望我可以使用@CacheConfig
注释某些全局配置类或CacheManager
,但是看起来@CacheConfig
仅意味着使用Cache Annotation进行方法类的配置。 Thus, I would need to use the same exact @CacheConfig
annotation definition for every class using Spring Cache operations. 因此,对于使用Spring Cache操作的每个类,我将需要使用完全相同的@CacheConfig
批注定义。
A workaround that I'm going to use in the meanwhile is implement my own meta-annotation of Spring Cache annotations such as @Cacheable
, so that the same exact name
and keyGenerator
will be used for all cache operations. 同时,我要使用的一种解决方法是实现自己的Spring Cache注释(例如@Cacheable
的元注释 ,以便所有缓存操作都使用相同的确切name
和keyGenerator
。 But, there must be a better way to do this than using custom annotations. 但是,必须有比使用自定义注释更好的方法。 If I ever run into the situation where I want to use a Cache named something other than "default", then I'm going to run into trouble with my custom annotation. 如果我曾经遇到过要使用一个名为“ default”以外的名称的Cache的情况,那么我的自定义注释会遇到麻烦。
Any ideas? 有任何想法吗? Thank you. 谢谢。
Yes, this is doable. 是的,这是可行的。
All you need is to populate AnnotationCacheOperationSource with your own CacheAnnotationParser
, see how is it done by default: SpringCacheAnnotationParser . 您所需要做的就是用您自己的CacheAnnotationParser
填充AnnotationCacheOperationSource ,看看默认情况下它是如何完成的: SpringCacheAnnotationParser 。
Unfortunately I can't provide you with code snippets, my Spring-fu is too weak at this moment. 不幸的是,我无法为您提供代码片段,我的Spring-fu目前太弱了。 If you manage to get this working - let me know. 如果您设法解决此问题,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.