shikeyin
2024-01-11 65da8373531677b1c37a98f53eaa30c892f35e5a
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
package com.iplatform.base.controller;
 
import com.iplatform.base.SystemController;
import com.iplatform.base.VariableConstants;
import com.iplatform.base.config.CacheProperties;
import com.iplatform.base.util.cache.CacheInfo;
import com.walker.cache.CacheProvider;
import com.walker.cache.SimpleCacheManager;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.web.ResponseValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
 
@RestController
@RequestMapping("/monitor/cache")
@ConditionalOnProperty(prefix = "iplatform.cache", name = "redis-enabled", havingValue = "true", matchIfMissing = false)
public class CacheController extends SystemController {
 
    private RedisTemplate<String, Object> redisTemplate;
    private CacheProperties cacheProperties;
 
    @Autowired(required = false)
    public CacheController(RedisTemplate<String, Object> redisTemplate, CacheProperties cacheProperties){
        this.redisTemplate = redisTemplate;
        this.cacheProperties = cacheProperties;
    }
 
    @RequestMapping("/clearCacheName")
    public ResponseValue clearCacheName(String name){
        if(StringUtils.isEmpty(name)){
            return ResponseValue.error("必须指定缓存名称");
        }
        CacheProvider<?> cacheProvider = SimpleCacheManager.getCacheProvider(name);
        if(cacheProvider == null){
            return ResponseValue.error("缓存对象不存在");
        }
        try {
            cacheProvider.reload();
            return ResponseValue.success("缓存重新加载成功");
        } catch (Exception e) {
            logger.error("重新加载缓存失败:" + e.getMessage(), e);
            return ResponseValue.error("重新加载缓存失败:" + name);
        }
    }
 
    /**
     * 清空 系统缓存,该功能主要对 Redis 缓存方式,通过手动触发重构缓存,在开发阶段使用。
     * @return
     * @date 2023-08-26
     */
    @RequestMapping("/clearCacheAll")
    public ResponseValue reloadAllCacheProvider(){
        if(!this.cacheProperties.isRedisRebuild()){
            return ResponseValue.error("未开启'重构缓存'选项");
        }
 
        // 2023-08-26 开启了重建redis缓存选项
        Collection<CacheProvider<?>> cacheList = SimpleCacheManager.getCacheProviders();
        if(cacheList != null && cacheList.size() > 0){
            String name = null;
            try{
                for(CacheProvider<?> cacheProvider : cacheList){
                    name = cacheProvider.getProviderName();
                    cacheProvider.reload();
                }
            } catch (Exception ex){
                logger.error("重新加载所有缓存,出现异常:" + ex.getMessage() + ", name=" + name, ex);
                return ResponseValue.error("重新加载缓存,出现错误,name = " + name);
            }
        }
        return ResponseValue.success();
    }
 
    @RequestMapping("/getInfo")
    public ResponseValue getInfo(){
        if(this.redisTemplate == null){
            return ResponseValue.error("系统未开启Redis方式缓存");
        }
 
        Properties info = (Properties) redisTemplate.execute((RedisCallback<Object>) connection -> connection.info());
//        Properties commandStats = (Properties) redisTemplate.execute((RedisCallback<Object>) connection -> connection.info("commandstats"));
        Object dbSize = redisTemplate.execute((RedisCallback<Object>) connection -> connection.dbSize());
 
        Map<String, Object> result = new HashMap<>(3);
        result.put("info", info);
        result.put("dbSize", dbSize);
        result.put("commandStats", new ArrayList<>());
 
        return ResponseValue.success(result);
    }
 
    /**
     * 返回缓存对象名称列表,即:系统所有缓存定义名称集合
     * @return
     * @date 2023-01-04
     */
    @GetMapping("/select/getNames")
    public ResponseValue getCacheNames(){
        List<CacheInfo> cacheNames = new ArrayList<>();
        Collection<CacheProvider<?>> cacheList = SimpleCacheManager.getCacheProviders();
        if(cacheList != null && cacheList.size() > 0){
            for(CacheProvider<?> cacheProvider : cacheList){
                cacheNames.add(new CacheInfo(cacheProvider.getProviderName(), cacheProvider.getProviderType().getName()));
            }
        }
        return ResponseValue.success(cacheNames);
    }
 
    @GetMapping("/select/getKeys/{cacheName}")
    public ResponseValue getCacheKeys(@PathVariable String cacheName){
        if(StringUtils.isEmpty(cacheName)){
            return ResponseValue.error("参数错误");
        }
        List<String> keyList = new ArrayList<>((int) VariableConstants.MAX_CACHE_SHOW);
        CacheProvider<?> cacheProvider = SimpleCacheManager.getCacheProvider(cacheName);
        if(cacheProvider != null){
            if(cacheProvider.getCache().getPersistentSize() <= VariableConstants.MAX_CACHE_SHOW){
//                Cachable cachable = null;
//                for(Iterator<Cachable> it = cacheProvider.getCache().getIterator(); it.hasNext();){
//                    cachable = it.next();
//                    keyList.add(cachable.getKey());
//                }
                return ResponseValue.success(cacheProvider.getCache().getKeys());
            } else {
                return ResponseValue.error("缓存数据量超过 " + VariableConstants.MAX_CACHE_SHOW + ", 暂无法显示");
            }
        }
        return ResponseValue.success(keyList);
    }
 
    @GetMapping("/select/getValue/{cacheName}/{cacheKey}")
    public ResponseValue getOneCacheValue(@PathVariable String cacheName, @PathVariable String cacheKey){
        CacheProvider<?> cacheProvider = SimpleCacheManager.getCacheProvider(cacheName);
        if(cacheProvider != null){
            Object value = cacheProvider.getCacheData(cacheKey);
            CacheInfo cacheInfo = new CacheInfo(cacheName, cacheKey, value.toString());
            return ResponseValue.success(cacheInfo);
        }
        return ResponseValue.success();
    }
}