@cacheput未更新现有缓存 [英] @cacheput is not updating the existing cache

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

问题描述

我正在使用Spring 4和Hazelcast 3.2.我正在尝试使用以下代码向现有缓存添加新记录.不知何故,缓存没有得到更新,同时我也没有看到任何错误.下面是供参考的代码段.

I am working with Spring 4 and Hazelcast 3.2. I am trying to add a new record to existing cache with below code. somehow cache is not getting updated and at the same time I don't see any errors also. below is the code snippet for reference.

注意:-Cacheable工作正常,只有cacheput不工作.请对此亮点

Note:- Cacheable is working fine, only cacheput is not working. Please throw light on this

@SuppressWarnings("unchecked")`enter code here`
    @Transactional(readOnly = true, propagation = Propagation.REQUIRED)
    @Cacheable(value="user-role-data")
    public List<User> getUsersList() {
    // Business Logic
    List<User> users= criteriaQuery.list();

    }

@SuppressWarnings("unchecked")
    @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
    @CachePut(value = "user-role-data")
    public User addUser(User user) {
                    return user;

    } 

推荐答案

我遇到了同样的问题,并设法解决了这个问题.这个问题似乎与交易管理有关.由于未提交事务,因此无法以与创建或更新新记录时相同的方法对缓存进行基本更新.这是我解决的方法.

I had the same issue and managed to solved it. The issue seemed to be tied to the transaction management. Bascially updating the cache in the same method where you are creating or updating the new record does not work because the transaction was not committed. Here's how I solved it.

服务层调用回购以插入用户然后回到服务层在insert/update db调用之后在服务层中,我称为刷新缓存方法返回了用户数据,并且此方法具有cacheput批注之后,它起作用了.

Service layer calls repo to insert user Then go back to service layer After the insert /update db call In the service layer I called a refresh cache method That returned the user data and this method has the cacheput annotation After that it worked.

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

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