如何将@CachePut与@Cacheable注释一起使用?

时间:2015-07-03 16:38:01

标签: spring ehcache

我使用@Cacheable注释从下面的方法获取电影列表。由于它没有任何参数,我将密钥设置为#root.method.name。

@Cacheable(value="movieFindCache", key = "#root.method.name")
public List<Movie> getMovies(){
    ...
    return movies;
}

现在我想添加一部新电影,同样应该添加到上面的缓存中。

@CachePut(value="movieFindCache", key="getMovies")
public void addNewMovie(){
    // create and add new movie to movies list
}

我尝试了这个,但它给了我例外。

    org.springframework.expression.spel.SpelEvaluationException: 
Property or field 'getMovies' cannot be found on object of type 'org.springframework.cache.interceptor.CacheExpressionRootObject' - maybe not public?

我们可以在这里使用@CachePut注释,还是有其他方法可以做到这一点?

1 个答案:

答案 0 :(得分:1)

key是一种Spring Expression语言http://docs.spring.io/spring/docs/current/spring-framework-reference/html/expressions.html

尝试''

@CachePut(value="movieFindCache", key="'getMovies'")