Search in sources :

Example 1 with PayaraCacheKeyInvocationContext

use of fish.payara.cdi.jsr107.implementation.PayaraCacheKeyInvocationContext in project Payara by payara.

the class CachePutInterceptor method cachePut.

@AroundInvoke
public Object cachePut(InvocationContext ctx) throws Throwable {
    if (!isEnabled()) {
        return ctx.proceed();
    }
    CachePut annotation = ctx.getMethod().getAnnotation(CachePut.class);
    PayaraCacheKeyInvocationContext<CachePut> pctx = new PayaraCacheKeyInvocationContext<>(ctx, annotation);
    if (!annotation.afterInvocation()) {
        doPut(pctx);
    }
    Object result = null;
    try {
        result = ctx.proceed();
    } catch (Throwable e) {
        if (annotation.afterInvocation()) {
            if (shouldICache(annotation.cacheFor(), annotation.noCacheFor(), e, false)) {
                doPut(pctx);
            }
        }
        throw e;
    }
    if (annotation.afterInvocation()) {
        doPut(pctx);
    }
    return result;
}
Also used : CachePut(javax.cache.annotation.CachePut) PayaraCacheKeyInvocationContext(fish.payara.cdi.jsr107.implementation.PayaraCacheKeyInvocationContext) AroundInvoke(javax.interceptor.AroundInvoke)

Example 2 with PayaraCacheKeyInvocationContext

use of fish.payara.cdi.jsr107.implementation.PayaraCacheKeyInvocationContext in project Payara by payara.

the class CacheRemoveInterceptor method cacheRemove.

@AroundInvoke
public Object cacheRemove(InvocationContext ctx) throws Throwable {
    if (!isEnabled()) {
        return ctx.proceed();
    }
    CacheRemove annotation = ctx.getMethod().getAnnotation(CacheRemove.class);
    PayaraCacheKeyInvocationContext<CacheRemove> pctx = new PayaraCacheKeyInvocationContext<>(ctx, annotation);
    if (!annotation.afterInvocation()) {
        doRemove(pctx);
    }
    Object result = null;
    try {
        result = ctx.proceed();
    } catch (Throwable e) {
        if (annotation.afterInvocation()) {
            if (shouldIEvict(annotation.evictFor(), annotation.noEvictFor(), e)) {
                doRemove(pctx);
            }
        }
        throw e;
    }
    if (annotation.afterInvocation()) {
        doRemove(pctx);
    }
    return result;
}
Also used : CacheRemove(javax.cache.annotation.CacheRemove) PayaraCacheKeyInvocationContext(fish.payara.cdi.jsr107.implementation.PayaraCacheKeyInvocationContext) AroundInvoke(javax.interceptor.AroundInvoke)

Example 3 with PayaraCacheKeyInvocationContext

use of fish.payara.cdi.jsr107.implementation.PayaraCacheKeyInvocationContext in project Payara by payara.

the class CacheResultInterceptor method cacheResult.

@AroundInvoke
@SuppressWarnings("unchecked")
public Object cacheResult(InvocationContext ctx) throws Throwable {
    if (!isEnabled()) {
        return ctx.proceed();
    }
    // get my annotation
    CacheResult annotation = ctx.getMethod().getAnnotation(CacheResult.class);
    PayaraCacheKeyInvocationContext<CacheResult> pctx = new PayaraCacheKeyInvocationContext<>(ctx, annotation);
    CacheResolverFactory resolverF = pctx.getFactory();
    CacheResolver cacheResolver = resolverF.getCacheResolver(pctx);
    Cache cache = cacheResolver.resolveCache(pctx);
    boolean cacheExceptions = (annotation.exceptionCacheName() != null && annotation.exceptionCacheName().length() > 0);
    CacheKeyGenerator generator = pctx.getGenerator();
    GeneratedCacheKey key = generator.generateCacheKey(pctx);
    if (!annotation.skipGet()) {
        Object cacheResult = cache.get(key);
        if (cacheResult != null) {
            return cacheResult;
        } else {
            // check exception cache
            if (cacheExceptions) {
                Cache exceptionCache = resolverF.getExceptionCacheResolver(pctx).resolveCache(pctx);
                Throwable e = (Throwable) exceptionCache.get(key);
                if (e != null) {
                    throw e;
                }
            }
        }
    }
    // call the method
    Object result = null;
    try {
        result = ctx.proceed();
        cache.put(key, result);
    } catch (Throwable e) {
        if (cacheExceptions) {
            Cache exceptionCache = resolverF.getExceptionCacheResolver(pctx).resolveCache(pctx);
            if (shouldICache(annotation.cachedExceptions(), annotation.nonCachedExceptions(), e, true)) {
                exceptionCache.put(key, e);
            }
        }
        throw e;
    }
    return result;
}
Also used : CacheResult(javax.cache.annotation.CacheResult) CacheResolver(javax.cache.annotation.CacheResolver) PayaraCacheKeyInvocationContext(fish.payara.cdi.jsr107.implementation.PayaraCacheKeyInvocationContext) CacheKeyGenerator(javax.cache.annotation.CacheKeyGenerator) CacheResolverFactory(javax.cache.annotation.CacheResolverFactory) Cache(javax.cache.Cache) GeneratedCacheKey(javax.cache.annotation.GeneratedCacheKey) AroundInvoke(javax.interceptor.AroundInvoke)

Example 4 with PayaraCacheKeyInvocationContext

use of fish.payara.cdi.jsr107.implementation.PayaraCacheKeyInvocationContext in project Payara by payara.

the class CacheRemoveAllInterceptor method cacheRemoveAll.

@AroundInvoke
public Object cacheRemoveAll(InvocationContext ctx) throws Throwable {
    if (!isEnabled()) {
        return ctx.proceed();
    }
    CacheRemoveAll annotation = ctx.getMethod().getAnnotation(CacheRemoveAll.class);
    PayaraCacheKeyInvocationContext<CacheRemoveAll> pctx = new PayaraCacheKeyInvocationContext<>(ctx, annotation);
    if (!annotation.afterInvocation()) {
        doRemoveAll(pctx);
    }
    Object result = null;
    try {
        result = ctx.proceed();
    } catch (Throwable e) {
        if (annotation.afterInvocation()) {
            if (shouldIEvict(annotation.evictFor(), annotation.noEvictFor(), e)) {
                doRemoveAll(pctx);
            }
        }
        throw e;
    }
    if (annotation.afterInvocation()) {
        doRemoveAll(pctx);
    }
    return result;
}
Also used : CacheRemoveAll(javax.cache.annotation.CacheRemoveAll) PayaraCacheKeyInvocationContext(fish.payara.cdi.jsr107.implementation.PayaraCacheKeyInvocationContext) AroundInvoke(javax.interceptor.AroundInvoke)

Aggregations

PayaraCacheKeyInvocationContext (fish.payara.cdi.jsr107.implementation.PayaraCacheKeyInvocationContext)4 AroundInvoke (javax.interceptor.AroundInvoke)4 Cache (javax.cache.Cache)1 CacheKeyGenerator (javax.cache.annotation.CacheKeyGenerator)1 CachePut (javax.cache.annotation.CachePut)1 CacheRemove (javax.cache.annotation.CacheRemove)1 CacheRemoveAll (javax.cache.annotation.CacheRemoveAll)1 CacheResolver (javax.cache.annotation.CacheResolver)1 CacheResolverFactory (javax.cache.annotation.CacheResolverFactory)1 CacheResult (javax.cache.annotation.CacheResult)1 GeneratedCacheKey (javax.cache.annotation.GeneratedCacheKey)1