/** * Send a result through, with necessary logging. * * @param result The result to send through * @return The same result/object * @checkstyle DesignForExtensionCheck (2 lines) */ public Object through(final Object result) { final int hit = this.accessed.getAndIncrement(); final Class<?> type = this.method.getDeclaringClass(); if (hit > 0 && LogHelper.enabled(this.level, type)) { LogHelper.log( this.level, type, "%s: %s from cache (hit #%d, %[ms]s old)", this, Mnemos.toText(result, true, false), hit, System.currentTimeMillis() - this.start); } return result; }
/** * Flush cache. * * @param point Joint point * @param when When it happens * @since 0.7.18 */ private void flush(final JoinPoint point, final String when) { synchronized (this.tunnels) { for (final MethodCacher.Key key : this.tunnels.keySet()) { if (!key.sameTarget(point)) { continue; } final MethodCacher.Tunnel removed = this.tunnels.remove(key); final Method method = MethodSignature.class.cast(point.getSignature()).getMethod(); if (LogHelper.enabled(key.getLevel(), method.getDeclaringClass())) { LogHelper.log( key.getLevel(), method.getDeclaringClass(), "%s: %s:%s removed from cache %s", Mnemos.toText(method, point.getArgs(), true, false), key, removed, when); } } } }
/** * Get a result through the tunnel. * * @return The result * @throws Throwable If something goes wrong inside * @checkstyle IllegalThrows (5 lines) */ @SuppressWarnings("PMD.AvoidSynchronizedAtMethodLevel") public synchronized Object through() throws Throwable { if (!this.executed) { final long start = System.currentTimeMillis(); final Object result = this.point.proceed(); this.hasresult = result != null; this.cached = new SoftReference<Object>(result); final Method method = MethodSignature.class.cast(this.point.getSignature()).getMethod(); final Cacheable annot = method.getAnnotation(Cacheable.class); final String suffix; if (annot.forever()) { this.lifetime = Long.MAX_VALUE; suffix = "valid forever"; } else if (annot.lifetime() == 0) { this.lifetime = 0L; suffix = "invalid immediately"; } else { final long msec = annot.unit().toMillis((long) annot.lifetime()); this.lifetime = start + msec; suffix = Logger.format("valid for %[ms]s", msec); } final Class<?> type = method.getDeclaringClass(); if (LogHelper.enabled(this.key.getLevel(), type)) { LogHelper.log( this.key.getLevel(), type, "%s: %s cached in %[ms]s, %s", Mnemos.toText(method, this.point.getArgs(), true, false), Mnemos.toText(this.cached.get(), true, false), System.currentTimeMillis() - start, suffix); } this.executed = true; } return this.key.through(this.cached.get()); }