add
yj
2024-12-05 b9900893177c78fc559223521fe839aa21000017
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
package com.dobbinsoft.fw.support.aspect;
 
import com.dobbinsoft.fw.support.annotation.AspectCommonCache;
import com.dobbinsoft.fw.support.component.CacheComponent;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.List;
 
/**
 * Created with IntelliJ IDEA.
 * Description: 基本缓存
 * User: rize
 * Date: 2020/3/28
 * Time: 14:09
 */
@Aspect
@Component
public class RedisCommonCacheAspect {
 
    @Autowired
    private CacheComponent cacheComponent;
 
    @Pointcut("@annotation(com.dobbinsoft.fw.support.annotation.AspectCommonCache)")
    public void cachePointCut() {}
 
 
    @Around("cachePointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        AspectCommonCache annotation = signature.getMethod().getAnnotation(AspectCommonCache.class);
        int[] ints = annotation.argIndex();
        Object[] args = joinPoint.getArgs();
        String key = annotation.value();
        for (int i = 0; i < ints.length; i++) {
            if (i != 0) {
                key = key + ":" + args[ints[i]];
            } else {
                key = key + args[ints[i]];
            }
        }
 
        // 走缓存
        if (annotation.arrayClass() != Object.class) {
            List objList = cacheComponent.getObjList(key, annotation.arrayClass());
            if (objList != null) {
                return objList;
            }
        } else {
            Object obj = cacheComponent.getObj(key, signature.getReturnType());
            if (obj != null) {
                return obj;
            }
        }
 
        // 走方法
        Object proceed = joinPoint.proceed();
        if (annotation.second() > 0) {
            cacheComponent.putObj(key, proceed, annotation.second());
        } else {
            cacheComponent.putObj(key, proceed);
        }
        return proceed;
    }
 
}