Created
September 9, 2022 22:07
-
-
Save georgeben/f17d6d0ab3bbbb204f162b8ed7138b5b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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