Using Spring beans as a key with @Cacheable annotation

后端 未结 2 944
终归单人心
终归单人心 2021-01-03 16:45

How to make following to work: - a spring bean that has a method that should be cached with @Cacheable annotation - another spring bean that creates keys for the cache (Key

2条回答
  •  猫巷女王i
    2021-01-03 17:08

    In the case you have a static class function, it will work like this

    @Cacheable(value = "cacheName", key = "T(pkg.beans.KeyCreatorBean).createKey(#p0)")
    @Override
    public List getExamples(ExampleId exampleId) {
      ...
    }
    

    with

    package pkg.beans;
    
    import org.springframework.stereotype.Repository;
    
    public class KeyCreatorBean  {
    
        public static Object createKey(Object o) {
            return Integer.valueOf((o != null) ? o.hashCode() : 53);
        }
    
    }
    

提交回复
热议问题