简体   繁体   中英

Spring Cache: How to specify global @CacheConfig for application

I am working on setting up Spring Cache support for my Java application. 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 . 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. Thus, I would need to use the same exact @CacheConfig annotation definition for every class using Spring Cache operations.

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. 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.

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 .

Unfortunately I can't provide you with code snippets, my Spring-fu is too weak at this moment. If you manage to get this working - let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM