简体   繁体   English

如果我的cacheResolver没有找到要使用的缓存,我应该返回什么?

[英]What should I return if my cacheResolver found no cache to use?

I'm using EhCache on a service's delegate with spring's annotations. 我在带有spring注释的服务委托上使用EhCache。

On my @Cacheable annotation I've added a resolverFactoryName field to resolve my cache at runtime. 在我的@Cacheable批注中,我添加了resolverFactoryName字段以在运行时解析我的缓存。

Then I implement CacheableCacheResolver interface, and return a cache depending of business rules, but sometimes I have no cache to return according to these rules. 然后,我实现CacheableCacheResolver接口,并根据业务规则返回缓存,但是有时我没有根据这些规则返回的缓存。

If I return null EhCache's lib gave me a NPE. 如果我返回null,则EhCache的lib给了我NPE。 What is the way to do when I've no cache to return and so the app should no cache any of these values ? 当我没有缓存要返回并且应用程序不应该缓存这些值时,该怎么办? Is there a "fake" cache to return? 是否有“假”缓存要返回? Is there something like a NoCacheException to throw? 是否有类似NoCacheException的东西抛出?

I do not think exception is the way to go, you really want to return a no-op cache implementation. 我不认为例外是要走的路,您真的想返回一个无操作缓存实现。

From the class name you give - CacheableCacheResolver - it looks like you are using ehcache-spring-annotations which does not seem to have support for what you want - a no-op cache. 从提供的类名CacheableCacheResolver看来,您正在使用的ehcache-spring-annotations似乎不支持您想要的内容-无操作缓存。

Note that you can still take care of it yourself. 请注意,您仍然可以自己照顾它。

If you were to use the Spring Caching abstraction, you could use a similar system which has support for no-op caches through org.springframework.cache.support.NoOpCacheManager.NoOpCache . 如果要使用Spring Caching抽象,则可以使用类似的系统 ,该系统通过org.springframework.cache.support.NoOpCacheManager.NoOpCache支持无操作缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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