Files
JRedisDesktop/electron/redis/commandLogger.ts
T
2026-07-10 23:05:10 +08:00

50 lines
1.2 KiB
TypeScript

// electron/main/redis/commandLogger.ts
// 命令日志记录器
export interface CommandEntry {
id: number
time: string
connection: string
command: string
duration: number
isWrite: boolean
}
const MAX_ENTRIES = 5000
let nextId = 1
const entries: CommandEntry[] = []
const WRITE_COMMANDS = new Set([
'SET', 'DEL', 'HSET', 'HDEL', 'RPUSH', 'LPUSH', 'LSET', 'LREM',
'SADD', 'SREM', 'ZADD', 'ZREM', 'XADD', 'XDEL', 'XTRIM',
'RENAME', 'EXPIRE', 'PERSIST', 'FLUSHDB', 'SELECT',
'SETEX', 'INCR', 'DECR', 'INCRBY', 'DECRBY', 'APPEND',
'HMSET', 'HINCRBY', 'HINCRBYFLOAT',
'LPOP', 'RPOP', 'BLPOP', 'BRPOP',
'ZINCRBY', 'ZREMRANGEBYRANK', 'ZREMRANGEBYSCORE',
'UNLINK', 'MOVE', 'RENAMENX',
])
export function log(connection: string, command: string, duration: number): void {
const isWrite = WRITE_COMMANDS.has(command.split(' ')[0].toUpperCase())
entries.push({
id: nextId++,
time: new Date().toISOString(),
connection,
command,
duration,
isWrite,
})
if (entries.length > MAX_ENTRIES) {
entries.splice(0, entries.length - MAX_ENTRIES)
}
}
export function getLog(): CommandEntry[] {
return entries
}
export function clearLog(): void {
entries.length = 0
}