2014-02-25 5 views
0

메소드 결과를 캐싱하기 위해 ehcache를 사용하고 있습니다. 키는 구성원 개체와 메서드 매개 변수의 조합이어야합니다. 내가 https://code.google.com/p/ehcache-spring-annotations/issues/detail?id=69을 언급하지만 그들은 키 생성에서 메소드 매개 변수를 포함하는 방법을 지정하지 않은 B와 C 를 기반으로하는 키가 필요인스턴스 변수와 매개 변수를 키로 사용하는 스프링 캐시

Class A { 

private B b; 

@Cacheable(value="someCache",key="some key based on B and C") 
public Result getResult(C c){ 
...... 
} 

: 뭔가처럼 내 클래스 보인다. 누군가가 이걸 도와 줄 수 있니?

답변

0

이 문제를 해결하기 위해 맞춤 키 생성기를 구현했습니다. 아직도 나는 이것이 맞춤 키 생성기를 사용하지 않고 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; 
     } 
    } 
} 
1

키에 root.target과 함께 A 개체에 액세스 할 수 있습니다. 예 :

key="#root.target.b.id+'-'+#c.id" 
+0

사과. 스프링 캐쉬 주석을 달았습니다. ehcache의 @Cacheable 주석에는 '키'가 없습니다. @ 캐시 가능 (cacheName = "homeCache", keyGenerator = .......) – falcon