日志接口调整
This commit is contained in:
parent
103d5a44f3
commit
b7839ab729
|
@ -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<String> getAccessToken(String code) {
|
||||
|
|
|
@ -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<String, Object> systemLogMap = redisCache.getCacheHashValues("SystemLog", 100);
|
||||
Map<String, Object> systemLogMap = redisCache.getDataWithPrefix("SystemLog:", 100);
|
||||
Gson gson = new Gson();
|
||||
// 转换日志数据并保存到数据库
|
||||
List<SystemLog> 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<String, Object> systemLogMap = redisCache.getCacheHashValues("SystemLog", 100);
|
||||
Map<String, Object> 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++;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String> keys(final String 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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue