@Cacheable is not able to update

只愿长相守 提交于 2021-01-28 00:51:50

问题


In spring framework we have @Cacheable to cache data right. Now my requirement is i want to retrieve all data form database by using Get method.

Controller

@RequestMapping(value = "/getUploadData", method = RequestMethod.GET)
public ResponseEntity<List<Ticket>> getUploadFileData() throws IOException {
    return new ResponseEntity<>(ticketBookingService.getFileUploadData(), HttpStatus.OK);
}

Service

@Cacheable(value="ticketsCache")
public List<Ticket> getFileUploadData() {
    List<Ticket> listOfData = (List<Ticket>) ticketBookingDao.findAll();
    return listOfData;
}

}

output: click image here to check output

http://localhost:8080/api/tickets/getUploadData

[{"ticketId":1,"passengerName":"Sean","bookingDate":1502649000000,"sourceStation":"Pune","destStation":"Mumbai","email":"sean.s2017@yahoo.com"},{"ticketId":2,"passengerName":"Raj","bookingDate":1502476200000,"sourceStation":"Chennai","destStation":"Mumbai","email":"raj.s2007@siffy.com"},{"ticketId":3,"passengerName":"Martin","bookingDate":1502735400000,"sourceStation":"Delhi","destStation":"Mumbai","email":"martin.s2001@xyz.com"},{"ticketId":4,"passengerName":"John","bookingDate":1503253800000,"sourceStation":"Chennai","destStation":"Mumbai","email":"john.s2011@yahoo.com"}]

Now i will do get and put operation by ticketid.

Get: Controller:

    @GetMapping(value="/ticket/{ticketId}")
public Ticket getTicketById(@PathVariable("ticketId")Integer ticketId){
    return ticketBookingService.getTicketById(ticketId);
}

Service:

    @Cacheable(value="ticketsCache",key="#ticketId",unless="#result==null")
public Ticket getTicketById(Integer ticketId) {
    return ticketBookingDao.findOne(ticketId);
}

http://localhost:8080/api/tickets/ticket/1

{"ticketId":1,"passengerName":"Sean","bookingDate":1502649000000,"sourceStation":"Pune","destStation":"Mumbai","email":"sean.s2017@yahoo.com"}

Now when i do update email by using ticketid:

Put: controller

    @PutMapping(value="/ticket/{ticketId}/{newEmail:.+}")
public Ticket updateTicket(@PathVariable("ticketId")Integer ticketId,@PathVariable("newEmail")String newEmail){
    return ticketBookingService.updateTicket(ticketId,newEmail);
}

Service:

@CachePut(value="ticketsCache",key="#ticketId")
public Ticket updateTicket(Integer ticketId, String newEmail) {
    Ticket upadedTicket = null;
    Ticket ticketFromDb = ticketBookingDao.findOne(ticketId);
    if(ticketFromDb != null){
        ticketFromDb.setEmail(newEmail);
        upadedTicket = ticketBookingDao.save(ticketFromDb); 
    }
    return upadedTicket;
}

http://localhost:8080/api/tickets/ticket/1/abcd@yahoo.com

{
"ticketId": 1,
"passengerName": "Sean",
"bookingDate": 1502649000000,
"sourceStation": "Pune",
"destStation": "Mumbai",
"email": "abcd@yahoo.com"

}

Now when get data by using ID changes are updating.

http://localhost:8080/api/tickets/ticket/1

{"ticketId":1,"passengerName":"Sean","bookingDate":1502649000000,"sourceStation":"Pune","destStation":"Mumbai","email":"abcd@yahoo.com"}

Now my Question is if i try to get all data by using above first URL my changes are not reflecting.

http://localhost:8080/api/tickets/getUploadData

[{"ticketId":1,"passengerName":"Sean","bookingDate":1502649000000,"sourceStation":"Pune","destStation":"Mumbai","email":"sean.s2017@yahoo.com"},{"ticketId":2,"passengerName":"Raj","bookingDate":1502476200000,"sourceStation":"Chennai","destStation":"Mumbai","email":"raj.s2007@siffy.com"},{"ticketId":3,"passengerName":"Martin","bookingDate":1502735400000,"sourceStation":"Delhi","destStation":"Mumbai","email":"martin.s2001@xyz.com"},{"ticketId":4,"passengerName":"John","bookingDate":1503253800000,"sourceStation":"Chennai","destStation":"Mumbai","email":"john.s2011@yahoo.com"}]

Suggest me how to reslove this issue


回答1:


You cannot bulk update the cache with Spring.

Please check the following issue - closed with status declined:

Thanks for creating the issue but I am not keen to add this extra complexity to the cache abstraction. It is not meant to manage state for you (the next logical step if we allow this is that we have to keep the returned list in sync with each item). And if we don't we are inconsistent and we merely provide a way to talk to the cache using annotations. That's not very helpful.

Back to your example, this is typically what a second level cache is meant to do for you. This is not in the scope of the cache abstraction.



来源:https://stackoverflow.com/questions/60912349/cacheable-is-not-able-to-update

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!