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

alibaba / jetcache / #385

11 Oct 2023 03:46AM UTC coverage: 88.891% (+0.01%) from 88.878%
#385

push

areyouok
improve unit test stability

4745 of 5338 relevant lines covered (88.89%)

0.89 hits per line

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

93.01
/jetcache-core/src/main/java/com/alicp/jetcache/support/DefaultCacheMonitor.java
1
package com.alicp.jetcache.support;
2

3
import com.alicp.jetcache.CacheGetResult;
4
import com.alicp.jetcache.CacheMonitor;
5
import com.alicp.jetcache.CacheResult;
6
import com.alicp.jetcache.MultiGetResult;
7
import com.alicp.jetcache.event.CacheEvent;
8
import com.alicp.jetcache.event.CacheGetAllEvent;
9
import com.alicp.jetcache.event.CacheGetEvent;
10
import com.alicp.jetcache.event.CacheLoadAllEvent;
11
import com.alicp.jetcache.event.CacheLoadEvent;
12
import com.alicp.jetcache.event.CachePutAllEvent;
13
import com.alicp.jetcache.event.CachePutEvent;
14
import com.alicp.jetcache.event.CacheRemoveAllEvent;
15
import com.alicp.jetcache.event.CacheRemoveEvent;
16
import org.slf4j.Logger;
17
import org.slf4j.LoggerFactory;
18

19
import java.util.Map;
20
import java.util.Set;
21
import java.util.concurrent.locks.ReentrantLock;
22

23
/**
24
 * Created on 2016/10/27.
25
 *
26
 * @author <a href="mailto:areyouok@gmail.com">huangli</a>
27
 */
