| ¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.doumee.api.Repeat; |
| | | |
| | | import com.doumee.core.constants.ResponseStatus; |
| | | import com.doumee.core.exception.BusinessException; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.aspectj.lang.ProceedingJoinPoint; |
| | | import org.aspectj.lang.annotation.Around; |
| | | import org.aspectj.lang.annotation.Aspect; |
| | | import org.aspectj.lang.reflect.MethodSignature; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.web.context.request.RequestContextHolder; |
| | | import org.springframework.web.context.request.ServletRequestAttributes; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.lang.reflect.Method; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | // å¼å¯æ¥å¿ï¼éè¦ä¾èµlombok |
| | | @Slf4j |
| | | // æä¸ä¸ªç±»å®ä¹ä¸ºåé¢ä¾å®¹å¨è¯»å |
| | | @Aspect |
| | | @Component |
| | | public class RepeatSubmitAspect { |
| | | |
| | | @Resource |
| | | private RedisTemplate<String, String> redisTemplate; |
| | | |
| | | // è¿æ¯ä¸ä¸ªç¯ç»éç¥ï¼å®ä¼å´ç»è¢« @RepeatSubmit 注解æ è®°çæ¹æ³æ§è¡,è¿éç repeatSubmit ä¸ä¸é¢çåæ°å¯¹åº |
| | | @Around("@annotation(repeatSubmit)") |
| | | public Object around(ProceedingJoinPoint point, RepeatSubmit repeatSubmit) throws Throwable { |
| | | |
| | | // è·åç¨æ·çtokenéªè¯,è¿é项ç®ç¨çæ¯ header éç Authorization åæ° |
| | | HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); |
| | | String requestToken = request.getHeader("token"); |
| | | |
| | | // è·å注解 |
| | | MethodSignature signature = (MethodSignature) point.getSignature(); |
| | | Method method = signature.getMethod(); |
| | | |
| | | // è·åç±»ï¼æ¹æ³ |
| | | String className = method.getDeclaringClass().getName(); |
| | | String methodName = method.getName(); |
| | | |
| | | // ç»è£
keyï¼ç¨æ·å¯ä¸æ è¯+æä½ç±»+æ¹æ³ |
| | | String key = requestToken + "#" + className + "#" + methodName; |
| | | String keyHashCode = String.valueOf(Math.abs(key.hashCode())); |
| | | log.info("key:{},keyHashcode:{}", key, keyHashCode); |
| | | |
| | | //è·åè¶
æ¶æ¶é´ |
| | | int timeOut = repeatSubmit.timeout(); |
| | | log.info("è¶
æ¶æ¶é´{}", timeOut); |
| | | |
| | | // ä»ç¼åç»ä¸æ ¹æ®keyè·åæ°æ® |
| | | String value = redisTemplate.opsForValue().get(keyHashCode); |
| | | |
| | | if (value != null) { |
| | | log.info("éå¤æäº¤"); |
| | | // 妿valueä¸ä¸ºç©º; return "请å¿éå¤æäº¤"; |
| | | throw new BusinessException(ResponseStatus.MASSIVE_REQUEST); |
| | | |
| | | } else { |
| | | log.info("馿¬¡æäº¤"); |
| | | // value为空ï¼åå å
¥ç¼åï¼å¹¶è®¾ç½®è¿æè¿ææ¶é´ |
| | | redisTemplate.opsForValue().set(keyHashCode, "1", timeOut, TimeUnit.MILLISECONDS); |
| | | } |
| | | |
| | | //æ§è¡Object |
| | | Object object = point.proceed(); |
| | | |
| | | return object; |
| | | } |
| | | } |