Skip to content

Instantly share code, notes, and snippets.

@georgeben
Created September 9, 2022 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save georgeben/f17d6d0ab3bbbb204f162b8ed7138b5b to your computer and use it in GitHub Desktop.
Save georgeben/f17d6d0ab3bbbb204f162b8ed7138b5b to your computer and use it in GitHub Desktop.
/* eslint-disable prefer-destructuring */
const { promisify } = require("util");
function scanRedis(pattern) {
const scan = promisify(redisClient.scan).bind(redisClient);
const result = [];
let cursor = "0";
do {
// eslint-disable-next-line no-await-in-loop
const reply = await scan(cursor, "MATCH", pattern, "COUNT", 100);
cursor = reply[0];
result.push(...reply[1]);
} while (cursor !== "0");
return result;
}
function destroyUserSessions(userId) {
const sessions = await scanRedis({
pattern: `sess:${userId}-*`,
});
const unlinkSessions = sessions.map((session) => redisClient.unlink(session));
await Promise.all(unlinkSessions);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment