Açıklaması şöyle
@Caching: Java doesn’t allow you to use the same annotation type twice in a method or class. So, if you want to say @CacheEvict in two different caches in the same method @Cacheable can be used to aggregate other cache annotations.
Örnek
Şöyle yaparız
@Caching(evict = { @CacheEvict(“address”), @CacheEvict(value=“employee”, key=”#employee.id”) }) public Employee getEmployee(Employee employee) { // some code }
Örnek
Şöyle yaparız. Burada aynı anda iki cache'ten birden silme işlemi olduğu için @Caching kullanılıyorGeri kalan kodları şöyle yaparız@RestController("/")public class TodoListController {private ToDoList sample = new ToDoList(1, new ArrayList<ToDo>(Arrays.asList(
new ToDo(1, "Comment", false),new ToDo(2, "Clap", false),new ToDo(3, "Follow Author", false))), false);@DeleteMapping("/todo/{id}")@Caching(evict = {@CacheEvict(value = "todo-single", key = "#id"),@CacheEvict(value="todo-list", key="'getList'")})public void deleteToDo(@PathVariable("id") long id) throws Exception {Optional<ToDo> item = sample.getTasks().stream()
.filter(x -> x.getId() == id).findFirst();if (item.isPresent()) {sample.getTasks().remove(item.get());} else {throw new Exception("To Do item not found");}}}
@Cacheable(value = "todo-list" , key="'getList'")@GetMappingpublic ToDoList getList() {return sample;}@Cacheable(value = "todo-single", key = "#id")@GetMapping("/todo/{id}")public Optional<ToDo> getToDo(@PathVariable("id") long id) {return sample.getTasks().stream().filter(x -> x.getId() == id).findFirst();}@PutMapping("/todo")@CachePut(value = "todo-single", key = "#toDo.id")@CacheEvict(value="todo-list" , key="'getList'")public ToDoList addToDo(@RequestBody ToDo toDo) {sample.getTasks().add(toDo);return sample;}@PostMapping("/todo/{id}")@CachePut(value = "todo-single", key = "#id")@CacheEvict(value="todo-list", key="'getList'")public ToDo updateToDo(@PathVariable("id") long id, @RequestBody ToDo toDo)
throws Exception {Optional<ToDo> item = sample.getTasks().stream()
.filter(x -> x.getId() == id).findFirst();if (item.isPresent()) {item.get().setCompleted(toDo.isCompleted());return item.get();} else {throw new Exception("To Do item not found");}}
Ve şöyle yaparız
@DeleteMapping("/todo/{id}")@Caching(evict = {@CacheEvict(value = "todo-single", key = "#id"),@CacheEvict(value="todo-list", key="'getList'")})public void deleteToDo(@PathVariable("id") long id) throws Exception {Optional<ToDo> item = sample.getTasks().stream()
.filter(x -> x.getId() == id).findFirst();if (item.isPresent()) {sample.getTasks().remove(item.get());} else {throw new Exception("To Do item not found");}}
Hiç yorum yok:
Yorum Gönder