zoukankan      html  css  js  c++  java
  • 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());

    }
    }

  • 相关阅读:
    java 的 四 个 基 本 特 性 ——封装 继承 多态 抽象
    java中的对象 方法 引用 等一些抽象的概念是什么意思呢?
    谈谈java中的并发(一)
    来说说Java中String 类的那些事情
    Alibaba SpringCloud (一) Nacos 集群环境搭建
    Window 环境下SonarQube的安装与部署
    Windows 环境下MySQL 8.0.15 安装配置指南
    Docker 入门
    Springboot中,Tomcat启动war包的流程
    SpringCloud入门(十一):Sleuth 与 Zipkin分布式链路跟踪
  • 原文地址:https://www.cnblogs.com/blogszixin/p/3289516.html
Copyright © 2011-2022 走看看