admin
2021-03-18 b37275dba6b782bf3bb3817c4504f6cdef1bef7c
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
package com.yeshi.buwan.aspect;
 
import com.yeshi.buwan.util.RedisManager;
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.springframework.stereotype.Component;
import org.yeshi.utils.SPELExpressionUtil;
import org.yeshi.utils.StringUtil;
import org.yeshi.utils.annotation.RequestSerializableByKeyService;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.params.SetParams;
 
import javax.annotation.Resource;
import java.lang.reflect.Method;
 
@Component
@Aspect
public class RequestSerializableServiceAspect {
 
    @Resource
    private RedisManager redisManager;
 
 
    @Around("execution(public * com.yeshi.buwan.service.imp..*.*(..))")
    public Object requestSerializable(ProceedingJoinPoint joinPoint) throws Throwable {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method targetMethod = methodSignature.getMethod();
        String cacheKey = null;
        try {
            Method realMethod = joinPoint.getTarget().getClass().getDeclaredMethod(joinPoint.getSignature().getName(),
                    targetMethod.getParameterTypes());
           if (realMethod.isAnnotationPresent(RequestSerializableByKeyService.class)) {
                RequestSerializableByKeyService rs = realMethod.getAnnotation(RequestSerializableByKeyService.class);
                String key = rs.key();
                cacheKey =  SPELExpressionUtil.generateKeyBySpEL(key, joinPoint);
                try {// redis做原子性保护
                    if (!StringUtil.isNullOrEmpty(cacheKey)) {
                        cacheKey = joinPoint.getTarget().getClass().getName() + "." + targetMethod.getName() + "-"
                                + cacheKey;
                        cacheKey = "service-" + StringUtil.Md5(cacheKey);
                        // jiedis原子性做拦截
                        Jedis jedis = redisManager.getJedis();
                        try {
                            String result = null;
                            long startTime = System.currentTimeMillis();
                            // 等待响应
                            while (StringUtil.isNullOrEmpty(result)) {
                                result = jedis.set(cacheKey, "1", new SetParams().nx().ex(30));
                                if (StringUtil.isNullOrEmpty(result)) {
                                    try {
                                        Thread.sleep(50);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                    if (System.currentTimeMillis() - startTime > 1000 * 60L) {
                                        return null;
                                    }
                                }
                            }
 
                            if (!StringUtil.isNullOrEmpty(result)) {
                                try {
                                    return joinPoint.proceed();
                                } finally {
                                    jedis.del(cacheKey);
                                }
                            }
                        } finally {
                            jedis.close();
                        }
                    }
                } catch (Exception e) {// 原子性保护出错
                        return joinPoint.proceed();
                }
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        }
        return joinPoint.proceed();
    }
}