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());

    }
    }

  • 相关阅读:
    问题-[DelphiXE2]提示第三控件不存在
    问题-[DelphiXE2]编译程序体积大的问题
    问题-[delphi2007、2010]无法二次启动,报EditorLineEnds.ttr被占用,进程一直有bds.exe?
    问题-[VMware Workstation]断电后,重启电脑,之后就提示“内部错误”
    问题-[Delphi]通过Map文件查找内存地址出错代码所在行
    问题-[WIN8.132位系统]安装Win8.1 遇到无法升级.NET Framework 3.5.1
    问题-[DelphiXE7]新建的安桌模拟器运行程序闪退
    问题-[Delphi]用LoadLibrary加载DLL时返回0的错误
    问题-[Access]“无法打开工作组信息文件中的表 'MSysAccounts'”的问题的解决方法
    教程-Delphi 调用控制面板设置功能
  • 原文地址:https://www.cnblogs.com/blogszixin/p/3289516.html
Copyright © 2011-2022 走看看