@Test public void testInvoke() throws Exception { final String parentKey = null; expect(mockDao.get(parentKey, 1L)).andReturn("1").once(); expect(mockDao.get(parentKey, 2L)).andReturn("2").once(); replay(mockDao); String value = dao.get(parentKey, 1L); assertTrue("1".equals(value)); value = dao.get(parentKey, 1L); assertTrue("1".equals(value)); value = dao.get(parentKey, 2L); assertTrue("2".equals(value)); }
@Override public Object invoke(final MethodInvocation methodInvocation) throws Throwable { final Method method = methodInvocation.getMethod(); final CacheResult annotation = method.getAnnotation(CacheResult.class); checkNotNull(annotation); final CacheKeyInvocationContext<CacheResult> context = new GuiceCacheKeyInvocationContext<>(methodInvocation, annotation, annotation.cacheName()); LOGGER.debug("Cache RESULT {}", context.getMethod().getName()); final Cache<GeneratedCacheKey, Optional<?>> cache = getCache(context, methodInvocation); final CacheKeyGenerator keyGenerator = getCacheKeyGenerator(methodInvocation, annotation.cacheKeyGenerator()); final GeneratedCacheKey key = keyGenerator.generateCacheKey(context); Object result; if(!annotation.skipGet()) { Optional<?> value = cache.get(key, new Callable<Optional<?>>() { @Override public Optional<?> call() throws Exception { LOGGER.trace("Loading for {}({})", method.getName(), key); try { final Object value = methodInvocation.proceed(); return null != value ? Optional.of(value) : Optional.absent(); } catch (Throwable throwable) { LOGGER.error("Failed to populate cache {} {}", method.getName(), throwable); throw new ExecutionException("During invocation: ", throwable); } } }); return value.isPresent() ? value.get() : null; } result = methodInvocation.proceed(); cache.put(key, null != result ? Optional.of(result) : Optional.absent()); return result; }
CacheResultMethodInterceptor extends AbstractCacheMethodInterceptor { @Override public Object invoke(final MethodInvocation methodInvocation) throws Throwable { final Method method = methodInvocation.getMethod(); final CacheResult annotation = method.getAnnotation(CacheResult.class); checkNotNull(annotation); final CacheKeyInvocationContext<CacheResult> context = new GuiceCacheKeyInvocationContext<>(methodInvocation, annotation, annotation.cacheName()); LOGGER.debug("Cache RESULT {}", context.getMethod().getName()); final Cache<GeneratedCacheKey, Optional<?>> cache = getCache(context, methodInvocation); final CacheKeyGenerator keyGenerator = getCacheKeyGenerator(methodInvocation, annotation.cacheKeyGenerator()); final GeneratedCacheKey key = keyGenerator.generateCacheKey(context); Object result; if(!annotation.skipGet()) { Optional<?> value = cache.get(key, new Callable<Optional<?>>() { @Override public Optional<?> call() throws Exception { LOGGER.trace("Loading for {}({})", method.getName(), key); try { final Object value = methodInvocation.proceed(); return null != value ? Optional.of(value) : Optional.absent(); } catch (Throwable throwable) { LOGGER.error("Failed to populate cache {} {}", method.getName(), throwable); throw new ExecutionException("During invocation: ", throwable); } } }); return value.isPresent() ? value.get() : null; } result = methodInvocation.proceed(); cache.put(key, null != result ? Optional.of(result) : Optional.absent()); return result; } }
CacheResultMethodInterceptor extends AbstractCacheMethodInterceptor { @Override public Object invoke(final MethodInvocation methodInvocation) throws Throwable { final Method method = methodInvocation.getMethod(); final CacheResult annotation = method.getAnnotation(CacheResult.class); checkNotNull(annotation); final CacheKeyInvocationContext<CacheResult> context = new GuiceCacheKeyInvocationContext<>(methodInvocation, annotation, annotation.cacheName()); LOGGER.debug("Cache RESULT {}", context.getMethod().getName()); final Cache<GeneratedCacheKey, Optional<?>> cache = getCache(context, methodInvocation); final CacheKeyGenerator keyGenerator = getCacheKeyGenerator(methodInvocation, annotation.cacheKeyGenerator()); final GeneratedCacheKey key = keyGenerator.generateCacheKey(context); Object result; if(!annotation.skipGet()) { Optional<?> value = cache.get(key, new Callable<Optional<?>>() { @Override public Optional<?> call() throws Exception { LOGGER.trace("Loading for {}({})", method.getName(), key); try { final Object value = methodInvocation.proceed(); return null != value ? Optional.of(value) : Optional.absent(); } catch (Throwable throwable) { LOGGER.error("Failed to populate cache {} {}", method.getName(), throwable); throw new ExecutionException("During invocation: ", throwable); } } }); return value.isPresent() ? value.get() : null; } result = methodInvocation.proceed(); cache.put(key, null != result ? Optional.of(result) : Optional.absent()); return result; } @Inject  CacheResultMethodInterceptor(Provider<CacheResolver> cacheResolverProvider, Provider<CacheKeyGenerator> defaultCacheKeyGeneratorProvider); }
CacheResultMethodInterceptor extends AbstractCacheMethodInterceptor { @Override public Object invoke(final MethodInvocation methodInvocation) throws Throwable { final Method method = methodInvocation.getMethod(); final CacheResult annotation = method.getAnnotation(CacheResult.class); checkNotNull(annotation); final CacheKeyInvocationContext<CacheResult> context = new GuiceCacheKeyInvocationContext<>(methodInvocation, annotation, annotation.cacheName()); LOGGER.debug("Cache RESULT {}", context.getMethod().getName()); final Cache<GeneratedCacheKey, Optional<?>> cache = getCache(context, methodInvocation); final CacheKeyGenerator keyGenerator = getCacheKeyGenerator(methodInvocation, annotation.cacheKeyGenerator()); final GeneratedCacheKey key = keyGenerator.generateCacheKey(context); Object result; if(!annotation.skipGet()) { Optional<?> value = cache.get(key, new Callable<Optional<?>>() { @Override public Optional<?> call() throws Exception { LOGGER.trace("Loading for {}({})", method.getName(), key); try { final Object value = methodInvocation.proceed(); return null != value ? Optional.of(value) : Optional.absent(); } catch (Throwable throwable) { LOGGER.error("Failed to populate cache {} {}", method.getName(), throwable); throw new ExecutionException("During invocation: ", throwable); } } }); return value.isPresent() ? value.get() : null; } result = methodInvocation.proceed(); cache.put(key, null != result ? Optional.of(result) : Optional.absent()); return result; } @Inject  CacheResultMethodInterceptor(Provider<CacheResolver> cacheResolverProvider, Provider<CacheKeyGenerator> defaultCacheKeyGeneratorProvider); @Override Object invoke(final MethodInvocation methodInvocation); }
CacheResultMethodInterceptor extends AbstractCacheMethodInterceptor { @Override public Object invoke(final MethodInvocation methodInvocation) throws Throwable { final Method method = methodInvocation.getMethod(); final CacheResult annotation = method.getAnnotation(CacheResult.class); checkNotNull(annotation); final CacheKeyInvocationContext<CacheResult> context = new GuiceCacheKeyInvocationContext<>(methodInvocation, annotation, annotation.cacheName()); LOGGER.debug("Cache RESULT {}", context.getMethod().getName()); final Cache<GeneratedCacheKey, Optional<?>> cache = getCache(context, methodInvocation); final CacheKeyGenerator keyGenerator = getCacheKeyGenerator(methodInvocation, annotation.cacheKeyGenerator()); final GeneratedCacheKey key = keyGenerator.generateCacheKey(context); Object result; if(!annotation.skipGet()) { Optional<?> value = cache.get(key, new Callable<Optional<?>>() { @Override public Optional<?> call() throws Exception { LOGGER.trace("Loading for {}({})", method.getName(), key); try { final Object value = methodInvocation.proceed(); return null != value ? Optional.of(value) : Optional.absent(); } catch (Throwable throwable) { LOGGER.error("Failed to populate cache {} {}", method.getName(), throwable); throw new ExecutionException("During invocation: ", throwable); } } }); return value.isPresent() ? value.get() : null; } result = methodInvocation.proceed(); cache.put(key, null != result ? Optional.of(result) : Optional.absent()); return result; } @Inject  CacheResultMethodInterceptor(Provider<CacheResolver> cacheResolverProvider, Provider<CacheKeyGenerator> defaultCacheKeyGeneratorProvider); @Override Object invoke(final MethodInvocation methodInvocation);  }