봄 캐시으로 인스턴스 변수이고 매개 변수는 열쇠
-
21-12-2019 - |
문제
내가 사용하 ehcache 캐싱 방법은 결과입니다.핵심은 둘의 조합 회원 대상 및 방법의 매개 변수입니다.스는 다음과 같:
Class A {
private B b;
@Cacheable(value="someCache",key="some key based on B and C")
public Result getResult(C c){
......
}
저는 키가 필요합을 기반으로 합 B and C.나라 https://code.google.com/p/ehcache-spring-annotations/issues/detail?id=69 하지만 그들은 지정하지 않은 방법을 포함하는 방법에서 매개 변수를 키 생성.할 수 있는 사람이 있었는 데 도움이?
해결책 2
이 문제를 해결하기 위해 사용자 정의 키 생성기를 구현했습니다.그래도 사용자 정의 키 생성기를 사용하지 않고도 EHCache가 해결할 수 있다고 생각합니다.그러나 나는 어디에서나 답변을 얻을 수 없었다.아래 답변을 참조하십시오 :
@Component
public class Home {
private Parameter param;
@Cacheable(cacheName = "homeCache",
keyGenerator = @KeyGenerator(name = "com.myapp.cache.Home.ParamKeyGenerator"))
public Result getPerson(Person p) {
//Do something with p
return result;
}
public Parameter getParam() {
return param;
}
public void setParam(Parameter param) {
this.param = param;
}
public static class ParamKeyGenerator implements CacheKeyGenerator<Serializable> {
public Serializable generateKey(MethodInvocation methodInvocation) {
Object[] obj = methodInvocation.getArguments();
Home h = (Home) methodInvocation.getThis();
return this.generateKey(obj[0], h.getParam());
}
public Serializable generateKey(Object... data) {
String key = ((Person) data[0]).getName() + ((Parameter) data[1]).getName();
System.out.println("generating key: " + key);
return key;
}
}
}
. 다른 팁
액세스할 수 있습니다 A
체 root.target
에서 핵심입니다.예:
key="#root.target.b.id+'-'+#c.id"
제휴하지 않습니다 StackOverflow