Search in sources :

Example 1 with CacheValueRetrievalException

use of cn.taketoday.cache.CacheValueRetrievalException in project today-framework by TAKETODAY.

the class CacheableInterceptor method invoke.

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
    Method method = invocation.getMethod();
    MethodKey methodKey = new MethodKey(method, Cacheable.class);
    CacheConfiguration cacheable = expressionOperations.getConfig(methodKey);
    CacheEvaluationContext context = expressionOperations.prepareContext(methodKey, invocation);
    if (expressionOperations.passCondition(cacheable.condition(), context)) {
        // pass the condition
        Cache cache = obtainCache(method, cacheable);
        Object key = expressionOperations.createKey(cacheable.key(), context, invocation);
        if (cacheable.sync()) {
            // for sync
            try {
                return cache.get(key, invocation::proceed);
            } catch (CacheValueRetrievalException e) {
                throw e.getCause();
            }
        } else {
            Object value = get(cache, key);
            if (value == null) {
                value = invocation.proceed();
                if (expressionOperations.allowPutCache(cacheable.unless(), value, context)) {
                    put(cache, key, value);
                }
            }
            return value;
        }
    }
    return invocation.proceed();
}
Also used : CacheValueRetrievalException(cn.taketoday.cache.CacheValueRetrievalException) Method(java.lang.reflect.Method) CacheConfiguration(cn.taketoday.cache.annotation.CacheConfiguration) Cache(cn.taketoday.cache.Cache)

Aggregations

Cache (cn.taketoday.cache.Cache)1 CacheValueRetrievalException (cn.taketoday.cache.CacheValueRetrievalException)1 CacheConfiguration (cn.taketoday.cache.annotation.CacheConfiguration)1 Method (java.lang.reflect.Method)1