| 3273 |
vikas |
1 |
/**
|
|
|
2 |
*
|
|
|
3 |
*/
|
|
|
4 |
package in.shop2020.serving.controllers;
|
|
|
5 |
|
|
|
6 |
import java.util.ArrayList;
|
|
|
7 |
import java.util.HashMap;
|
|
|
8 |
import java.util.List;
|
|
|
9 |
import java.util.Map;
|
|
|
10 |
|
|
|
11 |
import net.sf.ehcache.Cache;
|
|
|
12 |
import net.sf.ehcache.CacheManager;
|
|
|
13 |
import net.sf.ehcache.Statistics;
|
|
|
14 |
|
|
|
15 |
import org.apache.log4j.Logger;
|
|
|
16 |
import org.apache.struts2.convention.annotation.InterceptorRef;
|
|
|
17 |
import org.apache.struts2.convention.annotation.InterceptorRefs;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* @author vikas
|
|
|
21 |
*
|
|
|
22 |
*/
|
|
|
23 |
@InterceptorRefs({
|
|
|
24 |
@InterceptorRef("defaultStack")
|
|
|
25 |
})
|
|
|
26 |
public class CacheAdminController {
|
|
|
27 |
|
|
|
28 |
private static final long serialVersionUID = -6730428402950686583L;
|
|
|
29 |
|
|
|
30 |
public static final String NAME_KEY = "name";
|
|
|
31 |
public static final String DISK_STORE_KEY = "disk_store_size";
|
|
|
32 |
public static final String EVICTION_POLICY_KEY = "eviction_policy";
|
|
|
33 |
public static final String MEMORY_STORE_KEY = "memory_store_size";
|
|
|
34 |
public static final String OFF_HEAP_STORE_KEY = "off_heap_store_size";
|
|
|
35 |
public static final String SEARCHES_PER_SEC_KEY = "searches_per_second";
|
|
|
36 |
public static final String STATUS_KEY = "status";
|
|
|
37 |
public static final String AVG_GET_TIME_KEY = "avg_get_time";
|
|
|
38 |
public static final String AVG_SEARCH_TIME_KEY = "avg_search_time";
|
|
|
39 |
public static final String CACHE_HITS_KEY = "cache_hits";
|
|
|
40 |
public static final String CACHE_MISSES_KEY = "cache_misses";
|
| 3347 |
vikas |
41 |
public static final String CACHE_HIT_RATIO_KEY = "cache_hit_ratio";
|
| 3273 |
vikas |
42 |
public static final String EVICTION_COUNT_KEY = "eviction_count";
|
|
|
43 |
|
|
|
44 |
private static Logger log = Logger.getLogger(CacheAdminController.class);
|
|
|
45 |
|
|
|
46 |
private List<Map<String, String>> cacheStats;
|
|
|
47 |
private String cacheName;
|
|
|
48 |
|
|
|
49 |
public String index() {
|
|
|
50 |
CacheManager cacheManager = CacheManager.create();
|
|
|
51 |
String[] cacheNames = cacheManager.getCacheNames();
|
|
|
52 |
cacheStats = new ArrayList<Map<String,String>>();
|
|
|
53 |
for (String cacheName : cacheNames) {
|
|
|
54 |
Map<String, String> cacheStat = new HashMap<String, String>();
|
|
|
55 |
Cache cache = cacheManager.getCache(cacheName);
|
|
|
56 |
cacheStat.put(NAME_KEY, cache.getName());
|
|
|
57 |
cacheStat.put(EVICTION_POLICY_KEY, cache.getMemoryStoreEvictionPolicy().getName());
|
|
|
58 |
cacheStat.put(MEMORY_STORE_KEY, Long.toString(cache.getMemoryStoreSize()));
|
|
|
59 |
cacheStat.put(OFF_HEAP_STORE_KEY, Long.toString(cache.getOffHeapStoreSize()));
|
|
|
60 |
cacheStat.put(DISK_STORE_KEY, Integer.toString(cache.getDiskStoreSize()));
|
|
|
61 |
cacheStat.put(STATUS_KEY, cache.getStatus().toString());
|
|
|
62 |
|
|
|
63 |
Statistics stat = cache.getStatistics();
|
| 3347 |
vikas |
64 |
Long hits = stat.getCacheHits();
|
|
|
65 |
Long misses = stat.getCacheMisses();
|
|
|
66 |
Double hitratio = 0d;
|
|
|
67 |
if (hits > 0) {
|
|
|
68 |
hitratio = ((double)hits)/(hits + misses)*100;
|
|
|
69 |
}
|
| 3273 |
vikas |
70 |
cacheStat.put(AVG_GET_TIME_KEY, Float.toString(stat.getAverageGetTime()));
|
|
|
71 |
cacheStat.put(AVG_SEARCH_TIME_KEY, Float.toString(stat.getAverageSearchTime()));
|
| 3347 |
vikas |
72 |
cacheStat.put(CACHE_HITS_KEY, Long.toString(hits));
|
|
|
73 |
cacheStat.put(CACHE_MISSES_KEY, Long.toString(misses));
|
|
|
74 |
cacheStat.put(CACHE_HIT_RATIO_KEY, Long.toString(hitratio.longValue()));
|
| 3273 |
vikas |
75 |
cacheStat.put(EVICTION_COUNT_KEY, Long.toString(stat.getEvictionCount()));
|
| 3297 |
vikas |
76 |
cacheStat.put(SEARCHES_PER_SEC_KEY, Long.toString(stat.getSearchesPerSecond()));
|
| 3273 |
vikas |
77 |
cacheStats.add(cacheStat);
|
|
|
78 |
log.info(stat);
|
|
|
79 |
}
|
|
|
80 |
return "index";
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
public void destroy() {
|
|
|
84 |
CacheManager cacheManager = CacheManager.create();
|
|
|
85 |
for (String name : cacheManager.getCacheNames()) {
|
|
|
86 |
if (name.equals(cacheName)) {
|
|
|
87 |
cacheManager.getCache(name).removeAll();
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
}
|
|
|
91 |
public List<Map<String, String>> getCacheStats() {
|
|
|
92 |
return cacheStats;
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
public void setId(String name) {
|
|
|
96 |
cacheName = name;
|
|
|
97 |
}
|
|
|
98 |
}
|