AOP annotations in the form of integrated memcache

1. First Custom Note: Add the cache

@Target (ElementType.METHOD)
@Retention (RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface the Memcached {
// default = STATIC Key prefix may be used to clear the cache OMS
String prefix () default "STATIC_";
// Key
String Key () default "";
// filter
String Conditions () default "to true";
// packet buffer
String Group () default "Portal-HOS-mclient1";
// buffer period of two days unit S
int expiration () default 60 * 60 * 48;
}
2. class section

@Aspect
public class MemCachedAop {

private static final Logger log = LoggerFactory.getLogger(MemCachedAop.class);

@Value("${enable.static.cache}")
private boolean enableCache;

@Pointcut("execution(* com.ylzinfo.hospital.portal.service.appservice.*.*.*(..))")
protected void appservicePointcut(){}

@Autowired
private ICacheClient cacheClient;

/**
* 写入或者读取缓存
* 仅针对有注解的且该包下的方法
*/
@Around("(@annotation(memcached) && appservicePointcut())")
public Object doMemcachedAround(ProceedingJoinPoint call, Memcached memcached) throws Throwable {
String packageName = call.getSignature().getDeclaringTypeName();
Call.getSignature methodName = String () getName ();.
Log.info ( "execution method: {} -> {}", the packageName, methodName);
// returns the final result
Object Result = null;
// check Conditions
IF (null = the memcached!
&& checkConditions (Call, memcached.conditions ())
&& enableCache) {
String Key = resolvingKey (Call, memcached.prefix (), memcached.key ());
String memcached.group Group = ();
Result cacheClient.get = (Group, Key);
IF (Result == null) {
// do not exist in memcached
the try {
// perform aop intercept method
Result = call.proceed ();
// Get annotation configuration memcached expiration time
int = memcached.expiration expiration ();
cacheClient.put (Group, Key, Result, expiration);
log.info ( "\ n [] buffer write Memcached" +
"\ NGROUP = {}" +
"\ NKey = {}" +
"\ nValue = {}" +
"\ nexpiration = {}", Group, Key , JSON.toJSON (Result), expiration);
} the catch (the Throwable E) {
log.error ( "failed to perform the method: {} -> {}", the packageName, methodName);
log.error ( "failure reason: {} ", e.getMessage ());
}
} the else {
// there is a direct return to the memcached
log.info (" \ n [] buffer read Memcached "+
" \ NGROUP = {} "+
" \ NKey = {} " +
"\ nValue = {}", Group, Key, JSON.toJSON (Result));
}
} {the else
the try {
Result = call.proceed ();
} the catch (the Throwable E) {
log.error ( "failed to perform the method : {} -> {} " , packageName,methodName);
log.error ( "failure reason: {}", e.getMessage ());
}
}
Return Result;
}



/ **
* Retrieve the cached Key
* Key is defined in the annotation support SPEL expression
* @param Key
* @param Method
* @param args
* @return
* /
Private parseKey String (String Key, Method, Method , Object [] args) {

// Get the intercepted method parameter name list (using Spring support libraries)
LocalVariableTableParameterNameDiscoverer U =
new new LocalVariableTableParameterNameDiscoverer ();
String [] = u.getParameterNames paraNameArr (method);
// key is used for SPEL analytical
ExpressionParser is SpelExpressionParser new new Parser = ();
// SPEL context
, StandardEvaluationContext, new new context, StandardEvaluationContext, = ();
// put into SPEL context of method parameters
for (int i = 0; i <paraNameArr.length; i ++) {
context.setVariable (paraNameArr [I], args [I]);
}
return parser.parseExpression (Key) .getValue (context, String.class);
}

/ **
* Get method of the object is intercepted
*
* MethodSignature.getMethod () the object is a method of obtaining the top-level parent class interface or
* to cache annotation on the class method implementation
* should be used so that the reflection method of the object to get the current object
* /
public method, getMethod (ProceedingJoinPoint PJP) {
// Get the type of the parameter
object [] args = pjp.getArgs ();
Class [] = new new Class argTypes [pjp.getArgs () length.];
for (int I = 0; I <args.length; I ++) {
argTypes [I] = args [ I] .getClass ();
}
Method, Method = null;
the try {
Method = pjp.getTarget () getClass () getMethod (pjp.getSignature () getName (), argTypes.)..;
The catch} (a NoSuchMethodException E) {
log.error (e.getMessage ());
} the catch (a SecurityException E) {
log.error (e.getMessage ());
}
return Method;
}

/ **
* Analytical Key
* @author zengfanqi
* @date 2019/5/6
* @param
* @return String
* /
public String resolvingKey (Call ProceedingJoinPoint, prefix String, String key) {

// if the key is empty directly method name as key
String methodName = call.getSignature () .getName ();
iF (StringUtils.isEmpty (key)) {
return prefix methodName +;
}

// determines whether the key is the expression spel
iF (key.startsWith ( "#")) {
Method, Method getMethod = (Call );
String parsekey = parseKey(key,method,call.getArgs());
if(StringUtils.isEmpty(parsekey)){
parsekey = methodName;
}
key = prefix+parsekey;
}else{
key = prefix+key;
}
return key;
}

/**
* 校验conditions
* @author zengfanqi
* @date 2019/5/6
* @param
* @return
*/
public boolean checkConditions(ProceedingJoinPoint call,String conditions){
boolean flag = false;
Method method=getMethod(call);
String condition = parseKey(conditions,method,call.getArgs());
if("true".equals(condition)){
flag = true;
}
return flag;
}
}
--------------------- 

Guess you like

Origin www.cnblogs.com/ly570/p/10992441.html