- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我在 Spring Boot 中遇到了这个奇怪的问题,其中 @Cacheable
在 Controller 中工作但不在服务内部。我可以在 Redis 中看到 GET 调用,但看不到 PUT 调用。
这是有效的,因为它在 Controller 内部
@RestController
@RequestMapping(value="/places")
public class PlacesController {
private AwesomeService awesomeService;
@Autowired
public PlacesController(AwesomeService awesomeService) {
this.awesomeService = awesomeService;
}
@GetMapping(value = "/search")
@Cacheable(value = "com.example.webservice.controller.PlacesController", key = "#query", unless = "#result != null")
public Result search(@RequestParam(value = "query") String query) {
return this.awesomeService.queryAutoComplete(query);
}
}
但是当我在服务中这样做时,@Cacheable
不工作
@Service
public class AwesomeApi {
private final RestTemplate restTemplate = new RestTemplate();
@Cacheable(value = "com.example.webservice.api.AwesomeApi", key = "#query", unless = "#result != null")
public ApiResult queryAutoComplete(String query) {
try {
return restTemplate.getForObject(query, ApiResult.class);
} catch (Throwable e) {
return null;
}
}
}
我可以在 Redis 中看到 GET
调用,但看不到 PUT
调用。
最佳答案
您的缓存应该可以正常工作。确保您有 @EnableCaching
注释并且您的 unless
条件是正确的。
现在,您正在使用 unless="#result != null"
,这意味着它将缓存结果,除非它不是 null
。这意味着它几乎永远不会缓存,除非 restTemplate.getForObject()
返回 null
,或者发生异常时,因为那时您也将返回 null
。
我假设您想缓存每个值,null
除外,但在那种情况下您必须反转您的条件,例如:
@Cacheable(
value = "com.example.webservice.api.AwesomeApi",
key = "#query",
unless = "#result == null") // Change '!=' into '=='
或者,as mentioned in the comments ,而不是反转条件,您可以使用 condition
代替 unless
:
@Cacheable(
value = "com.example.webservice.api.AwesomeApi",
key = "#query",
condition = "#result != null") // Change 'unless' into 'condition'
关于spring-boot - @Cacheable 在 Controller 中工作,但不在服务内部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51373323/
我正在尝试使用 Spring Cacheable,但遇到了类转换异常 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(cla
我就废话不多说了,大家还是直接看代码吧~ ? 1
我有这个实用方法来加载我的网络应用程序的内容 @Cacheable(value="dataStrore" ) public MarketJson retrieveMarketJson(DataRequ
与内存数据库一起使用@Cacheable是否相关? 我在内存数据库中使用 h2,只是想知道使用 @Cacheable 可以提高应用程序的性能。我的应用程序将频繁调用数据库中的静态数据 最佳答案 一般来
将 Spring 3.2 与 EhCache 2.9 一起使用。我已经注释了一个零参数方法如下: @Cacheable(value="myList", key="#result.method.name
我正在尝试将 Redis 与 Spring 的 @Cacheable 一起使用,但需要根据 Spring Boot 样式的应用程序属性有条件地打开或关闭缓存。我的第一次尝试似乎没有用。 applica
我在 Spring 3.2 中使用 @Cacheable 来缓存服务层方法的结果。服务类内部使用以下方法代码: @Cacheable("questions") public List getSecut
如何查看@Cacheable 的实现。我想了解他们如何能够获取方法参数的名称并在运行时获取其实际值。 例如: @Cacheable(cacheNames = "name", key = "#k
我正在努力在 Spring Boot 集成测试中测试 @Cacheable。这是我学习如何进行集成测试的第二天,我发现的所有示例都使用旧版本。我还看到了 assetEquals("some value
背景 项目中,使用@Cacheable进行数据缓存。发现:当redis宕机之后,@Cacheable注解的方法并未进行缓存冲突,而是直接抛出异常。而这样的异常会导致服务不可用。 原因分析 我们
我在 Spring 4.1.4 应用程序中使用最新的 Ehcache。我所拥有的是: class Contact{ int id; int revision; } @Cachea
我已经看到很多同一个问题的化身,但我想我已经尝试了所有修复 - 我的用法非常简单。 我一直在使用 Ehcache,但也没有用。因此,为了排除 Ehcache 问题并帮助指出更基本的东西,我转向了 Si
我有一个关于 Spring Security 和 Spring Caching 的问题。假设我有一个方法,并且我已经用 @PreAuthorize("condition") 和 @Cacheable(
我一般用@Cacheable在我的 spring-boot 应用程序中使用缓存配置并为每个缓存设置特定的 TTL(生存时间)。 我最近继承了一个使用 @Cacheable 的 spring boot
我想将一项服务设为可缓存。我一直在研究grails-cache plugin它看起来很有希望,但它导致了一些我不理解的行为。 考虑以下服务: class FooService { def co
我在使用多参数和分页的 @Cacheable 时遇到问题。 @Cacheable(value = "books", key = "#p0") public List findBooks(Long lo
当调用带有 @Cacheable 注释且同步标志设置为 true 的方法时,出现以下异常: java.lang.AbstractMethodError: org.springframework.cac
我已将我的应用程序升级到 Spring 3.1,并且所有 jar 都已充分更新。但是,当我尝试对其中一个 Controller 中的方法使用 @Cacheable 时,该 Controller 的所有
如何进行以下工作? public abstract class MyAbstractOne { @Cacheable(value="myCache") public MyObj
尝试使用内联刷新来实现我自己的缓存加载器。此缓存加载使用 RefreshAheadCacheFactory 如描述 http://terracotta.org/documentation/4.1/bi
我是一名优秀的程序员,十分优秀!