• redis缓存相关


    import com.meiliwan.emall.commons.jedisTool.JedisKey;

    import java.lang.annotation.*;

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    @Inherited
    public @interface HelloWorld {
    public JedisKey key();
    }

    package com.meiliwan.emall.pms.util;


    import com.google.gson.JsonObject;
    import com.google.gson.JsonParser;
    import com.meiliwan.emall.commons.jedisTool.JedisKey;
    import com.meiliwan.emall.commons.jedisTool.ShardJedisTool;
    import com.meiliwan.emall.commons.util.StringUtil;
    import com.meiliwan.emall.icetool.JSONTool;
    import org.apache.commons.lang.StringUtils;
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.aspectj.lang.annotation.*;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.stereotype.Component;

    import java.io.Serializable;
    import java.lang.reflect.Method;

    import static com.meiliwan.emall.icetool.JSONTool.addToResult;


    @Aspect
    @Component
    public class HelloWorldAspect {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Pointcut("execution(* com.meiliwan.emall.*.service..*Service.*(..)) && @annotation(com.meiliwan.emall.pms.util.HelloWorld)")
    public void helloWorldAnnotated() {}

    @Before("helloWorldAnnotated()")
    public void before(JoinPoint jp) throws Throwable{
    logger.debug("==============================before: "
    + jp.getSignature());


    }

    @Around("helloWorldAnnotated()")
    public void around(ProceedingJoinPoint pjp) throws Throwable{
    logger.debug("==============================@Around.before");

    //获取方法名称
    String methodName = pjp.getSignature().getName();
    if (StringUtils.isNotEmpty(methodName)){
    //获取目标类名
    Class targetClass = pjp.getTarget().getClass();

    Method[] methods = targetClass.getDeclaredMethods();
    if (methods != null){
    Method mt = null;
    //循环匹配对应的查找方法
    for(Method method : methods){
    if(method == null)continue;
    if(method.getName().equals(methodName)){
    mt = method;
    break;
    }
    }
    if (mt!=null){
    boolean hasAnnotation = mt.isAnnotationPresent(HelloWorld.class);
    if (hasAnnotation){
    HelloWorld annotations = mt.getAnnotation(HelloWorld.class);
    JedisKey key = annotations.key();
    if (key!=null&&!key.equals("")){
    //获取方法参数
    Object[] args = pjp.getArgs();
    JsonObject resultObj = (JsonObject)args[0];
    Object redidId = args[1];
    //加一个异常捕获,用于特殊情况,如果缓存宕机或者链接超时等
    try {
    //查缓存
    String obj = ShardJedisTool.getVolatile().get(key, (Serializable) redidId);
    if(obj!=null&&!obj.equals("")){
    JsonObject redisObj = (JsonObject)(new JsonParser().parse(obj));
    addToResult(redisObj.get("resultObj").getAsJsonObject(),resultObj);
    } else{
    //走数据库查询
    pjp.proceed();
    //缓存到缓存中
    ShardJedisTool.getVolatile().set(key, (Serializable) redidId, resultObj);
    }
    }catch (Exception e){
    logger.debug("========缓存宕机或者链接超时等=======");
    pjp.proceed();
    }
    }
    }
    }
    }
    }

    logger.debug("==============================@Around.after");

    }

    @After("helloWorldAnnotated()")
    public void after(JoinPoint joinPoint) {
    logger.debug("==============================after: "
    + joinPoint.getSignature());

    }
    }

  • 相关阅读:
    chrome调试
    css与jquery、图标字体、常用数据
    subline 快捷键与功能解释
    mysql-5.7.25安装及常用语法
    python 异步IO-aiohttp与简单的异步HTTP客户端/服务器
    python异步IO-asyncio、async和await
    python 异步IO-协程
    python 异步IO
    python-生成器即send()用法
    python -迭代器与生成器 以及 iterable(可迭代对象)、yield语句
  • 原文地址:https://www.cnblogs.com/blogszixin/p/3289516.html
Copyright © 2020-2023  润新知