bdcf4fe299
- Move main process to electron/main/ - Move preload to electron/preload/ - Add shared types in electron/shared/ - Split redis-service into modular redis/ directory - Flatten renderer to src/ (remove src/renderer/ nesting) - Update electron.vite.config.ts paths - Update tsconfig paths - Output to electron-dist/
74 lines
2.3 KiB
TypeScript
74 lines
2.3 KiB
TypeScript
// src/main/redis/keys.ts
|
|
// Key 操作
|
|
import { getClient } from './connection'
|
|
|
|
export async function getKeyType(id: string, key: string): Promise<string> {
|
|
const client = getClient(id)
|
|
if (!client) throw new Error(`Client ${id} not found`)
|
|
return client.type(key)
|
|
}
|
|
|
|
export async function getKeyTTL(id: string, key: string): Promise<number> {
|
|
const client = getClient(id)
|
|
if (!client) throw new Error(`Client ${id} not found`)
|
|
return client.ttl(key)
|
|
}
|
|
|
|
export async function deleteKey(id: string, key: string): Promise<void> {
|
|
const client = getClient(id)
|
|
if (!client) throw new Error(`Client ${id} not found`)
|
|
await client.del(key)
|
|
}
|
|
|
|
export async function setKeyTTL(id: string, key: string, ttl: number): Promise<void> {
|
|
const client = getClient(id)
|
|
if (!client) throw new Error(`Client ${id} not found`)
|
|
if (ttl < 0) {
|
|
await client.persist(key)
|
|
} else {
|
|
await client.expire(key, ttl)
|
|
}
|
|
}
|
|
|
|
export async function renameKey(id: string, oldKey: string, newKey: string): Promise<void> {
|
|
const client = getClient(id)
|
|
if (!client) throw new Error(`Client ${id} not found`)
|
|
await client.rename(oldKey, newKey)
|
|
}
|
|
|
|
export async function existsKey(id: string, key: string): Promise<boolean> {
|
|
const client = getClient(id)
|
|
if (!client) throw new Error(`Client ${id} not found`)
|
|
return (await client.exists(key)) === 1
|
|
}
|
|
|
|
export async function batchDelete(id: string, keys: string[]): Promise<number> {
|
|
const client = getClient(id)
|
|
if (!client) throw new Error(`Client ${id} not found`)
|
|
if (keys.length === 0) return 0
|
|
const pipeline = client.pipeline()
|
|
for (const key of keys) {
|
|
pipeline.del(key)
|
|
}
|
|
const results = await pipeline.exec()
|
|
return results?.filter(([err]) => !err).length ?? 0
|
|
}
|
|
|
|
export async function memoryUsage(id: string, key: string): Promise<number | null> {
|
|
const client = getClient(id)
|
|
if (!client) throw new Error(`Client ${id} not found`)
|
|
return client.memory('USAGE', key) as Promise<number | null>
|
|
}
|
|
|
|
export async function scanKeys(
|
|
id: string,
|
|
cursor: string,
|
|
pattern: string,
|
|
count: number
|
|
): Promise<{ cursor: string; keys: string[] }> {
|
|
const client = getClient(id)
|
|
if (!client) throw new Error(`Client ${id} not found`)
|
|
const [nextCursor, keys] = await client.scan(cursor, 'MATCH', pattern, 'COUNT', count)
|
|
return { cursor: nextCursor, keys }
|
|
}
|