Search in sources :

Example 1 with GeneratedCacheKey

use of javax.cache.annotation.GeneratedCacheKey in project Payara by payara.

the class CachePutInterceptor method doPut.

@SuppressWarnings("unchecked")
private void doPut(PayaraCacheKeyInvocationContext<CachePut> pctx) throws Throwable {
    CacheKeyGenerator generator = pctx.getGenerator();
    CacheResolverFactory resolverF = pctx.getFactory();
    CacheResolver cacheResolver = resolverF.getCacheResolver(pctx);
    Cache cache = cacheResolver.resolveCache(pctx);
    GeneratedCacheKey key = generator.generateCacheKey(pctx);
    Object value = pctx.getValueParameter().getValue();
    cache.put(key, value);
}
Also used : CacheResolver(javax.cache.annotation.CacheResolver) CacheKeyGenerator(javax.cache.annotation.CacheKeyGenerator) CacheResolverFactory(javax.cache.annotation.CacheResolverFactory) Cache(javax.cache.Cache) GeneratedCacheKey(javax.cache.annotation.GeneratedCacheKey)

Example 2 with GeneratedCacheKey

use of javax.cache.annotation.GeneratedCacheKey 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 3 with GeneratedCacheKey

use of javax.cache.annotation.GeneratedCacheKey in project Payara by payara.

the class CacheRemoveInterceptor method doRemove.

private void doRemove(PayaraCacheKeyInvocationContext<CacheRemove> pctx) {
    CacheKeyGenerator generator = pctx.getGenerator();
    CacheResolverFactory resolverF = pctx.getFactory();
    CacheResolver cacheResolver = resolverF.getCacheResolver(pctx);
    Cache cache = cacheResolver.resolveCache(pctx);
    GeneratedCacheKey key = generator.generateCacheKey(pctx);
    cache.remove(key);
}
Also used : CacheResolver(javax.cache.annotation.CacheResolver) CacheKeyGenerator(javax.cache.annotation.CacheKeyGenerator) CacheResolverFactory(javax.cache.annotation.CacheResolverFactory) Cache(javax.cache.Cache) GeneratedCacheKey(javax.cache.annotation.GeneratedCacheKey)

Aggregations

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