• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

alibaba / jetcache / #405

16 Apr 2024 05:58AM UTC coverage: 0.0% (-88.9%) from 88.866%
#405

push

areyouok
add encoding to fix coverage report

0 of 5353 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/jetcache-anno/src/main/java/com/alicp/jetcache/anno/method/CacheConfigUtil.java
1
/**
2
 * Created on  13-09-20 22:01
3
 */
4
package com.alicp.jetcache.anno.method;
5

6
import com.alicp.jetcache.CacheConfigException;
7
import com.alicp.jetcache.RefreshPolicy;
8
import com.alicp.jetcache.anno.CacheConsts;
9
import com.alicp.jetcache.anno.CacheInvalidate;
10
import com.alicp.jetcache.anno.CacheInvalidateContainer;
11
import com.alicp.jetcache.anno.CachePenetrationProtect;
12
import com.alicp.jetcache.anno.CacheRefresh;
13
import com.alicp.jetcache.anno.CacheUpdate;
14
import com.alicp.jetcache.anno.Cached;
15
import com.alicp.jetcache.anno.EnableCache;
16
import com.alicp.jetcache.anno.support.CacheInvalidateAnnoConfig;
17
import com.alicp.jetcache.anno.support.CacheUpdateAnnoConfig;
18
import com.alicp.jetcache.anno.support.CachedAnnoConfig;
19
import com.alicp.jetcache.anno.support.PenetrationProtectConfig;
20

21
import java.lang.reflect.Method;
22
import java.time.Duration;
23
import java.util.ArrayList;
24
import java.util.List;
25
import java.util.concurrent.TimeUnit;
26

27
/**
28
 * @author huangli
29
 */
