Will Spring's @CachePut annotation work with a void return type?

annotations, caching, ehcache, spring

Solution

Yes, it's because void return type. The @CachePut annotation places the result of method into the cache. In your case there is no result so nothing is put to the cache.

Change method signature to return User:

public User addUser(User user) { 
   ...
}

Problem

I am attempting to implement caching in my application using Ehcache and the Spring 3.1 built in caching annotations (@Cacheable, @CacheEvict, and @CachePut). I have created a cache as follows: ``` @Cacheable(value = "userCache", key = "#user.id") public List<User> getAllUsers() { ... } ``` I am attempting to update this cache with a new value using the @CachePut annotation as below: ``` @CachePut(value = "userCache", key = "#user.id") public void addUser(User user) { ... } ``` However, the new "User" is not being added to the cache. Is this because of the void return type?

Original source