-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathstream-producer.js
50 lines (43 loc) · 1.32 KB
/
stream-producer.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// A sample stream producer using XADD.
// https://redis.io/commands/xadd/
import { createClient } from 'redis';
const client = createClient();
await client.connect();
for (let i = 0; i < 10000; i++) {
await client.xAdd(
'mystream',
'*', // * = Let Redis generate a timestamp ID for this new entry.
// Payload to add to the stream:
{
i: i.toString()
// Other name/value pairs can go here as required...
}
);
// Also add to a stream whose length we will cap at approximately
// 1000 entries using the MAXLEN trimming strategy:
// https://redis.io/commands/xadd/
await client.xAdd(
'mytrimmedstream',
'*',
// Payload to add to the stream:
{
i: i.toString()
// Other name/value pairs can go here as required...
},
// Specify a trimming strategy...
{
TRIM: {
strategy: 'MAXLEN', // Trim by length.
strategyModifier: '~', // Approximate trimming.
threshold: 1000 // Retain around 1000 entries.
}
}
);
}
// Take a look at how many entries are in the streams...
// https://redis.io/commands/xlen/
// Should be 10000:
console.log(`Length of mystream: ${await client.xLen('mystream')}.`);
// Should be approximately 1000:
console.log(`Length of mytrimmedstream: ${await client.xLen('mytrimmedstream')}.`);
await client.quit();