Example usage for org.springframework.cache.interceptor CacheOperationExpressionEvaluator NO_RESULT

List of usage examples for org.springframework.cache.interceptor CacheOperationExpressionEvaluator NO_RESULT

Introduction

In this page you can find the example usage for org.springframework.cache.interceptor CacheOperationExpressionEvaluator NO_RESULT.

Prototype

Object NO_RESULT

To view the source code for org.springframework.cache.interceptor CacheOperationExpressionEvaluator NO_RESULT.

Click Source Link

Document

Indicate that there is no result variable.

Usage

From source file:org.springframework.cache.interceptor.CacheAspectSupport.java

@Nullable
private Object execute(final CacheOperationInvoker invoker, Method method, CacheOperationContexts contexts) {
    // Special handling of synchronized invocation
    if (contexts.isSynchronized()) {
        CacheOperationContext context = contexts.get(CacheableOperation.class).iterator().next();
        if (isConditionPassing(context, CacheOperationExpressionEvaluator.NO_RESULT)) {
            Object key = generateKey(context, CacheOperationExpressionEvaluator.NO_RESULT);
            Cache cache = context.getCaches().iterator().next();
            try {
                return wrapCacheValue(method, cache.get(key, () -> {
                    return unwrapReturnValue(invokeOperation(invoker));
                }));/*  w w w  .j  ava2 s .com*/
            } catch (Cache.ValueRetrievalException ex) {
                // The invoker wraps any Throwable in a ThrowableWrapper instance so we
                // can just make sure that one bubbles up the stack.
                throw (CacheOperationInvoker.ThrowableWrapper) ex.getCause();
            }
        } else {
            // No caching required, only call the underlying method
            return invokeOperation(invoker);
        }
    }

    // Process any early evictions
    processCacheEvicts(contexts.get(CacheEvictOperation.class), true,
            CacheOperationExpressionEvaluator.NO_RESULT);

    // Check if we have a cached item matching the conditions
    Cache.ValueWrapper cacheHit = findCachedItem(contexts.get(CacheableOperation.class));

    // Collect puts from any @Cacheable miss, if no cached item is found
    List<CachePutRequest> cachePutRequests = new LinkedList<>();
    if (cacheHit == null) {
        collectPutRequests(contexts.get(CacheableOperation.class), CacheOperationExpressionEvaluator.NO_RESULT,
                cachePutRequests);
    }

    Object cacheValue;
    Object returnValue;

    if (cacheHit != null && cachePutRequests.isEmpty() && !hasCachePut(contexts)) {
        // If there are no put requests, just use the cache hit
        cacheValue = cacheHit.get();
        returnValue = wrapCacheValue(method, cacheValue);
    } else {
        // Invoke the method if we don't have a cache hit
        returnValue = invokeOperation(invoker);
        cacheValue = unwrapReturnValue(returnValue);
    }

    // Collect any explicit @CachePuts
    collectPutRequests(contexts.get(CachePutOperation.class), cacheValue, cachePutRequests);

    // Process any collected put requests, either from @CachePut or a @Cacheable miss
    for (CachePutRequest cachePutRequest : cachePutRequests) {
        cachePutRequest.apply(cacheValue);
    }

    // Process any late evictions
    processCacheEvicts(contexts.get(CacheEvictOperation.class), false, cacheValue);

    return returnValue;
}

From source file:org.springframework.cache.interceptor.CacheAspectSupport.java

/**
 * Find a cached item only for {@link CacheableOperation} that passes the condition.
 * @param contexts the cacheable operations
 * @return a {@link Cache.ValueWrapper} holding the cached item,
 * or {@code null} if none is found/*from  w ww  . j  a  va2s . c  o  m*/
 */
@Nullable
private Cache.ValueWrapper findCachedItem(Collection<CacheOperationContext> contexts) {
    Object result = CacheOperationExpressionEvaluator.NO_RESULT;
    for (CacheOperationContext context : contexts) {
        if (isConditionPassing(context, result)) {
            Object key = generateKey(context, result);
            Cache.ValueWrapper cached = findInCaches(context, key);
            if (cached != null) {
                return cached;
            } else {
                if (logger.isTraceEnabled()) {
                    logger.trace("No cache entry for key '" + key + "' in cache(s) " + context.getCacheNames());
                }
            }
        }
    }
    return null;
}