1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| package org.jeecg.modules.mock.aspect;
import cn.hutool.core.util.StrUtil; import lombok.extern.slf4j.Slf4j; import org.apache.shiro.SecurityUtils; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.jeecg.common.constant.CacheConstant; import org.jeecg.common.system.vo.LoginUser; import org.jeecg.common.util.RedisUtil; import org.jeecg.modules.basic.base.CustomDataConstant; import org.jeecg.modules.mock.aspect.anno.CustomCache; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest; import java.lang.reflect.Method; import java.util.Arrays; import java.util.stream.Collectors;
@Aspect @Component @Slf4j public class CustomCacheAspect { @Autowired private RedisUtil redisUtil;
@Pointcut("@annotation(org.jeecg.modules.mock.aspect.anno.CustomCache)") public void logPointCut() {
}
public static String getCacheKey(String key, String[] params, HttpServletRequest request) { if(params.length == 0) { return key; } String paramValues = Arrays.asList(params).stream().map(e -> { String parameterValue = request.getParameter(e); if(StrUtil.isEmpty(parameterValue) && CustomDataConstant.HTTP_PARAMETER_ORGCODE.equals(e)) { LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal(); return sysUser.getDataOrgCode(); } else { return parameterValue; } }).filter(e->StrUtil.isNotBlank(e)).collect(Collectors.joining(""));
if(StrUtil.isNotBlank(paramValues)) { return key + ":" + paramValues.hashCode(); } else { return key; } }
@Around("logPointCut()") public Object around(ProceedingJoinPoint point) throws Throwable { Object resultData = getDataFromCache(point); if(resultData != null) { return resultData; }
long beginTime = System.currentTimeMillis(); Object result = point.proceed(); long time = System.currentTimeMillis() - beginTime; log.info("耗时:{} 毫秒", time);
saveDataInCache(point, result);
return result; }
private Object getDataFromCache(ProceedingJoinPoint joinPoint) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod();
CustomCache annotation = method.getAnnotation(CustomCache.class); if(annotation != null){ String key = annotation.key(); String[] params = annotation.params();
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest(); String cacheKey = getCacheKey(key, params, request); return redisUtil.get(CacheConstant.CUSTOM_CACHE + ":" + cacheKey); } return null; }
private void saveDataInCache(ProceedingJoinPoint joinPoint, Object obj) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod();
CustomCache annotation = method.getAnnotation(CustomCache.class); if(annotation != null){ String key = annotation.key(); String[] params = annotation.params(); int timeout = annotation.timeout();
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest(); String cacheKey = getCacheKey(key, params, request); if(timeout > 0) { redisUtil.set(CacheConstant.CUSTOM_CACHE + ":" + cacheKey, obj, timeout); } else { redisUtil.set(CacheConstant.CUSTOM_CACHE + ":" + cacheKey, obj); } } } }
|