地质所 沉降监测网建设项目
chenhuan
2024-05-16 f992b4e508b358eba4170b1e9b1bb21319f7a3cd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package com.javaweb.ehcache.service;
 
import com.alibaba.fastjson.JSON;
 
import com.javaweb.common.core.domain.AjaxResult;
import com.javaweb.common.core.page.TableDataInfo;
import com.javaweb.common.utils.StringUtils;
import com.javaweb.ehcache.util.EhCacheUtils;
import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
 
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @author wujiyue
 * @date 2015/10/30
 */
@Service
public class EhCacheMonitorService {
 
    private final String cacheValueKey = "key";
    private final String cacheNameKey = "cachename";
    private final String cacheSizeKey = "size";
    private final String cacheMsSizeKey = "memorystoresize";
    private final String cacheDsSizeKey = "diskstoresize";
    private final String cachesImKey = "sizeinmemory";
    private final String cacheExplainKey = "explain";
    //系统缓存
    public Map<String, Object> sysCacheState(){
        //获得系统缓存对象
        Cache cache = EhCacheUtils.getCacheManager().getCache(EhCacheUtils.SYS_CACHE);
        Map<String, Object> m = new HashMap<String, Object>();
        m.put(cacheNameKey, cache.getName());
        m.put(cacheSizeKey, Integer.valueOf(cache.getSize()));
        m.put(cacheMsSizeKey, Long.valueOf(cache.getMemoryStoreSize()));
        m.put(cacheDsSizeKey, Integer.valueOf(cache.getDiskStoreSize()));
        m.put(cachesImKey, Long.valueOf(cache.calculateInMemorySize()));
        return m;
    }
    //用户缓存
    public Map<String, Object> userCacheState(){
        //获得用户缓存对象
        Cache cache = EhCacheUtils.getCacheManager().getCache(EhCacheUtils.USER_CACHE);
 
        Map<String, Object> m = new HashMap<String, Object>();
        m.put(cacheNameKey, cache.getName());
        m.put(cacheSizeKey, Integer.valueOf(cache.getSize()));
        m.put(cacheMsSizeKey, Long.valueOf(cache.getMemoryStoreSize()));
        m.put(cacheDsSizeKey, Integer.valueOf(cache.getDiskStoreSize()));
        m.put(cachesImKey, Long.valueOf(cache.calculateInMemorySize()));
        return m;
    }
 
