@Cacheable无法更新 [英] @Cacheable is not able to update

查看:270
本文介绍了@Cacheable无法更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在spring框架中,我们有@Cacheable来正确缓存数据.现在我的要求是我想使用Get方法检索所有数据表单数据库.

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.

控制器

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

服务

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

}

输出:单击此处的图像以检查输出

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"}]

现在,我将通过ticketid进行获取和放置操作.

Now i will do get and put operation by ticketid.

获取:控制器:

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

服务:

    @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}

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

现在,当我使用工单ID更新电子邮件时:

Now when i do update email by using ticketid:

放置:控制器

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

服务:

@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"

}

现在,当使用ID更改获取数据时,将进行更新.

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"}

现在,我的问题是,如果我尝试通过使用上述第一个URL获取所有数据,则我的更改未反映出来.

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"}]

建议我如何解决此问题

推荐答案

您不能使用Spring批量更新缓存.

请检查以下问题-状态为拒绝:

感谢创建问题,但我不希望将这种额外的复杂性添加到缓存抽象中.这并不是要为您管理状态(如果允许的话,下一个逻辑步骤是我们必须使返回的列表与每个项目保持同步).如果不这样做,我们就会前后矛盾,我们仅提供一种使用批注与缓存进行对话的方法.那不是很有帮助.

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.

这篇关于@Cacheable无法更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