Improved redis to get keys by prefix.
This commit is contained in:
parent
5209467b0a
commit
e0776e9595
@ -31,6 +31,7 @@ class RedisServer {
|
||||
|
||||
async connect() {
|
||||
if (this.connected) return;
|
||||
logger.info('Connecting to Redis...');
|
||||
await this.client.connect();
|
||||
this.connected = true;
|
||||
logger.info('Connected to Redis');
|
||||
@ -61,6 +62,21 @@ class RedisServer {
|
||||
await this.connect();
|
||||
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();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user