Search in sources :

Example 1 with CacheType

use of org.killbill.billing.util.cache.Cachable.CacheType 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;
}
Also used : Annotation(java.lang.annotation.Annotation) CacheLoaderArgument(org.killbill.billing.util.cache.CacheLoaderArgument) CacheType(org.killbill.billing.util.cache.Cachable.CacheType) LinkedHashMap(java.util.LinkedHashMap) Predicate(com.google.common.base.Predicate) WithProfilingCallback(org.killbill.commons.profiling.Profiling.WithProfilingCallback) ObjectType(org.killbill.billing.ObjectType) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) CachableKey(org.killbill.billing.util.cache.CachableKey)

Example 2 with CacheType

use of org.killbill.billing.util.cache.Cachable.CacheType in project killbill by killbill.

the class CacheControllerDispatcherProvider method get.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public CacheControllerDispatcher get() {
    final Map<CacheType, CacheController<Object, Object>> cacheControllers = new LinkedHashMap<CacheType, CacheController<Object, Object>>();
    for (final BaseCacheLoader cacheLoader : cacheLoaders) {
        final CacheType cacheType = cacheLoader.getCacheType();
        final CacheController<Object, Object> cacheController;
        if (cacheConfig.getDisabledCaches() != null && cacheConfig.getDisabledCaches().contains(cacheType.getCacheName())) {
            logger.info("Disabling cache for cacheName='{}'", cacheLoader.getCacheType().getCacheName());
            cacheController = new NoOpCacheController(cacheLoader);
        } else {
            final Cache cache = cacheManager.getCache(cacheType.getCacheName(), cacheType.getKeyType(), cacheType.getValueType());
            if (cache == null) {
                logger.warn("Cache for cacheName='{}' not configured", cacheLoader.getCacheType().getCacheName());
                continue;
            }
            Preconditions.checkState(!cache.isClosed(), "Cache '%s' should not be closed", cacheType.getCacheName());
            cacheController = new KillBillCacheController<Object, Object>(cache, cacheLoader);
        }
        cacheControllers.put(cacheType, cacheController);
    }
    return new CacheControllerDispatcher(cacheControllers);
}
Also used : CacheType(org.killbill.billing.util.cache.Cachable.CacheType) LinkedHashMap(java.util.LinkedHashMap) Cache(javax.cache.Cache)

Aggregations

LinkedHashMap (java.util.LinkedHashMap)2 CacheType (org.killbill.billing.util.cache.Cachable.CacheType)2 Predicate (com.google.common.base.Predicate)1 Annotation (java.lang.annotation.Annotation)1 Cache (javax.cache.Cache)1 ObjectType (org.killbill.billing.ObjectType)1 InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)1 CachableKey (org.killbill.billing.util.cache.CachableKey)1 CacheLoaderArgument (org.killbill.billing.util.cache.CacheLoaderArgument)1 WithProfilingCallback (org.killbill.commons.profiling.Profiling.WithProfilingCallback)1