如何在同一个ehCache中缓存2个不同的bean方法?

时间:2021-10-19 03:50:05

Say I have 2 different bean methods which I want to cache by EhCache:

假设我有两种不同的bean方法,我想通过EhCache缓存:

@Component
public class StatService {

@Cacheable(value = "statCalc")
public int getMeth1(int param) {
    // LOGIC1
}

@Cacheable(value = "statCalc")
public int getMeth2(int param) {
    // LOGIC2
}
}

I want to reside them in same cache - ehcache.xml:

我想将它们驻留在同一个缓存中 - ehcache.xml:

<cache name="statCalc"
       ...
/>

The problem is that cache key is generated by input parameters and not by method signature, thus getMeth2(1) can return value cached by getMeth1(1).

问题是缓存键是由输入参数而不是方法签名生成的,因此getMeth2(1)可以返回由getMeth1(1)缓存的值。

What is the easiest way to build key using method name?

使用方法名称构建密钥的最简单方法是什么?

P.S. Please, don't mention the fact that using same cache for different methods could be wrong, just help to solve this problem.

附:请不要提及为不同方法使用相同缓存可能是错误的,只是帮助解决这个问题。

2 个解决方案

#1


3  

Use custom KeyGenerator, e.g.

使用自定义KeyGenerator,例如

public class CustomKeyGenerator implements KeyGenerator{
    @Override
    public Object generate(Object target, Method method, Object... params) {
        StringBuilder key = new StringBuilder();
        //include method name in key
        key.append(method.getName());
        if (params.length > 0) {
            key.append(';');
            for (Object argument : params) {
                key.append(argument);
                key.append(';');
            }
        }
        return key.toString();
    }
}

Register key generator as a bean and add @CacheKeyStrategy("keyGeneratorBeanName") annotation to the cacheable methods

将密钥生成器注册为bean并将@CacheKeyStrategy(“keyGeneratorBeanName”)注释添加到可缓存方法

#2


6  

Spring Cache abstraction allows you to use SpeL to specify the cache key. For example you can use a method name , parameter values etc.

Spring Cache abstraction允许您使用SpeL指定缓存键。例如,您可以使用方法名称,参数值等。

For example

例如

@Component
public class StatService {

   @Cacheable(value="statCalc",key="#root.method.name.concat(':').concat(#param)")
   public int getMeth1(int param) {
      // LOGIC1
   }

   @Cacheable(value="statCalc",key="#root.method.name.concat(':').concat(#param)")
   public int getMeth2(int param) {
   // LOGIC2
   }
}

For the method call getMeth1(5) the key will be getMethod1:5 For method call getMethod1(0) key will be getMethod1:0

对于方法调用getMeth1(5),键将为getMethod1:5对于方法调用,getMethod1(0)键将为getMethod1:0

For method call getMeth2(3) the key will be getMethod2:3. This allows you to cache results for different parameters

对于方法调用getMeth2(3),键将是getMethod2:3。这允许您缓存不同参数的结果

#1


3  

Use custom KeyGenerator, e.g.

使用自定义KeyGenerator,例如

public class CustomKeyGenerator implements KeyGenerator{
    @Override
    public Object generate(Object target, Method method, Object... params) {
        StringBuilder key = new StringBuilder();
        //include method name in key
        key.append(method.getName());
        if (params.length > 0) {
            key.append(';');
            for (Object argument : params) {
                key.append(argument);
                key.append(';');
            }
        }
        return key.toString();
    }
}

Register key generator as a bean and add @CacheKeyStrategy("keyGeneratorBeanName") annotation to the cacheable methods

将密钥生成器注册为bean并将@CacheKeyStrategy(“keyGeneratorBeanName”)注释添加到可缓存方法

#2


6  

Spring Cache abstraction allows you to use SpeL to specify the cache key. For example you can use a method name , parameter values etc.

Spring Cache abstraction允许您使用SpeL指定缓存键。例如,您可以使用方法名称,参数值等。

For example

例如

@Component
public class StatService {

   @Cacheable(value="statCalc",key="#root.method.name.concat(':').concat(#param)")
   public int getMeth1(int param) {
      // LOGIC1
   }

   @Cacheable(value="statCalc",key="#root.method.name.concat(':').concat(#param)")
   public int getMeth2(int param) {
   // LOGIC2
   }
}

For the method call getMeth1(5) the key will be getMethod1:5 For method call getMethod1(0) key will be getMethod1:0

对于方法调用getMeth1(5),键将为getMethod1:5对于方法调用,getMethod1(0)键将为getMethod1:0

For method call getMeth2(3) the key will be getMethod2:3. This allows you to cache results for different parameters

对于方法调用getMeth2(3),键将是getMethod2:3。这允许您缓存不同参数的结果