Search in sources :

Example 1 with CacheResolver

use of javax.cache.annotation.CacheResolver 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 CacheResolver

use of javax.cache.annotation.CacheResolver 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 CacheResolver

use of javax.cache.annotation.CacheResolver in project spring-framework by spring-projects.

the class CacheResolverAdapterTests method getCacheResolver.

@SuppressWarnings({ "rawtypes", "unchecked" })
protected CacheResolver getCacheResolver(CacheInvocationContext<? extends Annotation> context, String cacheName) {
    CacheResolver cacheResolver = mock(CacheResolver.class);
    javax.cache.Cache cache;
    if (cacheName == null) {
        cache = null;
    } else {
        cache = mock(javax.cache.Cache.class);
        given(cache.getName()).willReturn(cacheName);
    }
    given(cacheResolver.resolveCache(context)).willReturn(cache);
    return cacheResolver;
}
Also used : CacheResolver(javax.cache.annotation.CacheResolver) Cache(org.springframework.cache.Cache)

Example 4 with CacheResolver

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

the class CacheRemoveAllInterceptor method doRemoveAll.

private void doRemoveAll(PayaraCacheKeyInvocationContext<CacheRemoveAll> pctx) {
    CacheResolverFactory resolverF = pctx.getFactory();
    CacheResolver cacheResolver = resolverF.getCacheResolver(pctx);
    Cache cache = cacheResolver.resolveCache(pctx);
    cache.removeAll();
}
Also used : CacheResolver(javax.cache.annotation.CacheResolver) CacheResolverFactory(javax.cache.annotation.CacheResolverFactory) Cache(javax.cache.Cache)

Example 5 with CacheResolver

use of javax.cache.annotation.CacheResolver 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

CacheResolver (javax.cache.annotation.CacheResolver)5 Cache (javax.cache.Cache)4 CacheResolverFactory (javax.cache.annotation.CacheResolverFactory)4 CacheKeyGenerator (javax.cache.annotation.CacheKeyGenerator)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 Cache (org.springframework.cache.Cache)1