scan.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. const BATCH_SIZE = 1000 // Default batch size for SCAN
  2. /**
  3. * Asynchronously scans a Redis instance or cluster for keys matching a pattern.
  4. *
  5. * This function handles both standalone Redis instances and Redis clusters.
  6. * For clusters, it iterates over all master nodes. It yields keys in batches
  7. * as they are found by the SCAN command.
  8. *
  9. * @param {object} redisClient - The Redis client instance (from @overleaf/redis-wrapper).
  10. * @param {string} pattern - The pattern to match keys against (e.g., 'user:*').
  11. * @param {number} [count=BATCH_SIZE] - Optional hint for Redis SCAN count per iteration.
  12. * @yields {string[]} A batch of matching keys.
  13. */
  14. async function* scanRedisCluster(redisClient, pattern, count = BATCH_SIZE) {
  15. const nodes = redisClient.nodes ? redisClient.nodes('master') : [redisClient]
  16. for (const node of nodes) {
  17. let cursor = '0'
  18. do {
  19. // redisClient from @overleaf/redis-wrapper uses ioredis style commands
  20. const [nextCursor, keys] = await node.scan(
  21. cursor,
  22. 'MATCH',
  23. pattern,
  24. 'COUNT',
  25. count
  26. )
  27. cursor = nextCursor
  28. if (keys.length > 0) {
  29. yield keys
  30. }
  31. } while (cursor !== '0')
  32. }
  33. }
  34. /**
  35. * Extracts the content within the first pair of curly braces {} from a string.
  36. * This is used to extract a user ID or project ID from a Redis key.
  37. *
  38. * @param {string} key - The input string containing content within curly braces.
  39. * @returns {string | null} The extracted content (the key ID) if found, otherwise null.
  40. */
  41. function extractKeyId(key) {
  42. const match = key.match(/\{(.*?)\}/)
  43. if (match && match[1]) {
  44. return match[1]
  45. }
  46. return null
  47. }
  48. module.exports = { scanRedisCluster, extractKeyId }