    /**
     * 获得特定key的系统缓存详情
     * @param map
     * @return
     */
    public Map<String, Object> getSysCacheByKey(Map<String, Object> map) {
        //获得系统缓存对象
        Cache cache = EhCacheUtils.getCacheManager().getCache(EhCacheUtils.SYS_CACHE);
        Map<String, Object> m = new HashMap<String, Object>();
        Object key = map.get(cacheValueKey);
        if (key==null||"".equals(key.toString())) {
            m.put(cacheExplainKey, "");
        } else {
            m.put(cacheValueKey, key.toString());
            Element element = cache.getQuiet(key.toString());
            if (element != null) {
                m.put(cacheExplainKey, element.toString());
            } else {
                m.put(cacheExplainKey, "");
            }
        }
        return m;
    }
    //获得指定key的缓存信息
    public Map<String, Object> viewCacheInfoByKey(String key){
        if(StringUtils.isNotEmpty(key)){
            Map<String, Object> res=getCacheInfoByKey(key,EhCacheUtils.SYS_CACHE);
            if(res!=null){
                return res;
            }else{
                res=getCacheInfoByKey(key,EhCacheUtils.USER_CACHE);
                if(res!=null){
                    return res;
                }else{
                    res=getCacheInfoByKey(key,EhCacheUtils.DEFAULT_CACHE);
                    if(res!=null){
                        return res;
                    }
                }
            }
        }
        return null;
    }
    private Map<String, Object> getCacheInfoByKey(String key,String cacheType){
        if(StringUtils.isEmpty(key)){
            return null;
        }
        Cache cache =null;
        if(cacheType.equals(EhCacheUtils.SYS_CACHE)){
            cache = EhCacheUtils.getCacheManager().getCache(EhCacheUtils.SYS_CACHE);
        }else if(cacheType.equals(EhCacheUtils.USER_CACHE)){
            cache = EhCacheUtils.getCacheManager().getCache(EhCacheUtils.USER_CACHE);
        }else{
            //return null;
            cache = EhCacheUtils.getCacheManager().getCache(EhCacheUtils.DEFAULT_CACHE);
        }
        List<String> keysList = cache.getKeys();
        keysList=keysList.stream().filter(s->{return s.contains(key);}).collect(Collectors.toList());
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        Map<String, Object> returnMap=new HashMap<String, Object>();
        if(CollectionUtils.isNotEmpty(keysList)){
            int  keysCount=keysList.size();
 
            for (int i = 0; i < keysCount; i++) {
                Element element = cache.getQuiet(keysList.get(i).toString());
 
                long l = element.getSerializedSize();
                Map<String, Object> elMap = new HashMap<String, Object>();
                elMap.put(cacheValueKey, keysList.get(i).toString());
                DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(element.getCreationTime());
                elMap.put("value",  JSON.toJSONString(element.getValue()));
                elMap.put("creattime", formatter.format(calendar.getTime()));
                calendar.setTimeInMillis(element.getLatestOfCreationAndUpdateTime());
                elMap.put("lastupdatetime", formatter.format(calendar.getTime()));
                calendar.setTimeInMillis(element.getLastAccessTime());
                elMap.put("lastaccesstime", formatter.format(calendar.getTime()));
                elMap.put("hittimes", Long.valueOf(element.getHitCount()));
 
                mapList.add(elMap);
            }
            returnMap.put("total",keysCount);
            returnMap.put("rows",mapList);
            return returnMap;
        }else{
            return null;
        }
    }
    public Object removeCacheByKey(String key){
 
        if(StringUtils.isNotEmpty(key)){
            Cache cache = EhCacheUtils.getCacheManager().getCache(EhCacheUtils.SYS_CACHE);
            Element element = cache.getQuiet(key);
            if(element!=null){
                cache.remove(key);
                return AjaxResult.success("清除缓存成功!");
            }else{
                Cache cacheUser = EhCacheUtils.getCacheManager().getCache(EhCacheUtils.USER_CACHE);
                Element elementUser = cacheUser.getQuiet(key);
                if(elementUser!=null){
                    cacheUser.remove(key);
                    return AjaxResult.success("清除缓存成功!");
                }else{
                    //return AjaxResult.error("不存在key="+key+"的缓存!");
                    Cache cacheDefault = EhCacheUtils.getCacheManager().getCache(EhCacheUtils.DEFAULT_CACHE);
                    Element elementDefault = cacheDefault.getQuiet(key);
                    if(elementDefault!=null){
                        cacheDefault.remove(key);
                        return AjaxResult.success("清除缓存成功!");
                    }else{
                        return AjaxResult.error("不存在key="+key+"的缓存!");
                    }
                }
            }
        }else{
            return AjaxResult.error("请指定要清除的缓存的key!");
        }
 
    }
    /**
     * 获得缓存详情 0系统缓存,1用户缓存
     * @param map
     * @return
     */
    public Object getCacheInfo(Map<String, Object> map) {
        //获得系统缓存对象
        String cachetype=String.valueOf(map.get("cachetype"));
        Cache cache =null;
        if("1".equals(cachetype)){
             cache = EhCacheUtils.getCacheManager().getCache(EhCacheUtils.USER_CACHE);
        }else if("0".equals(cachetype)){
            cache = EhCacheUtils.getCacheManager().getCache(EhCacheUtils.SYS_CACHE);
        }else{
           cache = EhCacheUtils.getCacheManager().getCache(EhCacheUtils.DEFAULT_CACHE);
        }
        String keyContent=String.valueOf(map.get("keyContent"));
        try {
            String pageStr=(String)map.get("pageNum");//当前页
            String limitStr=(String)map.get("pageSize");//每页几条数据
            pageStr= StringUtils.assertNotNullOrEmpty(pageStr,"1");
            limitStr= StringUtils.assertNotNullOrEmpty(limitStr,"10");
            int page=Integer.parseInt(pageStr);
            int limit = Integer.parseInt(limitStr);
            int start = page-1;
            if(page == 1 || page == 0){
                start = 0;
            }
            List<String> keysList = cache.getKeys();
 
            int keysCount=keysList.size();
            if(StringUtils.isNotEmpty(keyContent)&&!"null".equals(keyContent)){
                keysList=keysList.stream().filter(s->{return s.contains(keyContent);}).collect(Collectors.toList());
 
                keysCount=keysList.size();
            }
            long serializeLong = 0L;
            for (int i = 0; i < keysList.size(); i++) {
                Element e = cache.getQuiet(keysList.get(i).toString());
                serializeLong += e.getSerializedSize();
            }
            List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
            for (int i = start * limit; i < keysCount && i < (start+1) * limit; i++) {
                Element element = cache.getQuiet(keysList.get(i).toString());
                //JSONObject jsonValue=JSONObject.fromObject(element.getValue());
 
                long l = element.getSerializedSize();
                Map<String, Object> elMap = new HashMap<String, Object>();
                elMap.put(cacheValueKey, keysList.get(i).toString());
                DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(element.getCreationTime());
                elMap.put("value",  JSON.toJSONString(element.getValue()));
                elMap.put("creattime", formatter.format(calendar.getTime()));
                calendar.setTimeInMillis(element.getLatestOfCreationAndUpdateTime());
                elMap.put("lastupdatetime", formatter.format(calendar.getTime()));
                calendar.setTimeInMillis(element.getLastAccessTime());
                elMap.put("lastaccesstime", formatter.format(calendar.getTime()));
                elMap.put("hittimes", Long.valueOf(element.getHitCount()));
                if (serializeLong == 0L) {
                    elMap.put(cacheSizeKey, "0");
                    elMap.put("proprotion", "0%");
                } else {
                    double d = l * 100.0D / serializeLong;
                    elMap.put(cacheSizeKey, new DecimalFormat("####,###").format(l));
                    elMap.put("proprotion", new DecimalFormat("####.00").format(d) + "%");
                }
                mapList.add(elMap);
            }
 
            TableDataInfo tableDataInfo=new TableDataInfo();
            tableDataInfo.setTotal(keysList.size());
            tableDataInfo.setRows(mapList);
            tableDataInfo.setCode(0);
            tableDataInfo.setMsg(0);
 
            return tableDataInfo;
        }catch (Exception e) {
            return AjaxResult.error("获得系统缓存详情失败!");
        }
 
    }
}