class ReadCounterFromCacheAdvice extends CounterInCacheBase {

    @Around("readSingleCounter()")
    public Object readCounter(final ProceedingJoinPoint pjp) throws Throwable {
        if (isDisabled()) {
            getLogger().info("Cache disabled");
            return pjp.proceed();
        }

        // This is injected caching. If anything goes wrong in the caching, LOG
        // the crap outta it, but do not let it surface up past the AOP injection itself.
        // It will be invoked only if underlying method completes successfully.
        String cacheKey = null;
        ReadCounterFromCache annotation;
        AnnotationData data;
        try {
            Method methodToCache = getCacheBase().getMethodToCache(pjp, ReadCounterFromCache.class);
            verifyMethodSignature(methodToCache);
            annotation = methodToCache.getAnnotation(ReadCounterFromCache.class);
            data = AnnotationDataBuilder.buildAnnotationData(annotation, ReadCounterFromCache.class, methodToCache);
            cacheKey = getCacheBase().getCacheKeyBuilder().getCacheKey(data, pjp.getArgs(), methodToCache.toString());
            Long result = getCacheBase().getCache(data).getCounter(cacheKey);

            if (result != null) {
                getLogger().debug("Cache hit.");
                return convertResult(methodToCache, result);
            }
        } catch (Exception ex) {
            warn(ex, "Caching on method %s and key [%s] aborted due to an error.", pjp.toShortString(), cacheKey);
            return pjp.proceed();
        }

        final Object result = pjp.proceed();

        // This is injected caching. If anything goes wrong in the caching, LOG
        // the crap outta it, but do not let it surface up past the AOP injection itself.
        try {
            if (checkData(result, pjp)) {
                long value = ((Number) result).longValue();
                // tricky way to update counter
                getCacheBase().getCache(data).incr(cacheKey, 0, value, annotation.expiration());
            }
        } catch (Exception ex) {
            warn(ex, "Caching on method %s and key [%s] aborted due to an error.", pjp.toShortString(), cacheKey);
        }
        return result;
    }

}