将@Scheduled应用于@Cacheable rest方法

时间:2019-03-15 18:48:30

标签: java spring annotations

我正在尝试将调度添加到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");
}

我在Google周围搜索,但是我只能通过@Scheduled查找有关@CacheEvict的信息

1 个答案:

答案 0 :(得分:0)

我认为将@Cacheable放在void方法上是没有道理的。即使该方法正在返回值...

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

打开调试日志记录进行验证。

祝你好运!