docker-redis - local tests

Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
Dave Richer
2024-10-02 00:27:11 -04:00
parent b7423aebf6
commit 04dec6d91c
14 changed files with 456 additions and 18 deletions

View File

@@ -8,7 +8,7 @@ const applyRedisHelpers = (pubClient, app, logger) => {
// Store session data in Redis
const setSessionData = async (socketId, key, value) => {
try {
await pubClient.hSet(`socket:${socketId}`, key, JSON.stringify(value)); // Use Redis pubClient
await pubClient.hset(`socket:${socketId}`, key, JSON.stringify(value)); // Use Redis pubClient
} catch (error) {
logger.log(`Error Setting Session Data for socket ${socketId}: ${error}`, "ERROR", "redis");
}
@@ -17,7 +17,7 @@ const applyRedisHelpers = (pubClient, app, logger) => {
// Retrieve session data from Redis
const getSessionData = async (socketId, key) => {
try {
const data = await pubClient.hGet(`socket:${socketId}`, key);
const data = await pubClient.hget(`socket:${socketId}`, key);
return data ? JSON.parse(data) : null;
} catch (error) {
logger.log(`Error Getting Session Data for socket ${socketId}: ${error}`, "ERROR", "redis");
@@ -38,7 +38,7 @@ const applyRedisHelpers = (pubClient, app, logger) => {
try {
// keyValues is expected to be an object { key1: value1, key2: value2, ... }
const entries = Object.entries(keyValues).map(([key, value]) => [key, JSON.stringify(value)]);
await pubClient.hSet(`socket:${socketId}`, ...entries.flat());
await pubClient.hset(`socket:${socketId}`, ...entries.flat());
} catch (error) {
logger.log(`Error Setting Multiple Session Data for socket ${socketId}: ${error}`, "ERROR", "redis");
}
@@ -47,7 +47,7 @@ const applyRedisHelpers = (pubClient, app, logger) => {
// Retrieve multiple session data from Redis
const getMultipleSessionData = async (socketId, keys) => {
try {
const data = await pubClient.hmGet(`socket:${socketId}`, keys);
const data = await pubClient.hmget(`socket:${socketId}`, keys);
// Redis returns an object with null values for missing keys, so we parse the non-null ones
return Object.fromEntries(keys.map((key, index) => [key, data[index] ? JSON.parse(data[index]) : null]));
} catch (error) {
@@ -71,7 +71,7 @@ const applyRedisHelpers = (pubClient, app, logger) => {
// Helper function to add an item to the end of the Redis list
const addItemToEndOfList = async (socketId, key, newItem) => {
try {
await pubClient.rPush(`socket:${socketId}:${key}`, JSON.stringify(newItem));
await pubClient.rpush(`socket:${socketId}:${key}`, JSON.stringify(newItem));
} catch (error) {
logger.log(`Error adding item to the end of the list for socket ${socketId}: ${error}`, "ERROR", "redis");
}
@@ -80,7 +80,7 @@ const applyRedisHelpers = (pubClient, app, logger) => {
// Helper function to add an item to the beginning of the Redis list
const addItemToBeginningOfList = async (socketId, key, newItem) => {
try {
await pubClient.lPush(`socket:${socketId}:${key}`, JSON.stringify(newItem));
await pubClient.lpush(`socket:${socketId}:${key}`, JSON.stringify(newItem));
} catch (error) {
logger.log(`Error adding item to the beginning of the list for socket ${socketId}: ${error}`, "ERROR", "redis");
}
@@ -98,7 +98,7 @@ const applyRedisHelpers = (pubClient, app, logger) => {
// Add methods to manage room users
const addUserToRoom = async (room, user) => {
try {
await pubClient.sAdd(room, JSON.stringify(user));
await pubClient.sadd(room, JSON.stringify(user));
} catch (error) {
logger.log(`Error adding user to room ${room}: ${error}`, "ERROR", "redis");
}
@@ -106,7 +106,7 @@ const applyRedisHelpers = (pubClient, app, logger) => {
const removeUserFromRoom = async (room, user) => {
try {
await pubClient.sRem(room, JSON.stringify(user));
await pubClient.srem(room, JSON.stringify(user));
} catch (error) {
logger.log(`Error removing user to room ${room}: ${error}`, "ERROR", "redis");
}
@@ -114,7 +114,7 @@ const applyRedisHelpers = (pubClient, app, logger) => {
const getUsersInRoom = async (room) => {
try {
const users = await pubClient.sMembers(room);
const users = await pubClient.smembers(room);
return users.map((user) => JSON.parse(user));
} catch (error) {
logger.log(`Error getting users in room ${room}: ${error}`, "ERROR", "redis");