org.objectweb.jac.wrappers
Class CacheWrapper

java.lang.Object
  extended byorg.objectweb.jac.core.Wrapper
      extended byorg.objectweb.jac.wrappers.CacheWrapper
All Implemented Interfaces:
Advice, ConstructorInterceptor, Interceptor, MethodInterceptor, Serializable

public class CacheWrapper
extends Wrapper

CacheWrapper implements a wrapper that caches the wrappee methods results so that it can avoid the wrappee computations. It can improve performances of complex computations.

Please make sure to use the rigth wrapping method.

For example, if you have a Matrix class:

An instance of Matrix must be wrapped with stateCache since the result depends on the matrix state:

However, when the state of the matrix is modified, the cache should be cleared since its values are not valid anymore. This can be done by using the wrapping method clearCache.

See Also:
Serialized Form

Field Summary
protected  Vector cacheKeys
          Store the keys for the cache.
protected  Vector cacheValues
          Store the cached values.
 
Fields inherited from class org.objectweb.jac.core.Wrapper
ac, cr
 
Constructor Summary
CacheWrapper(AspectComponent ac)
           
 
Method Summary
 Object clearCache(Interaction interaction)
          Clear the cache.
 Object construct(ConstructorInvocation invocation)
           
protected  Object getCacheValue(int index)
          Get a cached value.
protected  Object getCacheValue(Object[] key)
          Get the memorized value of the method when called with the given (method, args) key.
 Object invoke(MethodInvocation invocation)
           
protected  int isCacheHit(Object[] key)
          Returns the index of a key in the cache, -1 if not found.
protected  void setCacheValue(Object[] key, Object value)
          Add a (method, args) key and its value into the cache.
 Object stateCache(Interaction interaction)
          This wrapping method seeks in the cache wether the triple (wrappe, method, args) has already been called on the wrappee object when wrappee was in the same state.
 Object statelessCache(Interaction interaction)
          This wrapping method seeks in the cache wether the couple (method, args) has already been called on the wrappee object.
 
Methods inherited from class org.objectweb.jac.core.Wrapper
attr, attrdef, defines, getAspectComponent, getAspectComponentName, getExceptionHandlers, proceed, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

cacheKeys

protected Vector cacheKeys
Store the keys for the cache.


cacheValues

protected Vector cacheValues
Store the cached values.

Constructor Detail

CacheWrapper

public CacheWrapper(AspectComponent ac)
Method Detail

statelessCache

public Object statelessCache(Interaction interaction)
This wrapping method seeks in the cache wether the couple (method, args) has already been called on the wrappee object. If yes, it returns the value from the cache. Otherwise it calls the wrappee object and memorizes the result in the cache.

This wrapper can be used on objects that have complex, but state independant computation to perform. For instance, cache could wrap a computational object that would be able to invert a matrix so that the inversion of a given matrix would be done only once.

NOTE: if the wrappee function depends on the the object state the values returned by the statelessCache will be wrong. In this case, use the stateCache wrapping method.

See Also:
stateCache(Interaction)

stateCache

public Object stateCache(Interaction interaction)
This wrapping method seeks in the cache wether the triple (wrappe, method, args) has already been called on the wrappee object when wrappee was in the same state. If yes, it returns the value from the cache. Otherwise it calls the wrappee object and memorizes the result in the cache.

This wrapper can be used on objects that have complex state dependant computation to perform. For instance, cache could wrap the invert method of a matrix object. If you invert this matrix three times, then the third inversion result will be found in the cache.

NOTE: if the wrappee method computation does not depend on the wrappee state, then you should use the statelessCache method for better performance and hits rate.

See Also:
statelessCache(Interaction)

clearCache

public Object clearCache(Interaction interaction)
Clear the cache.

This method should wrap all the methods that change the state of the wrappee in a case of a state cache.

Returns:
the value returned by the wrapped method

setCacheValue

protected void setCacheValue(Object[] key,
                             Object value)
Add a (method, args) key and its value into the cache.

Parameters:
key - the key to find the value in the cache
value - the cached value
See Also:
getCacheValue(Object[])

getCacheValue

protected Object getCacheValue(Object[] key)
Get the memorized value of the method when called with the given (method, args) key.

Parameters:
key - the key
Returns:
the cached value that matches the key
See Also:
setCacheValue(Object[],Object)

getCacheValue

protected Object getCacheValue(int index)
Get a cached value.

Parameters:
index - the index in the cache
Returns:
the cached value

isCacheHit

protected int isCacheHit(Object[] key)
Returns the index of a key in the cache, -1 if not found.

Parameters:
key - a complex key
Returns:
the location of the key

invoke

public Object invoke(MethodInvocation invocation)
              throws Throwable
Specified by:
invoke in interface MethodInterceptor
Overrides:
invoke in class Wrapper
Throws:
Throwable

construct

public Object construct(ConstructorInvocation invocation)
                 throws Throwable
Specified by:
construct in interface ConstructorInterceptor
Overrides:
construct in class Wrapper
Throws:
Throwable