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

alibaba / jetcache / #416

14 Aug 2024 06:16AM UTC coverage: 88.808% (-0.004%) from 88.812%
#416

push

web-flow
upgrade docker-compose to v3 version (#912)

4761 of 5361 relevant lines covered (88.81%)

0.89 hits per line

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

93.2
/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 huangli
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
    private long epoch;
36

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

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

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

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

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

110
    private void afterGet(long millis, Object key, CacheGetResult result) {
111
        cacheStat.minGetTime = Math.min(cacheStat.minGetTime, millis);
1✔
112
        cacheStat.maxGetTime = Math.max(cacheStat.maxGetTime, millis);
1✔
113
        cacheStat.getTimeSum += millis;
1✔
114
        cacheStat.getCount++;
1✔
115
        parseSingleGet(result);
1✔
116
    }
1✔
117

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

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

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

176
    private void afterLoad(long millis, Object key, Object loadedValue, boolean success) {
177
        cacheStat.minLoadTime = Math.min(cacheStat.minLoadTime, millis);
1✔
178
        cacheStat.maxLoadTime = Math.max(cacheStat.maxLoadTime, millis);
1✔
179
        cacheStat.loadTimeSum += millis;
1✔
180
        cacheStat.loadCount++;
1✔
181
        if (success) {
1✔
182
            cacheStat.loadSuccessCount++;
1✔
183
        } else {
184
            cacheStat.loadFailCount++;
1✔
185
        }
186
    }
1✔
187

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

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

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

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

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

© 2025 Coveralls, Inc