日志接口调整

This commit is contained in:
dengwc 2024-03-22 17:26:53 +08:00
parent 103d5a44f3
commit b7839ab729
4 changed files with 68 additions and 22 deletions

View File

@ -35,12 +35,6 @@ public class FeishuLoginStrategy implements LoginStrategy {
@Value("${feishu.appSecret}") @Value("${feishu.appSecret}")
private String appSecret; private String appSecret;
@Value("${feishu.ANAppId}")
private String ANAppId;
@Value("${feishu.ANAppSecret}")
private String ANAppSecret;
@Override @Override
public Optional<String> getAccessToken(String code) { public Optional<String> getAccessToken(String code) {

View File

@ -46,12 +46,12 @@ public class LogService {
public void batchSaveLog() { public void batchSaveLog() {
while (processedLogCount < 5000) { while (processedLogCount < 5000) {
//获取日志数量数量 //获取日志数量数量
long logNums = redisCache.getHashSize("SystemLog"); int logNums = redisCache.getKeyCountWithPrefix("SystemLog:");
if (logNums < 100L) { if (logNums < 100) {
break;//少于100条结束循环 break;//少于100条结束循环
} }
// 获取100条日志数据 // 获取100条日志数据
Map<String, Object> systemLogMap = redisCache.getCacheHashValues("SystemLog", 100); Map<String, Object> systemLogMap = redisCache.getDataWithPrefix("SystemLog:", 100);
Gson gson = new Gson(); Gson gson = new Gson();
// 转换日志数据并保存到数据库 // 转换日志数据并保存到数据库
List<SystemLog> systemLogs = systemLogMap.values().stream() List<SystemLog> systemLogs = systemLogMap.values().stream()
@ -59,7 +59,7 @@ public class LogService {
.toList(); .toList();
jdbcSystemLogRepository.saveAll(systemLogs); jdbcSystemLogRepository.saveAll(systemLogs);
// 删除已处理的日志数据 // 删除已处理的日志数据
redisCache.delCacheMapValue("SystemLog", systemLogMap.keySet()); long l = redisCache.deleteObject(systemLogMap.keySet());
// 更新已处理日志数量 // 更新已处理日志数量
processedLogCount += systemLogs.size(); processedLogCount += systemLogs.size();
} }
@ -103,18 +103,18 @@ public class LogService {
} }
/** /**
* 保存redis可能剩余的日志信息 * 保存redis的日志信息
*/ */
@Transactional @Transactional
public void saveLogs() { public void saveLogs() {
int count = 0; // 初始化计数器 int count = 0; // 初始化计数器
while (count <= 10000) { //当计数器小于10000时执行循环 while (count <= 10000) { //当计数器小于10000时执行循环
long logNums = redisCache.getHashSize("SystemLog"); int logNums = redisCache.getKeyCountWithPrefix("SystemLog:");
if (logNums <= 0L) { if (logNums <= 0) {
break;//没有数据结束循环 break;//没有数据结束循环
} }
//获取日志信息 //获取日志信息
Map<String, Object> systemLogMap = redisCache.getCacheHashValues("SystemLog", 100); Map<String, Object> systemLogMap = redisCache.getDataWithPrefix("SystemLog:", 100);
if (systemLogMap.isEmpty()) { if (systemLogMap.isEmpty()) {
break; // 如果没有数据可取跳出循环 break; // 如果没有数据可取跳出循环
} }
@ -125,7 +125,7 @@ public class LogService {
).toList(); ).toList();
jdbcSystemLogRepository.saveAll(systemLogs); jdbcSystemLogRepository.saveAll(systemLogs);
// 删除已处理的日志数据 // 删除已处理的日志数据
redisCache.delCacheMapValue("SystemLog", systemLogMap.keySet()); long l = redisCache.deleteObject(systemLogMap.keySet());
count++; count++;
} }
} }

View File

@ -1,10 +1,12 @@
package com.chint.infrastructure.config.LogConfig; package com.chint.infrastructure.config.LogConfig;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.*; import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -268,4 +270,58 @@ public class RedisCache {
public Collection<String> keys(final String pattern) { public Collection<String> keys(final String pattern) {
return redisTemplate.keys(pattern); return redisTemplate.keys(pattern);
} }
/**
* 获取指定数量key前缀相同的数据
* @param prefix key前缀
* @param count 个数
* @return key 和value
*/
public Map<String, Object> getDataWithPrefix(String prefix, int count) {
ScanOptions options = ScanOptions.scanOptions().match(prefix + "*").count(count).build();
Cursor<byte[]> cursor = (Cursor<byte[]>) redisTemplate.execute((RedisCallback<Cursor<byte[]>>) connection -> connection.scan(options));
Map<String, Object> 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<byte[]> cursor = (Cursor<byte[]>) redisTemplate.execute((RedisCallback<Cursor<byte[]>>) connection -> connection.scan(options))) {
if (cursor != null) {
while (cursor.hasNext()) {
cursor.next();
count++;
}
}
}
return count;
}
} }

View File

@ -85,15 +85,11 @@ public class RequestLoggingInterceptor implements HandlerInterceptor {
.name(name) .name(name)
.accessTime(accessTime).build(); .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 timestamp = now.format(formatTimestamp);
String data = new Gson().toJson(systemLog); String data = new Gson().toJson(systemLog);
//存入redis //存入redis,过期时间设置为1天,hash不能给字段单独设置过期时间
redisCache.setCacheMapValue("SystemLog", timestamp, data); redisCache.setCacheObject("SystemLog:" + timestamp, data,1,TimeUnit.DAYS);
// 设置单独的过期时间
String hashFieldKey = "SystemLog" + ":" + timestamp;
redisCache.expire(hashFieldKey, 1, TimeUnit.DAYS);
} }
} }