Improved redis to get keys by prefix.

This commit is contained in:
Tom Butcher 2025-12-13 21:12:07 +00:00
parent 113a16818a
commit 6e24f61819

View File

@ -31,6 +31,7 @@ class RedisServer {
async connect() { async connect() {
if (this.connected) return; if (this.connected) return;
logger.info('Connecting to Redis...');
await this.client.connect(); await this.client.connect();
this.connected = true; this.connected = true;
logger.info('Connected to Redis'); logger.info('Connected to Redis');
@ -61,6 +62,21 @@ class RedisServer {
await this.connect(); await this.connect();
await this.client.del(key); await this.client.del(key);
} }
async getKeysByPattern(pattern) {
await this.connect();
const keys = [];
let cursor = '0';
do {
const result = await this.client.scan(cursor, {
MATCH: pattern,
COUNT: 100,
});
cursor = result.cursor;
keys.push(...result.keys);
} while (cursor !== '0');
return keys;
}
} }
const redisServer = new RedisServer(); const redisServer = new RedisServer();