• 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-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);
×
31

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

© 2025 Coveralls, Inc