lakhan_Ideavate
lakhan_Ideavate

Reputation: 374

Cachable : How can we use static field of other class as Key in Spring Caching

How can we use static field of other class as Key in @Cachable

class Keys {
    public static String CACHE_KEY = 'MY_KEY';
}

class MyClass {

@Cachable(value="TestValue",key="#Keys.CACHE_KEY")
public void method(){

}

}

Upvotes: 0

Views: 453

Answers (1)

Essex Boy
Essex Boy

Reputation: 7950

Use a keyGenerator instead, although you wouldn't want a constant key obviously.

   @Cachable(value="TestValue",keyGenerator ="keyGenerator")
    public void method(){

    }

    @Bean
    public KeyGenerator keyGenerator() {
      return new KeyGenerator() {
        @Override
        public Object generate(Object o, Method method, Object... params) {
          return CACHE_KEY ;
        }
      };
    }

Upvotes: 1

Related Questions