30
public class CacheConfigUtil {
×
31
    private static CachedAnnoConfig parseCached(Method m) {
32
        Cached anno = m.getAnnotation(Cached.class);
×
33
        if (anno == null) {
×
34
            return null;
×
35
        }
36
        CachedAnnoConfig cc = new CachedAnnoConfig();
×
37
        cc.setArea(anno.area());
×
38
        cc.setName(anno.name());
×
39
        cc.setCacheType(anno.cacheType());
×
40
        cc.setSyncLocal(anno.syncLocal());
×
41
        cc.setEnabled(anno.enabled());
×
42
        cc.setTimeUnit(anno.timeUnit());
×
43
        cc.setExpire(anno.expire());
×
44
        cc.setLocalExpire(anno.localExpire());
×
45
        cc.setLocalLimit(anno.localLimit());
×
46
        cc.setCacheNullValue(anno.cacheNullValue());
×
47
        cc.setCondition(anno.condition());
×
48
        cc.setPostCondition(anno.postCondition());
×
49
        cc.setSerialPolicy(anno.serialPolicy());
×
50
        cc.setKeyConvertor(anno.keyConvertor());
×
51
        cc.setKey(anno.key());
×
52
        cc.setDefineMethod(m);
×
53

54
        CacheRefresh cacheRefresh = m.getAnnotation(CacheRefresh.class);
×
55
        if (cacheRefresh != null) {
×
56
            RefreshPolicy policy = parseRefreshPolicy(cacheRefresh);
×
57
            cc.setRefreshPolicy(policy);
×
58
        }
59

60
        CachePenetrationProtect protectAnno = m.getAnnotation(CachePenetrationProtect.class);
×
61
        if (protectAnno != null) {
×
62
            PenetrationProtectConfig protectConfig = parsePenetrationProtectConfig(protectAnno);
×
63
            cc.setPenetrationProtectConfig(protectConfig);
×
64
        }
65

66
        return cc;
×
67
    }
68

69
    public static PenetrationProtectConfig parsePenetrationProtectConfig(CachePenetrationProtect protectAnno) {
70
        PenetrationProtectConfig protectConfig = new PenetrationProtectConfig();
×
71
        protectConfig.setPenetrationProtect(protectAnno.value());
×
72
        if (!CacheConsts.isUndefined(protectAnno.timeout())) {
×
73
            long timeout = protectAnno.timeUnit().toMillis(protectAnno.timeout());
×
74
            protectConfig.setPenetrationProtectTimeout(Duration.ofMillis(timeout));
×
75
        }
76
        return protectConfig;
×
77
    }
78

79
    public static RefreshPolicy parseRefreshPolicy(CacheRefresh cacheRefresh) {
80
        RefreshPolicy policy = new RefreshPolicy();
×
81
        TimeUnit t = cacheRefresh.timeUnit();
×
82
        policy.setRefreshMillis(t.toMillis(cacheRefresh.refresh()));
×
83
        if (!CacheConsts.isUndefined(cacheRefresh.stopRefreshAfterLastAccess())) {
×
84
            policy.setStopRefreshAfterLastAccessMillis(t.toMillis(cacheRefresh.stopRefreshAfterLastAccess()));
×
85
        }
86
        if (!CacheConsts.isUndefined(cacheRefresh.refreshLockTimeout())) {
×
87
            policy.setRefreshLockTimeoutMillis(t.toMillis(cacheRefresh.refreshLockTimeout()));
×
88
        }
89
        return policy;
×
90
    }
91

92
    public static List<CacheInvalidateAnnoConfig> parseCacheInvalidates(Method m) {
93
        List<CacheInvalidateAnnoConfig> annoList = null;
×
94
        CacheInvalidate ci = m.getAnnotation(CacheInvalidate.class);
×
95
        if (ci != null) {
×
96
            annoList = new ArrayList<>(1);
×
97
            annoList.add(createCacheInvalidateAnnoConfig(ci, m));
×
98
        } else {
99
            CacheInvalidateContainer cic = m.getAnnotation(CacheInvalidateContainer.class);
×
100
            if (cic != null) {
×
101
                CacheInvalidate[] cacheInvalidates = cic.value();
×
102
                annoList = new ArrayList<>(cacheInvalidates.length);
×
103
                for (CacheInvalidate cacheInvalidate : cacheInvalidates) {
×
104
                    annoList.add(createCacheInvalidateAnnoConfig(cacheInvalidate, m));
×
105
                }
106
            }
107
        }
108
        return annoList;
×
109
    }
110

111
    private static CacheInvalidateAnnoConfig createCacheInvalidateAnnoConfig(CacheInvalidate anno, Method m) {
112
        CacheInvalidateAnnoConfig cc = new CacheInvalidateAnnoConfig();
×
113
        cc.setArea(anno.area());
×
114
        cc.setName(anno.name());
×
115
        if (cc.getName() == null || cc.getName().trim().equals("")) {
×
116
            throw new CacheConfigException("name is required for @CacheInvalidate: " + m.getClass().getName() + "." + m.getName());
×
117
        }
118
        cc.setKey(anno.key());
×
119
        cc.setCondition(anno.condition());
×
120
        cc.setMulti(anno.multi());
×
121
        cc.setDefineMethod(m);
×
122
        return cc;
×
123
    }
124

125
    private static CacheUpdateAnnoConfig parseCacheUpdate(Method m) {
126
        CacheUpdate anno = m.getAnnotation(CacheUpdate.class);
×
127
        if (anno == null) {
×
128
            return null;
×
129
        }
130
        CacheUpdateAnnoConfig cc = new CacheUpdateAnnoConfig();
×
131
        cc.setArea(anno.area());
×
132
        cc.setName(anno.name());
×
133
        if (cc.getName() == null || cc.getName().trim().equals("")) {
×
134
            throw new CacheConfigException("name is required for @CacheUpdate: " + m.getClass().getName() + "." + m.getName());
×
135
        }
136
        cc.setKey(anno.key());
×
137
        cc.setValue(anno.value());
×
138
        if (cc.getValue() == null || cc.getValue().trim().equals("")) {
×
139
            throw new CacheConfigException("value is required for @CacheUpdate: " + m.getClass().getName() + "." + m.getName());
×
140
        }
141
        cc.setCondition(anno.condition());
×
142
        cc.setMulti(anno.multi());
×
143
        cc.setDefineMethod(m);
×
144
        return cc;
×
145
    }
146

147

148
    private static boolean parseEnableCache(Method m) {
149
        EnableCache anno = m.getAnnotation(EnableCache.class);
×
150
        return anno != null;
×
151
    }
152

153
    public static boolean parse(CacheInvokeConfig cac, Method method) {
154
        boolean hasAnnotation = false;
×
155
        CachedAnnoConfig cachedConfig = parseCached(method);
×
156
        if (cachedConfig != null) {
×
157
            cac.setCachedAnnoConfig(cachedConfig);
×
158
            hasAnnotation = true;
×
159
        }
160
        boolean enable = parseEnableCache(method);
×
161
        if (enable) {
×
162
            cac.setEnableCacheContext(true);
×
163
            hasAnnotation = true;
×
164
        }
165
        List<CacheInvalidateAnnoConfig> invalidateAnnoConfigs = parseCacheInvalidates(method);
×
166
        if (invalidateAnnoConfigs != null) {
×
167
            cac.setInvalidateAnnoConfigs(invalidateAnnoConfigs);
×
168
            hasAnnotation = true;
×
169
        }
170
        CacheUpdateAnnoConfig updateAnnoConfig = parseCacheUpdate(method);
×
171
        if (updateAnnoConfig != null) {
×
172
            cac.setUpdateAnnoConfig(updateAnnoConfig);
×
173
            hasAnnotation = true;
×
174
        }
175

176
        if (cachedConfig != null && (invalidateAnnoConfigs != null || updateAnnoConfig != null)) {
×
177
            throw new CacheConfigException("@Cached can't coexists with @CacheInvalidate or @CacheUpdate: " + method);
×
178
        }
179

180
        return hasAnnotation;
×
181
    }
182
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc