All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gitee.fufu669.aspect.CacheFetchLaterAop Maven / Gradle / Ivy

There is a newer version: 6.666.66021
Show newest version
package com.gitee.fufu669.aspect;

import com.gitee.fufu669.common.CacheKeyCommon;
import com.gitee.fufu669.config.exception.CacheEmptyInstance;
import com.gitee.fufu669.service.CacheService;
import com.gitee.fufu669.utils.CacheAopUtil;
import com.gitee.fufu669.utils.CacheHtmlUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;

@Lazy
@Aspect
@Component
/** @author wangfupeng */
public class CacheFetchLaterAop {

    public static final Logger logger = LoggerFactory.getLogger(CacheFetchLaterAop.class);

    @Autowired
    CacheService cacheService;

    @Autowired
    @Qualifier(value="threadPoolTaskExecutorCacheFetchLater")
    ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Around("@annotation(com.gitee.fufu669.aspect.CacheFetchLater)")
    public Object aroundMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        int refreshSeconds = CacheKeyCommon.CACHEFETCHLATERAOP_REFRESH_EXPIRE_SECONDS;
        String methodName = null;
        int expireSeconds = CacheKeyCommon.CACHESERVICE_REDIS_DEFAULT_EXPIRED_SECONDS_1_WEEK;
        String description = "";
        try {
            String targetName = proceedingJoinPoint.getTarget().getClass().getName();
            methodName = proceedingJoinPoint.getSignature().getName();
            Object[] arguments = proceedingJoinPoint.getArgs();
            Class targetClass = Class.forName(targetName);
            Method[] methods = targetClass.getMethods();
            for (Method method : methods) {
                if (method.getName().equals(methodName)) {
                    refreshSeconds = method.getAnnotation(CacheFetchLater.class).refreshSeconds();
                    expireSeconds = method.getAnnotation(CacheFetchLater.class).expireSeconds();
                    description = method.getAnnotation(CacheFetchLater.class).description();
                    break;
                }
            }
        } catch (Exception e) {
            logger.info(e.toString());
        }
        logger.info("%%%%%%CacheFetchLaterAop:aroundMethod:description:"+ description+"%%%%%%");
        String cacheKey = CacheAopUtil.getCacheKey(proceedingJoinPoint);
        Object value = cacheService.getValue(cacheKey);
        if (value != null) {
            boolean refreshFlag = true;
            refreshFlag = CacheAopUtil.isRefreshFlag(refreshSeconds, cacheKey, cacheService);
            if (refreshFlag) {
                CustomThreadToRunAndSaveToRedis customThreadToRunAndSaveToRedis = new CustomThreadToRunAndSaveToRedis();
                customThreadToRunAndSaveToRedis.setProceedingJoinPoint(proceedingJoinPoint);
                customThreadToRunAndSaveToRedis.setExpireSeconds(expireSeconds);
                customThreadToRunAndSaveToRedis.setDescription(description);
                threadPoolTaskExecutor.execute(customThreadToRunAndSaveToRedis);
            }
            if(value.getClass().equals(Class.forName("com.gitee.fufu669.config.exception.CacheEmptyInstance"))){
                return null;
            }
            /*获取当前方法的返回的类名,然后如果是Long类型,碰着值是integer,要强转一哈,否则会报错。*/
            Signature signature = proceedingJoinPoint.getSignature();
            Class returnType = ((MethodSignature) signature).getReturnType();
            value = getValueFromReturnType(value, returnType);
            return value;
        } else {
            try {
                value = proceedingJoinPoint.proceed();
                if (value == null) {
                    Signature signature = proceedingJoinPoint.getSignature();
                    Class returnType = ((MethodSignature) signature).getReturnType();
                    if (returnType.equals(Class.forName("java.util.List"))) {
                        cacheService.setValue(cacheKey, new ArrayList<>());
                        cacheService.expire(cacheKey, expireSeconds);
                    } else if (returnType.equals(Class.forName("java.util.Map"))) {
                        cacheService.setValue(cacheKey, new HashMap());
                        cacheService.expire(cacheKey, expireSeconds);
                    }else {
                        if(returnType.isInterface()){
                            cacheService.setValue(cacheKey, new CacheEmptyInstance());
                            cacheService.expire(cacheKey, expireSeconds);
                        }else {
                            cacheService.setValue(cacheKey, returnType.newInstance());
                            cacheService.expire(cacheKey, expireSeconds);
                        }
                    }
                } else {
                    cacheService.setValue(cacheKey, value);
                    cacheService.expire(cacheKey,expireSeconds);
                }
                return value;
            } catch (Throwable throwable) {
                logger.info(throwable.getMessage(), throwable);
                return value;
            }
        }
    }

    public static Object getValueFromReturnType(Object value, Class returnType) throws ClassNotFoundException {
        if (returnType.equals(Class.forName("java.lang.Long"))) {
            value = Long.valueOf("" + value);
        } else if (returnType.equals(Class.forName("java.lang.Double"))) {
            value = Double.valueOf("" + value);
        } else if (returnType.equals(Class.forName("java.lang.Float"))) {
            value = Float.valueOf("" + value);
        } else if (returnType.equals(Class.forName("java.lang.Byte"))) {
            value = Byte.valueOf("" + value);
        } else if (returnType.equals(Class.forName("java.lang.Short"))) {
            value = Short.valueOf("" + value);
        }
        return value;
    }

    public class CustomThreadToRunAndSaveToRedis implements Runnable {
        private ProceedingJoinPoint proceedingJoinPoint;
        private int expireSeconds = CacheKeyCommon.CACHESERVICE_REDIS_DEFAULT_EXPIRED_SECONDS_1_WEEK;
        private String description = "";

        public void setProceedingJoinPoint(ProceedingJoinPoint proceedingJoinPoint) {
            this.proceedingJoinPoint = proceedingJoinPoint;
        }

        public void setExpireSeconds(int expireSeconds) {
            this.expireSeconds = expireSeconds;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public void run() {
            String targetName = proceedingJoinPoint.getTarget().getClass().getName();
            String methodName = proceedingJoinPoint.getSignature().getName();
            logger.info("%%%%%%CacheFetchLaterAop:multiThread:☆☆☆start☆☆☆:Classname:"+ CacheHtmlUtil.getLastStringSplitHtml(targetName,"\\.")+":methodName:"+methodName+":description:"+description+"%%%%%%");
            String cacheKey = null;
            try {
                Object value = null;
                try {
                    value = proceedingJoinPoint.proceed();
                } catch (Throwable throwable) {
                    logger.info(throwable.getMessage(), throwable);
                    return;
                }
                cacheKey = CacheAopUtil.getCacheKey(proceedingJoinPoint);
                if (value == null) {
                    Signature signature = proceedingJoinPoint.getSignature();
                    Class returnType = ((MethodSignature) signature).getReturnType();
                    try {
                        cacheService.setValue(cacheKey, returnType.newInstance());
                        cacheService.expire(cacheKey,expireSeconds);
                    } catch (Exception e) {
                        logger.info(e.getMessage(), e);
                        cacheService.setValue(cacheKey, new CacheEmptyInstance());
                        cacheService.expire(cacheKey,expireSeconds);
                    }
                } else {
                    cacheService.setValue(cacheKey, value);
                    cacheService.expire(cacheKey,expireSeconds);
                }
            } catch (Exception e) {
                logger.info(e.getMessage(), e);
            } finally {
                cacheService.unlock(CacheKeyCommon.LOCK + cacheKey);
                logger.info("%%%%%%CacheFetchLaterAop:multiThread:★★★end★★★:Classname:"+CacheHtmlUtil.getLastStringSplitHtml(targetName,"\\.")+":methodName:"+methodName+":description:" + description + "%%%%%%");
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy