/** * Copyright (c) 2012-2017, jcabi.com * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: 1) Redistributions of source code must retain the above * copyright notice, this list of conditions and the following * disclaimer. 2) Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution. 3) Neither the name of the jcabi.com nor * the names of its contributors may be used to endorse or promote * products derived from this software without specific prior written * permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. */ package com.jcabi.aspects.aj; import com.jcabi.aspects.Cacheable; import com.jcabi.aspects.Loggable; import com.jcabi.log.Logger; import java.lang.ref.SoftReference; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.Arrays; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.atomic.AtomicInteger; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.reflect.MethodSignature; /** * Cache method results. * *
It is an AspectJ aspect and you are not supposed to use it directly. It * is instantiated by AspectJ runtime framework when your code is annotated * with {@link Cacheable} annotation. * *
The class is thread-safe.
*
* @author Yegor Bugayenko (yegor@tpc2.com)
* @version $Id: 81af9dfb282bbcda540b9148c81ef09cf1cd0199 $
* @since 0.8
*/
@Aspect
@SuppressWarnings("PMD.TooManyMethods")
public final class MethodCacher {
/**
* Calling tunnels.
*/
private final transient
ConcurrentMap Try NOT to change the signature of this method, in order to keep
* it backward compatible.
*
* @param point Joint point
* @return The result of call
* @throws Throwable If something goes wrong inside
* @checkstyle IllegalThrows (4 lines)
*/
@Around("execution(* *(..)) && @annotation(com.jcabi.aspects.Cacheable)")
public Object cache(final ProceedingJoinPoint point) throws Throwable {
final MethodCacher.Key key = new MethodCacher.Key(point);
MethodCacher.Tunnel tunnel;
final Method method = MethodSignature.class
.cast(point.getSignature())
.getMethod();
final Cacheable annot = method.getAnnotation(Cacheable.class);
synchronized (this.tunnels) {
for (final Class> before : annot.before()) {
final boolean flag = Boolean.class.cast(
before.getMethod("flushBefore").invoke(method.getClass())
);
if (flag) {
this.preflush(point);
}
}
tunnel = this.tunnels.get(key);
if (this.isCreateTunnel(tunnel)) {
tunnel = new MethodCacher.Tunnel(
point, key, annot.asyncUpdate()
);
this.tunnels.put(key, tunnel);
}
if (tunnel.expired() && tunnel.asyncUpdate()) {
this.updatekeys.offer(key);
}
for (final Class> after : annot.after()) {
final boolean flag = Boolean.class.cast(
after.getMethod("flushAfter").invoke(method.getClass())
);
if (flag) {
this.postflush(point);
}
}
}
return tunnel.through();
}
/**
* Flush cache.
* @param point Join point
* @return Value of the method
* @since 0.7.14
* @deprecated Since 0.7.17, and preflush() should be used
* @throws Throwable If something goes wrong inside
* @checkstyle IllegalThrows (3 lines)
*/
@Deprecated
public Object flush(final ProceedingJoinPoint point) throws Throwable {
this.preflush(point);
return point.proceed();
}
/**
* Flush cache.
*
* Try NOT to change the signature of this method, in order to keep
* it backward compatible.
*
* @param point Joint point
* @since 0.7.14
*/
@Before
(
// @checkstyle StringLiteralsConcatenation (3 lines)
"execution(* *(..))"
+ " && (@annotation(com.jcabi.aspects.Cacheable.Flush)"
+ " || @annotation(com.jcabi.aspects.Cacheable.FlushBefore))"
)
public void preflush(final JoinPoint point) {
this.flush(point, "before the call");
}
/**
* Flush cache after method execution.
*
* Try NOT to change the signature of this method, in order to keep
* it backward compatible.
*
* @param point Joint point
* @since 0.7.18
*/
@After
(
// @checkstyle StringLiteralsConcatenation (2 lines)
"execution(* *(..))"
+ " && @annotation(com.jcabi.aspects.Cacheable.FlushAfter)"
)
public void postflush(final JoinPoint point) {
this.flush(point, "after the call");
}
/**
* 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
);
}
}
}
}
/**
* Whether create a new Tunnel.
* @param tunnel MethodCacher.Tunnel
* @return Boolean
*/
private boolean isCreateTunnel(final MethodCacher.Tunnel tunnel) {
return tunnel == null || (tunnel.expired() && !tunnel.asyncUpdate());
}
/**
* Mutable caching/calling tunnel, it is thread-safe.
*/
protected static final class Tunnel {
/**
* Proceeding join point.
*/
private final transient ProceedingJoinPoint point;
/**
* Key related to this tunnel.
*/
private final transient MethodCacher.Key key;
/**
* Whether asynchronous update.
*/
private final transient boolean async;
/**
* Was it already executed?
*/
private transient boolean executed;
/**
* When will it expire (moment in time).
*/
private transient long lifetime;
/**
* Has non-null result?
*/
private transient boolean hasresult;
/**
* Cached value.
*/
@SuppressWarnings("PMD.AvoidFieldNameMatchingMethodName")
private transient SoftReference