完成后端移除Redis改造

This commit is contained in:
wkc
2026-03-28 10:59:34 +08:00
parent bc2582246b
commit 65fe3d4605
17 changed files with 640 additions and 391 deletions

View File

@@ -1,26 +1,26 @@
package com.ruoyi.web.controller.monitor;
import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.cache.InMemoryCacheStats;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysCache;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
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 com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysCache;
/**
* 缓存监控
@@ -31,8 +31,7 @@ import com.ruoyi.system.domain.SysCache;
@RequestMapping("/monitor/cache")
public class CacheController
{
@Autowired
private RedisTemplate<String, String> redisTemplate;
private final RedisCache redisCache;
private final static List<SysCache> caches = new ArrayList<SysCache>();
{
@@ -45,27 +44,34 @@ public class CacheController
caches.add(new SysCache(CacheConstants.PWD_ERR_CNT_KEY, "密码错误次数"));
}
@SuppressWarnings("deprecation")
public CacheController(RedisCache redisCache)
{
this.redisCache = redisCache;
}
@PreAuthorize("@ss.hasPermi('monitor:cache:list')")
@GetMapping()
public AjaxResult getInfo() throws Exception
public AjaxResult getInfo()
{
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());
InMemoryCacheStats stats = redisCache.getCacheStats();
Map<String, Object> info = new LinkedHashMap<>();
info.put("cache_type", stats.cacheType());
info.put("cache_mode", stats.mode());
info.put("key_size", stats.keySize());
info.put("hit_count", stats.hitCount());
info.put("miss_count", stats.missCount());
info.put("expired_count", stats.expiredCount());
info.put("write_count", stats.writeCount());
Map<String, Object> result = new HashMap<>(3);
result.put("info", info);
result.put("dbSize", dbSize);
result.put("dbSize", stats.keySize());
List<Map<String, String>> pieList = new ArrayList<>();
commandStats.stringPropertyNames().forEach(key -> {
Map<String, String> data = new HashMap<>(2);
String property = commandStats.getProperty(key);
data.put("name", StringUtils.removeStart(key, "cmdstat_"));
data.put("value", StringUtils.substringBetween(property, "calls=", ",usec"));
pieList.add(data);
});
pieList.add(statEntry("hit_count", stats.hitCount()));
pieList.add(statEntry("miss_count", stats.missCount()));
pieList.add(statEntry("expired_count", stats.expiredCount()));
pieList.add(statEntry("write_count", stats.writeCount()));
result.put("commandStats", pieList);
return AjaxResult.success(result);
}
@@ -81,16 +87,16 @@ public class CacheController
@GetMapping("/getKeys/{cacheName}")
public AjaxResult getCacheKeys(@PathVariable String cacheName)
{
Set<String> cacheKeys = redisTemplate.keys(cacheName + "*");
return AjaxResult.success(new TreeSet<>(cacheKeys));
Set<String> cacheKeys = new TreeSet<>(redisCache.keys(cacheName + "*"));
return AjaxResult.success(cacheKeys);
}
@PreAuthorize("@ss.hasPermi('monitor:cache:list')")
@GetMapping("/getValue/{cacheName}/{cacheKey}")
public AjaxResult getCacheValue(@PathVariable String cacheName, @PathVariable String cacheKey)
{
String cacheValue = redisTemplate.opsForValue().get(cacheKey);
SysCache sysCache = new SysCache(cacheName, cacheKey, cacheValue);
Object cacheValue = redisCache.getCacheObject(cacheKey);
SysCache sysCache = new SysCache(cacheName, cacheKey, cacheValueToString(cacheValue));
return AjaxResult.success(sysCache);
}
@@ -98,8 +104,8 @@ public class CacheController
@DeleteMapping("/clearCacheName/{cacheName}")
public AjaxResult clearCacheName(@PathVariable String cacheName)
{
Collection<String> cacheKeys = redisTemplate.keys(cacheName + "*");
redisTemplate.delete(cacheKeys);
Collection<String> cacheKeys = redisCache.keys(cacheName + "*");
redisCache.deleteObject(cacheKeys);
return AjaxResult.success();
}
@@ -107,7 +113,7 @@ public class CacheController
@DeleteMapping("/clearCacheKey/{cacheKey}")
public AjaxResult clearCacheKey(@PathVariable String cacheKey)
{
redisTemplate.delete(cacheKey);
redisCache.deleteObject(cacheKey);
return AjaxResult.success();
}
@@ -115,8 +121,28 @@ public class CacheController
@DeleteMapping("/clearCacheAll")
public AjaxResult clearCacheAll()
{
Collection<String> cacheKeys = redisTemplate.keys("*");
redisTemplate.delete(cacheKeys);
redisCache.clear();
return AjaxResult.success();
}
private Map<String, String> statEntry(String name, long value)
{
Map<String, String> data = new HashMap<>(2);
data.put("name", name);
data.put("value", String.valueOf(value));
return data;
}
private String cacheValueToString(Object cacheValue)
{
if (cacheValue == null)
{
return StringUtils.EMPTY;
}
if (cacheValue instanceof String stringValue)
{
return stringValue;
}
return JSON.toJSONString(cacheValue);
}
}

View File

@@ -78,28 +78,5 @@ spring:
wall:
config:
multi-statement-allow: true
data:
# redis 配置
redis:
# 地址
host: 116.62.17.81
# 端口默认为6379
port: 6379
# 数据库索引
database: 0
# 密码
password: Kfcx@1234
# 连接超时时间
timeout: 10s
lettuce:
pool:
# 连接池中的最小空闲连接
min-idle: 0
# 连接池中的最大空闲连接
max-idle: 8
# 连接池的最大数据库连接数
max-active: 8
# #连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1ms
model:
url: http://localhost:8080/rate/pricing/mock/invokeModel