地质所 沉降监测网建设项目
chenhuan
2024-05-16 0fdd42e318f51f9e3c6581473416af1cca69877f
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
package com.javaweb.redis.service;
 
 
import com.javaweb.common.core.domain.AjaxResult;
import com.javaweb.common.core.page.TableDataInfo;
import com.javaweb.common.utils.StringUtils;
import com.javaweb.redis.util.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Service
public class RedisCacheMonitorService {
    protected Logger logger = LoggerFactory.getLogger(getClass());
 
    public Object removeCacheByKey(String key){
        if(RedisUtil.exists(key)){
            RedisUtil.del(key);
            return AjaxResult.success("清除缓存成功!");
        }
        return AjaxResult.error("不存在key为["+key+"]的缓存!");
    }
    public Map<String,Object> viewCacheInfoByKey(String key){
        Map<String,Object> res=new HashMap();
        if(RedisUtil.exists(key)){
            res.put("key",key);
            String value= RedisUtil.getStringValue(key);
            res.put("value",value);
        }
        return res;
    }
    /**
     * 获取缓存分页信息
     * @param map
     * @return
     */
    public Object listPg(Map<String, Object> map) {
        try {
            String keyContent = String.valueOf(map.get("keyContent"));
 
            String page_str = (String) map.get("pageNum");//当前页
            String limit_str = (String) map.get("pageSize");//每页几条数据
            page_str = StringUtils.assertNotNullOrEmpty(page_str, "1");
            limit_str = StringUtils.assertNotNullOrEmpty(limit_str, "10");
            int page = Integer.parseInt(page_str);
            int limit = Integer.parseInt(limit_str);
            int start = page - 1;
            if (page == 1 || page == 0) {
                start = 0;
            }
            if (StringUtils.isEmpty(keyContent)) {
                keyContent = "*";
            } else {
                keyContent = "*" + keyContent + "*";
            }
            List<String> keysList = RedisUtil.keys(keyContent);
            int keysCount = keysList.size();
            List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
            Map<String, Object> temp = null;
            for (int i = start * limit; i < keysCount && i < (start + 1) * limit; i++) {
                temp = new HashMap<>();
                String key = keysList.get(i);
                String type = RedisUtil.type(key);
                long ttl = RedisUtil.ttl(key);
                temp.put("key", key);
                temp.put("type", type);
                temp.put("ttl", ttl);
                mapList.add(temp);
            }
            TableDataInfo tableDataInfo=new TableDataInfo();
            tableDataInfo.setTotal(keysCount);
            tableDataInfo.setRows(mapList);
            tableDataInfo.setCode(0);
            tableDataInfo.setMsg(0);
 
            return tableDataInfo;
        }catch (Exception ex){
            ex.printStackTrace();
            return AjaxResult.error("获取缓存分页信息失败!");
        }
    }
 
 
}