Untitled

mail@pastecode.io avatar
unknown
plain_text
7 days ago
6.5 kB
2
Indexable
Never
/** @format */
const redisClient = require("../../databases/redis");
const createError = require("http-errors");

async function getData(key) {
  try {
    const data = await redisClient.get(key);
    return JSON.parse(data);
  } catch (error) {
    throw createError.BadRequest(error.message);
  }
}

async function setData(key, value) {
  try {
    const data = JSON.stringify(value);
    await redisClient.set(key, data);
  } catch (error) {
    throw createError.BadRequest(error.message);
  }
}

async function deleteKey(key) {
  try {
    console.log("Deleting Redis key:", key);
    await redisClient.del(key);
  } catch (error) {
    throw createError.BadRequest(error.message);
  }
}

async function setDataWithTTL(key, value, ttl) {
  try {
    const data = JSON.stringify(value);
    await redisClient.set(key, data, "EX", ttl || 180);
  } catch (error) {
    throw createError.BadRequest(error.message);
  }
}

async function setRange(key, data) {
  try {
    await redisClient.lpush(key, data);
  } catch (error) {
    throw createError.BadRequest(error.message);
  }
}

async function getRange(key, start, end) {
  try {
    console.log(`Querying Redis for key: ${key} from ${start} to ${end}`);
    const list = await redisClient.lrange(key, start, end);
    console.log("Retrieved list:", list);
    return list;
  } catch (error) {
    console.error("Error retrieving list from Redis:", error);
    throw createError.BadRequest(error.message);
  }
}

async function setHash(key, data) {
  try {
    const args = Object.entries(data).flat();
    await redisClient.hset(key, args);
  } catch (error) {
    console.error("Error setting hash in Redis:", error);
    throw createError.BadRequest(error.message);
  }
}

async function getHashData(list) {
  try {
    const productDetailsPromise = list.map((id) => {
      const key = `block_${id}`;
      return redisClient.hgetall(key);
    });
    return Promise.all(productDetailsPromise);
  } catch (error) {
    throw createError.BadRequest(error.message);
  }
}

async function removeFromList(key, id) {
  try {
    const data = await redisClient.lrem(key, 0, id);
    return data;
  } catch (error) {
    throw createError.BadRequest(error.message);
  }
}

async function removeFromHashTable(key) {
  try {
    const data = await redisClient.del(key);
    return data;
  } catch (error) {
    throw createError.BadRequest(error.message);
  }
}

async function setDataWithTTLForUser(key, value) {
  //console.log("kkvv", key, value);
  try {
    const data = JSON.stringify(value);
    await redisClient.set(key, data, "EX", 86400);
  } catch (error) {
    throw createError.BadRequest(error.message);
  }
}

/**
 * Higher level redis queries
 */

//for feed posts
async function addToSortedSetWithPipeline(key, posts, ttl) {
  const pipeline = redisClient.pipeline();
  posts.forEach((post) => {
    //console.log(`Adding post ID ${post.id} with score ${post.c_t} to ${key}`);
    pipeline.zadd(key, post.c_t, post.id.toString());
  });

  // Set the TTL for the entire sorted set
  if (ttl) {
    pipeline.expire(key, ttl);
  }

  await pipeline.exec();
}

//for feed posts
async function getSortedSetRange(key, start, end) {
  //const result = await redisClient.del(key);
  try {
    console.log(
      `Querying Redis Sorted Set for key: ${key} from ${start} to ${end}`
    );
    const members = await redisClient.zrevrange(key, start, end);
    console.log("Retrieved Sorted Set members:", members);
    return members;
  } catch (error) {
    console.error("Error retrieving sorted set members from Redis:", error);
    throw createError.BadRequest(error.message);
  }
}

//for feed posts
// async function addNewRecordAndRemoveOldest(keys, postId, timeAsScore) {
//   const pipeline = redisClient.pipeline();

//   // Iterate over each key and apply the operations
//   keys.forEach((key) => {
//     // Add the new post ID with the timestamp as score
//     pipeline.zadd(key, timeAsScore, postId);

//     // Remove the oldest post by removing the first element in the sorted set
//     // This assumes the sorted set is ordered by timestamp ascending (oldest first)
//     pipeline.zremrangebyrank(key, 0, 0);
//   });

//   // Execute all commands in the pipeline
//   try {
//     await pipeline.exec();
//     console.log("New post added and the oldest removed from all keys.");
//   } catch (error) {
//     console.error("Error managing sorted sets:", error);
//   }
// }

async function addNewRecordAndRemoveOldest(keys, postId, timeAsScore) {
  const pipeline = redisClient.pipeline();

  // Iterate over each key and apply the operations
  keys.forEach((key) => {
    console.log(
      `Adding postId: ${postId} to sorted set: ${key} with score: ${timeAsScore}`
    );

    // Add the new post ID with the timestamp as score
    pipeline.zadd(key, timeAsScore, postId);

    // Check the size of the sorted set
    pipeline.zcard(key);
  });

  // Execute all commands in the pipeline
  try {
    const results = await pipeline.exec();
    console.log("Pipeline results:", results);

    // Iterate over results to check size and conditionally remove the oldest element
    keys.forEach(async (key, index) => {
      const zcardResult = results[index * 2 + 1]; // Get the result of zcard for each key
      const [error, size] = zcardResult;

      if (!error && size > 1000) {
        console.log(`Removing oldest element from ${key} as size exceeds 1000`);
        await redisClient.zremrangebyrank(key, 0, 0);
      }
    });
  } catch (error) {
    console.error("Error managing sorted sets:", error);
  }
}

async function addToSortedSetGroupRecommend(key, groupIds, ttl) {
  const pipeline = redisClient.pipeline();
  groupIds.forEach((groupId) => {
    pipeline.zadd(key, groupId.score, groupId.value.toString());
  });

  // Set the TTL for the entire sorted set
  if (ttl) {
    pipeline.expire(key, ttl);
  }

  await pipeline.exec();
}

module.exports = {
  getData,
  setData,
  deleteKey,
  setDataWithTTL,
  setRange,
  getRange,
  setHash,
  getHashData,
  removeFromList,
  removeFromHashTable,
  setDataWithTTLForUser,
  //higher level
  getSortedSetRange,
  addToSortedSetWithPipeline,
  addNewRecordAndRemoveOldest,
  addToSortedSetGroupRecommend,
};
Leave a Comment