28
public class DefaultCacheMonitor implements CacheMonitor {
29

30
    private static final Logger logger = LoggerFactory.getLogger(DefaultCacheMonitor.class);
1✔
31

32
    private final ReentrantLock reentrantLock = new ReentrantLock();
1✔
33
    protected CacheStat cacheStat;
34
    private String cacheName;
35

36
    public DefaultCacheMonitor(String cacheName) {
1✔
37
        if (cacheName == null) {
1✔
38
            throw new NullPointerException();
×
39
        }
40
        this.cacheName = cacheName;
1✔
41
        resetStat();
1✔
42
    }
1✔
43

44
    public String getCacheName() {
45
        return cacheName;
×
46
    }
47

48
    public void resetStat() {
49
        reentrantLock.lock();
1✔
50
        try {
51
            cacheStat = new CacheStat();
1✔
52
            cacheStat.setStatStartTime(System.currentTimeMillis());
1✔
53
            cacheStat.setCacheName(cacheName);
1✔
54
        }finally {
55
            reentrantLock.unlock();
1✔
56
        }
57
    }
1✔
58

59
    public CacheStat getCacheStat() {
60
        reentrantLock.lock();
1✔
61
        try {
62
            CacheStat stat = cacheStat.clone();
1✔
63
            stat.setStatEndTime(System.currentTimeMillis());
1✔
64
            return stat;
1✔
65
        }finally {
66
            reentrantLock.unlock();
1✔
67
        }
68
    }
69

70
    @Override
71
    public void afterOperation(CacheEvent event) {
72
        reentrantLock.lock();
1✔
73
        try {
74
            if (event instanceof CacheGetEvent) {
1✔
75
                CacheGetEvent e = (CacheGetEvent) event;
1✔
76
                afterGet(e.getMillis(), e.getKey(), e.getResult());
1✔
77
            } else if (event instanceof CachePutEvent) {
1✔
78
                CachePutEvent e = (CachePutEvent) event;
1✔
79
                afterPut(e.getMillis(), e.getKey(), e.getValue(), e.getResult());
1✔
80
            } else if (event instanceof CacheRemoveEvent) {
1✔
81
                CacheRemoveEvent e = (CacheRemoveEvent) event;
1✔
82
                afterRemove(e.getMillis(), e.getKey(), e.getResult());
1✔
83
            } else if (event instanceof CacheLoadEvent) {
1✔
84
                CacheLoadEvent e = (CacheLoadEvent) event;
1✔
85
                afterLoad(e.getMillis(), e.getKey(), e.getLoadedValue(), e.isSuccess());
1✔
86
            } else if (event instanceof CacheGetAllEvent) {
1✔
87
                CacheGetAllEvent e = (CacheGetAllEvent) event;
1✔
88
                afterGetAll(e.getMillis(), e.getKeys(), e.getResult());
1✔
89
            } else if (event instanceof CacheLoadAllEvent) {
1✔
90
                CacheLoadAllEvent e = (CacheLoadAllEvent) event;
1✔
91
                afterLoadAll(e.getMillis(), e.getKeys(), e.getLoadedValue(), e.isSuccess());
1✔
92
            } else if (event instanceof CachePutAllEvent) {
1✔
93
                CachePutAllEvent e = (CachePutAllEvent) event;
1✔
94
                afterPutAll(e.getMillis(), e.getMap(), e.getResult());
1✔
95
            } else if (event instanceof CacheRemoveAllEvent) {
1✔
96
                CacheRemoveAllEvent e = (CacheRemoveAllEvent) event;
1✔
97
                afterRemoveAll(e.getMillis(), e.getKeys(), e.getResult());
1✔
98
            }
99
        }finally {
100
            reentrantLock.unlock();
1✔
101
        }
102
    }
1✔
103

104
    private void afterGet(long millis, Object key, CacheGetResult result) {
105
        cacheStat.minGetTime = Math.min(cacheStat.minGetTime, millis);
1✔
106
        cacheStat.maxGetTime = Math.max(cacheStat.maxGetTime, millis);
1✔
107
        cacheStat.getTimeSum += millis;
1✔
108
        cacheStat.getCount++;
1✔
109
        parseSingleGet(result);
1✔
110
    }
1✔
111

112
    private void parseSingleGet(CacheGetResult result) {
113
        switch (result.getResultCode()) {
1✔
114
            case SUCCESS:
115
                cacheStat.getHitCount++;
1✔
116
                break;
1✔
117
            case NOT_EXISTS:
118
                cacheStat.getMissCount++;
1✔
119
                break;
1✔
120
            case EXPIRED:
121
                cacheStat.getExpireCount++;
1✔
122
                break;
1✔
123
            case FAIL:
124
                cacheStat.getFailCount++;
1✔
125
                break;
1✔
126
            default:
127
                logger.warn("jetcache get return unexpected code: " + result.getResultCode());
×
128
        }
129
    }
1✔
130

131
    private void afterPut(long millis, Object key, Object value, CacheResult result) {
132
        cacheStat.minPutTime = Math.min(cacheStat.minPutTime, millis);
1✔
133
        cacheStat.maxPutTime = Math.max(cacheStat.maxPutTime, millis);
1✔
134
        cacheStat.putTimeSum += millis;
1✔
135
        cacheStat.putCount++;
1✔
136
        switch (result.getResultCode()) {
1✔
137
            case SUCCESS:
138
                cacheStat.putSuccessCount++;
1✔
139
                break;
1✔
140
            case FAIL:
141
            case PART_SUCCESS:
142
                cacheStat.putFailCount++;
1✔
143
                break;
1✔
144
            case EXISTS:
145
                break;
1✔
146
            default:
147
                logger.warn("jetcache PUT return unexpected code: " + result.getResultCode());
×
148
        }
149
    }
1✔
150

151
    private void afterRemove(long millis, Object key, CacheResult result) {
152
        cacheStat.minRemoveTime = Math.min(cacheStat.minRemoveTime, millis);
1✔
153
        cacheStat.maxRemoveTime = Math.max(cacheStat.maxRemoveTime, millis);
1✔
154
        cacheStat.removeTimeSum += millis;
1✔
155
        cacheStat.removeCount++;
1✔
156
        switch (result.getResultCode()) {
1✔
157
            case SUCCESS:
158
            case NOT_EXISTS:
159
                cacheStat.removeSuccessCount++;
1✔
160
                break;
1✔
161
            case FAIL:
162
            case PART_SUCCESS:
163
                cacheStat.removeFailCount++;
1✔
164
                break;
1✔
165
            default:
166
                logger.warn("jetcache REMOVE return unexpected code: " + result.getResultCode());
×
167
        }
168
    }
1✔
169

170
    private void afterLoad(long millis, Object key, Object loadedValue, boolean success) {
171
        cacheStat.minLoadTime = Math.min(cacheStat.minLoadTime, millis);
1✔
172
        cacheStat.maxLoadTime = Math.max(cacheStat.maxLoadTime, millis);
1✔
173
        cacheStat.loadTimeSum += millis;
1✔
174
        cacheStat.loadCount++;
1✔
175
        if (success) {
1✔
176
            cacheStat.loadSuccessCount++;
1✔
177
        } else {
178
            cacheStat.loadFailCount++;
1✔
179
        }
180
    }
1✔
181

182
    private void afterLoadAll(long millis, Set keys, Map loadedValue, boolean success) {
183
        if (keys == null) {
1✔
184
            return;
×
185
        }
186
        int count = keys.size();
1✔
187
        cacheStat.minLoadTime = Math.min(cacheStat.minLoadTime, millis);
1✔
188
        cacheStat.maxLoadTime = Math.max(cacheStat.maxLoadTime, millis);
1✔
189
        cacheStat.loadTimeSum += millis;
1✔
190
        cacheStat.loadCount += count;
1✔
191
        if (success) {
1✔
192
            cacheStat.loadSuccessCount += count;
1✔
193
        } else {
194
            cacheStat.loadFailCount += count;
×
195
        }
196
    }
1✔
197

198
    private void afterGetAll(long millis, Set keys, MultiGetResult result) {
199
        if (keys == null) {
1✔
200
            return;
1✔
201
        }
202
        int keyCount = keys.size();
1✔
203
        cacheStat.minGetTime = Math.min(cacheStat.minGetTime, millis);
1✔
204
        cacheStat.maxGetTime = Math.max(cacheStat.maxGetTime, millis);
1✔
205
        cacheStat.getTimeSum += millis;
1✔
206
        cacheStat.getCount += keyCount;
1✔
207
        Map resultValues = result.getValues();
1✔
208
        if (resultValues == null) {
1✔
209
            cacheStat.getFailCount += keyCount;
×
210
        } else {
211
            for (Object singleResult : resultValues.values()) {
1✔
212
                CacheGetResult r = ((CacheGetResult) singleResult);
1✔
213
                parseSingleGet(r);
1✔
214
            }
1✔
215
        }
216
    }
1✔
217

218
    private void afterRemoveAll(long millis, Set keys, CacheResult result) {
219
        if (keys == null) {
1✔
220
            return;
1✔
221
        }
222
        int keyCount = keys.size();
1✔
223
        cacheStat.minRemoveTime = Math.min(cacheStat.minRemoveTime, millis);
1✔
224
        cacheStat.maxRemoveTime = Math.max(cacheStat.maxRemoveTime, millis);
1✔
225
        cacheStat.removeTimeSum += millis;
1✔
226
        cacheStat.removeCount += keyCount;
1✔
227
        if (result.isSuccess()) {
1✔
228
            cacheStat.removeSuccessCount += keyCount;
1✔
229
        } else {
230
            cacheStat.removeFailCount += keyCount;
×
231
        }
232
    }
1✔
233

234
    private void afterPutAll(long millis, Map map, CacheResult result) {
235
        if (map == null) {
1✔
236
            return;
1✔
237
        }
238
        int keyCount = map.size();
1✔
239
        cacheStat.minPutTime = Math.min(cacheStat.minPutTime, millis);
1✔
240
        cacheStat.maxPutTime = Math.max(cacheStat.maxPutTime, millis);
1✔
241
        cacheStat.putTimeSum += millis;
1✔
242
        cacheStat.putCount += keyCount;
1✔
243
        if (result.isSuccess()) {
1✔
244
            cacheStat.putSuccessCount += keyCount;
1✔
245
        } else {
246
            cacheStat.putFailCount += keyCount;
×
247
        }
248
    }
1✔
249

250
}
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