// sanity check
if (cacheable == null)
throw new IllegalArgumentException("Null cachable, invalid usage?");
String cacheName = cacheable.name();
Cache cache = cacheConfig.configureCache(cacheName);
CacheMode mode = cacheable.mode(); // should not be null, as we got intercepted
Class<? extends KeyStrategy> ksClass = cacheable.key();
KeyStrategy ks = keys.get(ksClass);
if (ks == null) {
try {
ks = ksClass.newInstance();
} catch (Exception e) {
log.fine("Error creating KeyStrategy: " + e);
Constructor<? extends KeyStrategy> ctor = ksClass.getConstructor(CacheConfig.class);
ks = ctor.newInstance(cacheConfig);
}
keys.put(ksClass, ks);
}
Object target = ctx.getTarget();
Object[] args = ctx.getParameters();
Serializable key = ks.createKey(target, m, args);
Object value = null;
try {
if (mode == CacheMode.READ_ONLY || mode == CacheMode.ALL)
value = cache.get(key);
if (value != null) {
Object unwraped = ks.unwrap(value, target, m, args);
if (unwraped != null) // could be invalidated
return unwraped;
}
value = ctx.proceed();
if (value != null && (mode == CacheMode.WRITE_ONLY || mode == CacheMode.ALL))
cache.put(key, ks.wrap(value, target, m, args));
else if (mode == CacheMode.REMOVE)
cache.remove(key);
else if (mode == CacheMode.EVICT)
cache.evict();
else if (mode == CacheMode.CLEAR)
cache.clear();
return value;
} catch (Throwable e) {
return exceptionHandler.handleException(cache, ctx, key, value, e);
}