f28f127a92
CI / Generate TypeScript types (push) Successful in 12s
CI / Server unit tests (push) Successful in 20s
CI / Build server (push) Successful in 24s
CI / Build client (push) Successful in 36s
CI / Playwright E2E tests (push) Successful in 1m17s
CI / Build and push Docker image (push) Successful in 40s
CI / Notify (push) Successful in 2s
46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
import { RedisClientType, createClient } from 'redis';
|
|
import { StorageInterface } from "./StorageInterface";
|
|
|
|
let client: RedisClientType;
|
|
|
|
/**
|
|
* Implementace úložiště využívající Redis server.
|
|
*/
|
|
export default class RedisStorage implements StorageInterface {
|
|
constructor() {
|
|
const HOST = process.env.REDIS_HOST ?? 'localhost';
|
|
const PORT = process.env.REDIS_PORT ?? 6379;
|
|
client = createClient({ url: `redis://${HOST}:${PORT}` });
|
|
}
|
|
|
|
async initialize() {
|
|
await client.connect();
|
|
}
|
|
|
|
async hasData(key: string) {
|
|
const data = await client.json.get(key);
|
|
return (!!data);
|
|
}
|
|
|
|
async getData<Type>(key: string) {
|
|
const data = await client.json.get(key, { path: '.' });
|
|
return data as Type;
|
|
}
|
|
|
|
async setData<Type>(key: string, data: Type) {
|
|
await client.json.set(key, '.', data as any);
|
|
await client.json.get(key);
|
|
}
|
|
|
|
async listKeys(contains?: string): Promise<string[]> {
|
|
// SCAN je bezpečnější než KEYS na produkci (neblokuje server)
|
|
const match = contains ? `*${contains}*` : '*';
|
|
const keys: string[] = [];
|
|
for await (const key of client.scanIterator({ MATCH: match, COUNT: 100 })) {
|
|
// node-redis v4 vrací buď string, nebo (novější verze) pole stringů
|
|
if (Array.isArray(key)) keys.push(...key);
|
|
else keys.push(key);
|
|
}
|
|
return keys;
|
|
}
|
|
} |