简体   繁体   English

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

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

I have a methods for caching, and a method for cache eviction I want to use a cache key attribute to access cache, How do I force or lock someone calling this method to use same key. 我有一个缓存方法,一个缓存逐出的方法我想使用缓存键属性来访问缓存,如何强制或锁定调用此方法的人使用相同的密钥。

I tried something like this but, It didn't seem right. 我试过这样的事情但是,这似乎不对。 method parameter (cachekey) can be passed as any value desired by the caller. 方法参数(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 ");  }

If your method don't have any parameter just put the same method name as key and use it to Evict: 如果您的方法没有任何参数,只需将相同的方法名称作为键,并将其用于Evict:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM