10

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;

    } 
Somi Reddy
  • 101
  • 1
  • 3
  • 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. – M. Deinum Oct 15 '15 at 17:45
  • 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. – Somi Reddy Oct 16 '15 at 07:50
  • 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. – M. Deinum Oct 16 '15 at 08:19
  • Also see http://stackoverflow.com/questions/24940976/how-update-remove-an-item-already-cached-within-a-collection-of-items – Aerus Mar 03 '17 at 10:16

2 Answers2

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.

A_H
  • 121
  • 1
  • 6
2

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();
}
Anantha Raju C
  • 1,780
  • 12
  • 25
  • 35