Spring的@CachePut注释是否可以使用void返回类型?

问题描述:

我正在尝试使用Ehcache和Spring 3.1内置的缓存注释(@ Cacheable,@ CacheEvict和@CachePut)在我的应用程序中实现缓存.

I am attempting to implement caching in my application using Ehcache and the Spring 3.1 built in caching annotations (@Cacheable, @CacheEvict, and @CachePut).

我创建了如下缓存:

@Cacheable(value = "userCache", key = "#user.id")
public List<User> getAllUsers() {
...
}

我正尝试使用@CachePut批注使用以下新值来更新此缓存:

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?

是的,因为返回类型无效. @CachePut批注将方法的结果放入缓存中.在您的情况下,没有结果,因此不会将任何内容放入缓存.

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) { 
   ...
}