From b7839ab7290479d4fe6fa27d3da2dceb2dd81b49 Mon Sep 17 00:00:00 2001 From: dengwc Date: Fri, 22 Mar 2024 17:26:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=8E=A5=E5=8F=A3=E8=B0=83?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../login/strategy/FeishuLoginStrategy.java | 6 -- .../config/LogConfig/LogService.java | 18 +++--- .../config/LogConfig/RedisCache.java | 56 +++++++++++++++++++ .../LogConfig/RequestLoggingInterceptor.java | 10 +--- 4 files changed, 68 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/chint/application/services/login/strategy/FeishuLoginStrategy.java b/src/main/java/com/chint/application/services/login/strategy/FeishuLoginStrategy.java index a2952d9b..ede75dec 100644 --- a/src/main/java/com/chint/application/services/login/strategy/FeishuLoginStrategy.java +++ b/src/main/java/com/chint/application/services/login/strategy/FeishuLoginStrategy.java @@ -35,12 +35,6 @@ public class FeishuLoginStrategy implements LoginStrategy { @Value("${feishu.appSecret}") private String appSecret; - @Value("${feishu.ANAppId}") - private String ANAppId; - - @Value("${feishu.ANAppSecret}") - private String ANAppSecret; - @Override public Optional getAccessToken(String code) { diff --git a/src/main/java/com/chint/infrastructure/config/LogConfig/LogService.java b/src/main/java/com/chint/infrastructure/config/LogConfig/LogService.java index c925f596..10050839 100644 --- a/src/main/java/com/chint/infrastructure/config/LogConfig/LogService.java +++ b/src/main/java/com/chint/infrastructure/config/LogConfig/LogService.java @@ -46,12 +46,12 @@ public class LogService { public void batchSaveLog() { while (processedLogCount < 5000) { //获取日志数量数量 - long logNums = redisCache.getHashSize("SystemLog"); - if (logNums < 100L) { + int logNums = redisCache.getKeyCountWithPrefix("SystemLog:"); + if (logNums < 100) { break;//少于100条结束循环 } // 获取100条日志数据 - Map systemLogMap = redisCache.getCacheHashValues("SystemLog", 100); + Map systemLogMap = redisCache.getDataWithPrefix("SystemLog:", 100); Gson gson = new Gson(); // 转换日志数据并保存到数据库 List systemLogs = systemLogMap.values().stream() @@ -59,7 +59,7 @@ public class LogService { .toList(); jdbcSystemLogRepository.saveAll(systemLogs); // 删除已处理的日志数据 - redisCache.delCacheMapValue("SystemLog", systemLogMap.keySet()); + long l = redisCache.deleteObject(systemLogMap.keySet()); // 更新已处理日志数量 processedLogCount += systemLogs.size(); } @@ -103,18 +103,18 @@ public class LogService { } /** - * 保存redis可能剩余的日志信息 + * 保存redis中的日志信息 */ @Transactional public void saveLogs() { int count = 0; // 初始化计数器 while (count <= 10000) { //当计数器小于10000时执行循环 - long logNums = redisCache.getHashSize("SystemLog"); - if (logNums <= 0L) { + int logNums = redisCache.getKeyCountWithPrefix("SystemLog:"); + if (logNums <= 0) { break;//没有数据结束循环 } //获取日志信息 - Map systemLogMap = redisCache.getCacheHashValues("SystemLog", 100); + Map systemLogMap = redisCache.getDataWithPrefix("SystemLog:", 100); if (systemLogMap.isEmpty()) { break; // 如果没有数据可取,跳出循环 } @@ -125,7 +125,7 @@ public class LogService { ).toList(); jdbcSystemLogRepository.saveAll(systemLogs); // 删除已处理的日志数据 - redisCache.delCacheMapValue("SystemLog", systemLogMap.keySet()); + long l = redisCache.deleteObject(systemLogMap.keySet()); count++; } } diff --git a/src/main/java/com/chint/infrastructure/config/LogConfig/RedisCache.java b/src/main/java/com/chint/infrastructure/config/LogConfig/RedisCache.java index f21b1f9e..e2ff1be4 100644 --- a/src/main/java/com/chint/infrastructure/config/LogConfig/RedisCache.java +++ b/src/main/java/com/chint/infrastructure/config/LogConfig/RedisCache.java @@ -1,10 +1,12 @@ package com.chint.infrastructure.config.LogConfig; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.connection.DataType; import org.springframework.data.redis.core.*; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; +import java.nio.charset.StandardCharsets; import java.util.*; import java.util.concurrent.TimeUnit; @@ -268,4 +270,58 @@ public class RedisCache { public Collection keys(final String pattern) { return redisTemplate.keys(pattern); } + + /** + * 获取指定数量key前缀相同的数据 + * @param prefix key前缀 + * @param count 个数 + * @return key 和value + */ + public Map getDataWithPrefix(String prefix, int count) { + ScanOptions options = ScanOptions.scanOptions().match(prefix + "*").count(count).build(); + Cursor cursor = (Cursor) redisTemplate.execute((RedisCallback>) connection -> connection.scan(options)); + Map result = new HashMap<>(); + try { + if (cursor != null) { + int limit = 0; + while (cursor.hasNext() && limit < count) { + byte[] keyBytes = cursor.next(); + String key = new String(keyBytes, StandardCharsets.UTF_8); + + // 判断键的类型是否为 String,防止是其它类型是报错 + DataType dataType = redisTemplate.type(key); + if (dataType == DataType.STRING) { + Object value = redisTemplate.opsForValue().get(key); + result.put(key, value); + limit++; + } + } + } + } finally { + if (cursor != null) { + cursor.close(); + } + } + return result; + } + + + /** + * 获取指定数量key前缀相同的数量 + * @param prefix key前缀 + * @return 数量 + */ + public Integer getKeyCountWithPrefix(String prefix) { + ScanOptions options = ScanOptions.scanOptions().match(prefix + "*").build(); + Integer count = 0; + try (Cursor cursor = (Cursor) redisTemplate.execute((RedisCallback>) connection -> connection.scan(options))) { + if (cursor != null) { + while (cursor.hasNext()) { + cursor.next(); + count++; + } + } + } + return count; + } } \ No newline at end of file diff --git a/src/main/java/com/chint/infrastructure/config/LogConfig/RequestLoggingInterceptor.java b/src/main/java/com/chint/infrastructure/config/LogConfig/RequestLoggingInterceptor.java index 56b3e2bd..b32ef124 100644 --- a/src/main/java/com/chint/infrastructure/config/LogConfig/RequestLoggingInterceptor.java +++ b/src/main/java/com/chint/infrastructure/config/LogConfig/RequestLoggingInterceptor.java @@ -85,15 +85,11 @@ public class RequestLoggingInterceptor implements HandlerInterceptor { .name(name) .accessTime(accessTime).build(); //获得时间戳 - DateTimeFormatter formatTimestamp = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss SSSSSS"); + DateTimeFormatter formatTimestamp = DateTimeFormatter.ofPattern("yyyy-MM-dd:HH-mm-ss SSSSSS"); String timestamp = now.format(formatTimestamp); String data = new Gson().toJson(systemLog); - //存入redis - redisCache.setCacheMapValue("SystemLog", timestamp, data); - // 设置单独的过期时间 - String hashFieldKey = "SystemLog" + ":" + timestamp; - redisCache.expire(hashFieldKey, 1, TimeUnit.DAYS); + //存入redis,过期时间设置为1天,hash不能给字段单独设置过期时间 + redisCache.setCacheObject("SystemLog:" + timestamp, data,1,TimeUnit.DAYS); } - }