I have an application that uses ehcache for cache (but I think this problem is framework-agnostic), with a method interceptor so basically if I mark my method for caching something like this happnes:
public Object invoke(MethodInvocation mi) throws Throwable {
Object result = cache.get(key);
//key comes from MethodInvocation processing
if (result == null) {
result = mi.proceed();
cache.put(key, result);
}
return result;
}
So far so good. The thing I'm caching a method that returns an Array
, and gets called like this:
List<Object> result = methodWithCaching();
result.add(new Object()); //!
As you can imagine, the line marked with !
also updates the cache instance, and this is not what I want.
Can someone think of a way to stop this behavior without modifying the client, only the interceptor?