我最近开始从一个方法缓存结果.我正在使用@Cacheable和@CachePut来实现所需的功能.
但不知何故,保存操作不会更新findAll方法的缓存.以下是相同的代码段:
@RestController
@RequestMapping(path = "/test/v1")
@CacheConfig(cacheNames = "persons")
public class CacheDemoController {
@Autowired
private PersonRepository personRepository;
@Cacheable
@RequestMapping(method = RequestMethod.GET, path="/persons/{id}")
public Person getPerson(@PathVariable(name = "id") long id) {
return this.personRepository.findById(id);
}
@Cacheable
@RequestMapping(method = RequestMethod.GET, path="/persons")
public List<Person> findAll() {
return this.personRepository.findAll();
}
@CachePut
@RequestMapping(method = RequestMethod.POST, path="/save")
public Person savePerson(@RequestBody Person person) {
return this.personRepository.save(person);
}
}
Run Code Online (Sandbox Code Playgroud)
对于第一次调用findAll方法,它将结果存储在"person"缓存中,对于所有后续调用,即使在其间执行了save()操作,它也会返回相同的结果.
我对缓存很新,所以对此的任何建议都会有很大的帮助.
谢谢!