@cacheput is not updating the existing cache
Asked Answered
M

2

14

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.

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;

    } 
Miki answered 15/10, 2015 at 17:37 Comment(4)
You are using a single cache for different types. You are first caching a List of users and next expect the cacheput to put a single user into that cache. That is of course not going to work it will not update the list already in the cache.Gasconade
Thank you for your response. but this is what my requirement. I need to update the cache(list of user objects) with the new added user. Can you please share the workable solution to fulfill this requirement.Miki
You will have to manually update the cache or not use the spring cache abstraction but simple hibernates second level cache which takes care of this for you.Gasconade
Also see #24941476Dominik
P
8

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.

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.

Phenice answered 16/2, 2018 at 5:57 Comment(1)
Can you paste a sample code?Carpeting
U
3

An alternative approach is you could use @CacheEvict(allEntries = true) on the method used to Save or Update or Delete the records. It will flush the existing cache.

Example:

@CacheEvict(allEntries = true) 
public void saveOrUpdate(Person person) 
{
     personRepository.save(person);
}

A new cache will be formed with updated result the next time you call a @Cacheable method

Example:

@Cacheable // caches the result of getAllPersons() method
public List<Person> getAllPersons() 
{
     return personRepository.findAll();
}
Uniparous answered 15/4, 2019 at 12:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.