private CacheResultOperation createDefaultOperation(CacheMethodDetails<CacheResult> methodDetails) { return new CacheResultOperation(methodDetails, defaultCacheResolver, defaultKeyGenerator, defaultCacheResolver); }
@Test public void simpleGet() { CacheResultOperation operation = createSimpleOperation(); assertNotNull(operation.getKeyGenerator()); assertNotNull(operation.getExceptionCacheResolver()); assertNull(operation.getExceptionCacheName()); assertEquals(defaultExceptionCacheResolver, operation.getExceptionCacheResolver()); CacheInvocationParameter[] allParameters = operation.getAllParameters(2L); assertEquals(1, allParameters.length); assertCacheInvocationParameter(allParameters[0], Long.class, 2L, 0); CacheInvocationParameter[] keyParameters = operation.getKeyParameters(2L); assertEquals(1, keyParameters.length); assertCacheInvocationParameter(keyParameters[0], Long.class, 2L, 0); }
public CacheResultOperation(CacheMethodDetails<CacheResult> methodDetails, CacheResolver cacheResolver, KeyGenerator keyGenerator, @Nullable CacheResolver exceptionCacheResolver) { super(methodDetails, cacheResolver, keyGenerator); CacheResult ann = methodDetails.getCacheAnnotation(); this.exceptionTypeFilter = createExceptionTypeFilter(ann.cachedExceptions(), ann.nonCachedExceptions()); this.exceptionCacheResolver = exceptionCacheResolver; this.exceptionCacheName = (StringUtils.hasText(ann.exceptionCacheName()) ? ann.exceptionCacheName() : null); }
@Test public void customCacheResolver() { CacheResultOperation operation = getCacheOperation(CacheResultOperation.class, CustomService.class, name.getMethodName(), Long.class); assertJCacheResolver(operation.getCacheResolver(), TestableCacheResolver.class); assertJCacheResolver(operation.getExceptionCacheResolver(), null); assertEquals(KeyGeneratorAdapter.class, operation.getKeyGenerator().getClass()); assertEquals(defaultKeyGenerator, ((KeyGeneratorAdapter) operation.getKeyGenerator()).getTarget()); }
@Test public void fullGetConfig() { CacheMethodDetails<CacheResult> methodDetails = create(CacheResult.class, SampleObject.class, "fullGetConfig", Long.class); CacheResultOperation operation = createDefaultOperation(methodDetails); assertTrue(operation.isAlwaysInvoked()); assertNotNull(operation.getExceptionTypeFilter()); assertTrue(operation.getExceptionTypeFilter().match(IOException.class)); assertFalse(operation.getExceptionTypeFilter().match(NullPointerException.class)); }
@Test public void cacheWithException() { CacheResultOperation op = getDefaultCacheOperation(CacheResultOperation.class, String.class, boolean.class); assertDefaults(op); assertEquals(defaultExceptionCacheResolver, op.getExceptionCacheResolver()); assertEquals("exception", op.getExceptionCacheName()); }
@Nullable private Cache resolveExceptionCache(CacheOperationInvocationContext<CacheResultOperation> context) { CacheResolver exceptionCacheResolver = context.getOperation().getExceptionCacheResolver(); if (exceptionCacheResolver != null) { return extractFrom(context.getOperation().getExceptionCacheResolver().resolveCaches(context)); } return null; }
@Override protected Collection<String> getCacheNames(CacheOperationInvocationContext<?> context) { BasicOperation operation = context.getOperation(); if (!(operation instanceof CacheResultOperation)) { throw new IllegalStateException("Could not extract exception cache name from " + operation); } CacheResultOperation cacheResultOperation = (CacheResultOperation) operation; String exceptionCacheName = cacheResultOperation.getExceptionCacheName(); if (exceptionCacheName != null) { return Collections.singleton(exceptionCacheName); } return null; }
/** * Specify if the method should always be invoked regardless of a cache hit. * By default, the method is only invoked in case of a cache miss. * @see javax.cache.annotation.CacheResult#skipGet() */ public boolean isAlwaysInvoked() { return getCacheAnnotation().skipGet(); }
@Override @Nullable protected Object invoke( CacheOperationInvocationContext<CacheResultOperation> context, CacheOperationInvoker invoker) { CacheResultOperation operation = context.getOperation(); Object cacheKey = generateKey(context); Cache cache = resolveCache(context); Cache exceptionCache = resolveExceptionCache(context); if (!operation.isAlwaysInvoked()) { Cache.ValueWrapper cachedValue = doGet(cache, cacheKey); if (cachedValue != null) { return cachedValue.get(); } checkForCachedException(exceptionCache, cacheKey); } try { Object invocationResult = invoker.invoke(); doPut(cache, cacheKey, invocationResult); return invocationResult; } catch (CacheOperationInvoker.ThrowableWrapper ex) { Throwable original = ex.getOriginal(); cacheException(exceptionCache, operation.getExceptionTypeFilter(), cacheKey, original); throw ex; } }
@Test public void customKeyGenerator() { CacheResultOperation operation = getCacheOperation(CacheResultOperation.class, CustomService.class, name.getMethodName(), Long.class); assertEquals(defaultCacheResolver, operation.getCacheResolver()); assertNull(operation.getExceptionCacheResolver()); assertCacheKeyGenerator(operation.getKeyGenerator(), TestableCacheKeyGenerator.class); }
@Nullable private Cache resolveExceptionCache(CacheOperationInvocationContext<CacheResultOperation> context) { CacheResolver exceptionCacheResolver = context.getOperation().getExceptionCacheResolver(); if (exceptionCacheResolver != null) { return extractFrom(context.getOperation().getExceptionCacheResolver().resolveCaches(context)); } return null; }
@Override protected Collection<String> getCacheNames(CacheOperationInvocationContext<?> context) { BasicOperation operation = context.getOperation(); if (!(operation instanceof CacheResultOperation)) { throw new IllegalStateException("Could not extract exception cache name from " + operation); } CacheResultOperation cacheResultOperation = (CacheResultOperation) operation; String exceptionCacheName = cacheResultOperation.getExceptionCacheName(); if (exceptionCacheName != null) { return Collections.singleton(exceptionCacheName); } return null; }
/** * Specify if the method should always be invoked regardless of a cache hit. * By default, the method is only invoked in case of a cache miss. * @see javax.cache.annotation.CacheResult#skipGet() */ public boolean isAlwaysInvoked() { return getCacheAnnotation().skipGet(); }
@Override @Nullable protected Object invoke( CacheOperationInvocationContext<CacheResultOperation> context, CacheOperationInvoker invoker) { CacheResultOperation operation = context.getOperation(); Object cacheKey = generateKey(context); Cache cache = resolveCache(context); Cache exceptionCache = resolveExceptionCache(context); if (!operation.isAlwaysInvoked()) { Cache.ValueWrapper cachedValue = doGet(cache, cacheKey); if (cachedValue != null) { return cachedValue.get(); } checkForCachedException(exceptionCache, cacheKey); } try { Object invocationResult = invoker.invoke(); doPut(cache, cacheKey, invocationResult); return invocationResult; } catch (CacheOperationInvoker.ThrowableWrapper ex) { Throwable original = ex.getOriginal(); cacheException(exceptionCache, operation.getExceptionTypeFilter(), cacheKey, original); throw ex; } }
@Test public void customKeyGeneratorAndCacheResolver() { CacheResultOperation operation = getCacheOperation(CacheResultOperation.class, CustomServiceWithDefaults.class, name.getMethodName(), Long.class); assertJCacheResolver(operation.getCacheResolver(), TestableCacheResolver.class); assertJCacheResolver(operation.getExceptionCacheResolver(), null); assertCacheKeyGenerator(operation.getKeyGenerator(), TestableCacheKeyGenerator.class); }
@Override protected CacheResultOperation createSimpleOperation() { CacheMethodDetails<CacheResult> methodDetails = create(CacheResult.class, SampleObject.class, "simpleGet", Long.class); return new CacheResultOperation(methodDetails, defaultCacheResolver, defaultKeyGenerator, defaultExceptionCacheResolver); }
@Test public void cache() { CacheResultOperation op = getDefaultCacheOperation(CacheResultOperation.class, String.class); assertDefaults(op); assertNull("Exception caching not enabled so resolver should not be set", op.getExceptionCacheResolver()); }
@Override protected Collection<String> getCacheNames(CacheOperationInvocationContext<?> context) { BasicOperation operation = context.getOperation(); if (!(operation instanceof CacheResultOperation)) { throw new IllegalStateException("Could not extract exception cache name from " + operation); } CacheResultOperation cacheResultOperation = (CacheResultOperation) operation; String exceptionCacheName = cacheResultOperation.getExceptionCacheName(); if (exceptionCacheName != null) { return Collections.singleton(exceptionCacheName); } return null; }
/** * Specify if the method should always be invoked regardless of a cache hit. * By default, the method is only invoked in case of a cache miss. * @see javax.cache.annotation.CacheResult#skipGet() */ public boolean isAlwaysInvoked() { return getCacheAnnotation().skipGet(); }