Zavedení podpory pro Redis, agnostické úložiště dat

This commit is contained in:
2023-08-06 17:46:51 +02:00
parent 8a75c98c9a
commit 37542499a9
11 changed files with 282 additions and 97 deletions

View File

@@ -0,0 +1,32 @@
import { RedisClientType, createClient } from 'redis';
import { StorageInterface } from "./StorageInterface";
import { ClientData } from '../../../types';
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}` });
client.connect();
}
async hasData(date: string) {
const data = await client.json.get(date);
return (data ? true : false);
}
async getData(date: string) {
const data = await client.json.get(date, { path: '.' });
return data as unknown as ClientData;
}
async setData(date: string, data: ClientData) {
await client.json.set(date, '.', data as any);
const check = await client.json.get(date);
}
}