1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-06 02:15:48 +03:00

Updates examples. (#2219)

* Updates examples.

* Added command link for hset.
This commit is contained in:
Simon Prickett
2022-08-15 13:39:28 +01:00
committed by GitHub
parent 60ad6aab0b
commit f3462abf33
19 changed files with 972 additions and 1019 deletions

View File

@@ -1,53 +1,50 @@
// A sample stream producer using XADD.
// https://redis.io/commands/xadd/
import { createClient } from 'redis';
async function streamProducer() {
const client = createClient();
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...
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.
}
);
// 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',
id, // Re-use the ID from the previous stream.
// 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...
// 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();
}
);
}
streamProducer();
// 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();