use of org.killbill.billing.util.cache.CachableKey in project killbill by killbill.
the class EntitySqlDaoWrapperInvocationHandler method invokeWithCaching.
private Object invokeWithCaching(final Cachable cachableAnnotation, final Method method, final Object[] args) throws Throwable {
final ObjectType objectType = getObjectType();
final CacheType cacheType = cachableAnnotation.value();
final CacheController<Object, Object> cache = cacheControllerDispatcher.getCacheController(cacheType);
Object result = null;
if (cache != null) {
// Find all arguments marked with @CachableKey
final Map<Integer, Object> keyPieces = new LinkedHashMap<Integer, Object>();
final Annotation[][] annotations = method.getParameterAnnotations();
for (int i = 0; i < annotations.length; i++) {
for (int j = 0; j < annotations[i].length; j++) {
final Annotation annotation = annotations[i][j];
if (CachableKey.class.equals(annotation.annotationType())) {
// CachableKey position starts at 1
keyPieces.put(((CachableKey) annotation).value() - 1, args[i]);
break;
}
}
}
// Build the Cache key
final String cacheKey = buildCacheKey(keyPieces);
final InternalTenantContext internalTenantContext = (InternalTenantContext) Iterables.find(ImmutableList.copyOf(args), new Predicate<Object>() {
@Override
public boolean apply(final Object input) {
return input instanceof InternalTenantContext;
}
}, null);
final CacheLoaderArgument cacheLoaderArgument = new CacheLoaderArgument(objectType, args, internalTenantContext, handle);
return cache.get(cacheKey, cacheLoaderArgument);
}
if (result == null) {
result = prof.executeWithProfiling(ProfilingFeatureType.DAO_DETAILS, sqlDaoClass.getSimpleName() + "(raw) :" + method.getName(), new WithProfilingCallback() {
@Override
public Object execute() throws Throwable {
return method.invoke(sqlDao, args);
}
});
}
return result;
}
Aggregations