-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
36 lines (29 loc) · 930 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { createClient } from 'redis';
const REDIS_URL = process.env.REDIS_URL || 'redis://localhost:6379';
const client = createClient({
url: REDIS_URL
});
await client.connect();
const subClient = client.duplicate();
await subClient.connect();
await subClient.pSubscribe('__keyspace@0__:tokens:*', async (message, channel) => {
console.log(`event >>> ${message} on ${channel}`);
const affectedKey = channel.substring('__keyspace@0__:'.length);
const howMany = await client.sCard(affectedKey);
console.log(`Set cardinality ${affectedKey} is ${howMany}`);
const userName = affectedKey.split(':')[1];
if (howMany > 0) {
await client.zAdd('scoreboard', [
{
score: howMany,
value: userName
}
]);
} else {
await client.zRem('scoreboard', userName);
}
console.log('Scores:');
console.log(await client.zRangeWithScores('scoreboard', 0, -1, {
REV: true
}));
});