public Object cache()

in src/main/java/org/apache/geronimo/jcache/simple/cdi/CacheRemoveInterceptor.java [45:96]


    public Object cache(final InvocationContext ic) throws Throwable {
        final CDIJCacheHelper.MethodMeta methodMeta = helper.findMeta(ic);

        final String cacheName = methodMeta.getCacheRemoveCacheName();

        final CacheResolverFactory cacheResolverFactory = methodMeta.getCacheRemoveResolverFactory();
        final CacheKeyInvocationContext<CacheRemove> context = new CacheKeyInvocationContextImpl<CacheRemove>(ic,
                methodMeta.getCacheRemove(), cacheName, methodMeta);
        final CacheResolver cacheResolver = cacheResolverFactory.getCacheResolver(context);
        final Cache<Object, Object> cache = cacheResolver.resolveCache(context);

        final GeneratedCacheKey cacheKey = methodMeta.getCacheRemoveKeyGenerator().generateCacheKey(context);
        final CacheRemove cacheRemove = methodMeta.getCacheRemove();
        final boolean afterInvocation = methodMeta.isCacheRemoveAfter();

        if (!afterInvocation) {
            cache.remove(cacheKey);
        }

        final Object result;
        try {
            result = ic.proceed();
            if (CompletionStage.class.isInstance(result)) {
                final CompletionStage<?> completionStage = CompletionStage.class.cast(result);
                completionStage.exceptionally(t -> {
                    if (afterInvocation) {
                        if (helper.isIncluded(t.getClass(), cacheRemove.evictFor(), cacheRemove.noEvictFor())) {
                            cache.remove(cacheKey);
                        }
                    }
                    if (RuntimeException.class.isInstance(t)) {
                        throw RuntimeException.class.cast(t);
                    }
                    throw new IllegalStateException(t);
                });
            }
        } catch (final Throwable t) {
            if (afterInvocation) {
                if (helper.isIncluded(t.getClass(), cacheRemove.evictFor(), cacheRemove.noEvictFor())) {
                    cache.remove(cacheKey);
                }
            }

            throw t;
        }

        if (afterInvocation) {
            cache.remove(cacheKey);
        }

        return result;
    }