如何在没有方法参数的方法上使用 @cacheable 注释的键

How to use key for @cacheable annotation on a method which doesn't have a method parameter

我有一个缓存方法和一个缓存逐出方法我想使用缓存键属性来访问缓存,如何强制或锁定调用此方法的人使用相同的键。

我试过类似的方法,但似乎不对。方法参数 (cachekey) 可以作为调用者所需的任何值传递。

@Cacheable(value = "cacheNamex" , key ="#cachekey") 
    public   List  someCachableMethod(String cacheKey ) {
        List someList = someJdbctemplet.query(SOME_QUERY, someRowMapperObj);
        System.out.println(" data Returned from method");
        return someList;
    }
@CacheEvict((value = "cacheNamex" , key ="#cachekey") 
    public void someCacheEvictMethod(String cacheKey ){
           System.out.println("cache eviction called");
           System.out.println(" Expecting cache, cachex is cleared ");  }

如果您的方法没有任何参数,只需输入与键相同的方法名称并将其用于驱逐:

@Cacheable(value = "cacheNamex" , key ="'someCachableMethod'")
 public   List  someCachableMethod() {
 }
 @CacheEvict((value = "cacheNamex" , key ="'someCachableMethod'") 
 public void someCacheEvictMethod(){
}

如果您的方法没有参数,您可以使用没有键值的注释,如果您想了解有关此主题的更多信息,请参阅 spring 文档中的 http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.html#cache-annotations-cacheable-default-key..