首页 > 解决方案 > 将@Scheduled 应用于@Cacheable 休息方法

问题描述

我正在尝试将计划添加到 rest get 方法,当我在没有 @Cacheable 注释的情况下使用它时,计划程序可以正常工作。像这样——

@Scheduled(fixedDelay = 1000*5)
@GetMapping("test")
public void test(){
    System.out.println("scheduled task through spring");
}

问题是,当我添加@Cacheable 注解时,请求被加载一次,然后调度程序不会重复。

@Scheduled(fixedDelay = 1000*5)
@Cacheable("testData")
@GetMapping("test")
public void test(){
   System.out.println("scheduled task through spring");
}

我用谷歌搜索过,但我只能用@Scheduled 找到关于@CacheEvict 的信息

标签: javaspringannotations

解决方案


我不相信@Cacheable采用一种void方法是有意义的。即使该方法正在返回一个值...

我相信@Scheduled正在触发,但@Cacheable会导致返回缓存值,而不是再次执行该方法。我认为这是有道理的。

打开 DEBUG 日志记录以进行验证。

祝你好运!


推荐阅读