1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-04 15:02:09 +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,83 +1,80 @@
// This example demonstrates the use of the Count-Min Sketch
// in the RedisBloom module (https://redisbloom.io/)
// in the RedisBloom module (https://redis.io/docs/stack/bloom/)
import { createClient } from 'redis';
async function countMinSketch() {
const client = createClient();
const client = createClient();
await client.connect();
await client.connect();
// Delete any pre-existing Count-Min Sketch.
await client.del('mycms');
// Delete any pre-existing Count-Min Sketch.
await client.del('mycms');
// Initialize a Count-Min Sketch with error rate and probability:
// https://oss.redis.com/redisbloom/CountMinSketch_Commands/#cmsinitbyprob
try {
await client.cms.initByProb('mycms', 0.001, 0.01);
console.log('Initialized Count-Min Sketch.');
} catch (e) {
console.log('Error, maybe RedisBloom is not installed?:');
console.log(e);
}
const teamMembers = [
'leibale',
'simon',
'guy',
'suze',
'brian',
'steve',
'kyleb',
'kyleo',
'josefin',
'alex',
'nava',
'lance',
'rachel',
'kaitlyn'
];
// Store actual counts for comparison with CMS.
let actualCounts = {};
// Randomly emit a team member and count them with the CMS.
for (let n = 0; n < 1000; n++) {
const teamMember = teamMembers[Math.floor(Math.random() * teamMembers.length)];
await client.cms.incrBy('mycms', {
item: teamMember,
incrementBy: 1
});
actualCounts[teamMember] = actualCounts[teamMember] ? actualCounts[teamMember] + 1 : 1;
console.log(`Incremented score for ${teamMember}.`);
}
// Get count estimate for some team members:
// https://oss.redis.com/redisbloom/CountMinSketch_Commands/#cmsquery
const [ alexCount, rachelCount ] = await client.cms.query('mycms', [
'simon',
'lance'
]);
console.log(`Count estimate for alex: ${alexCount} (actual ${actualCounts.alex}).`);
console.log(`Count estimate for rachel: ${rachelCount} (actual ${actualCounts.rachel}).`);
// Get overall information about the Count-Min Sketch:
// https://oss.redis.com/redisbloom/CountMinSketch_Commands/#cmsinfo
const info = await client.cms.info('mycms');
console.log('Count-Min Sketch info:');
// info looks like this:
// {
// width: 2000,
// depth: 7,
// count: 1000
// }
console.log(info);
await client.quit();
// Initialize a Count-Min Sketch with error rate and probability:
// https://redis.io/commands/cms.initbyprob/
try {
await client.cms.initByProb('mycms', 0.001, 0.01);
console.log('Reserved Count Min Sketch.');
} catch (e) {
console.log('Error, maybe RedisBloom is not installed?:');
console.log(e);
}
countMinSketch();
const teamMembers = [
'leibale',
'simon',
'guy',
'suze',
'brian',
'steve',
'kyleb',
'kyleo',
'josefin',
'alex',
'nava',
'lance',
'rachel',
'kaitlyn'
];
// Store actual counts for comparison with CMS.
let actualCounts = {};
// Randomly emit a team member and count them with the CMS.
// https://redis.io/commands/cms.incrby/
for (let n = 0; n < 1000; n++) {
const teamMember = teamMembers[Math.floor(Math.random() * teamMembers.length)];
await client.cms.incrBy('mycms', {
item: teamMember,
incrementBy: 1
});
actualCounts[teamMember] = actualCounts[teamMember] ? actualCounts[teamMember] + 1 : 1;
console.log(`Incremented score for ${teamMember}.`);
}
// Get count estimate for some team members:
// https://redis.io/commands/cms.query/
const [ alexCount, rachelCount ] = await client.cms.query('mycms', [
'alex',
'rachel'
]);
console.log(`Count estimate for alex: ${alexCount} (actual ${actualCounts.alex}).`);
console.log(`Count estimate for rachel: ${rachelCount} (actual ${actualCounts.rachel}).`);
// Get overall information about the Count-Min Sketch:
// https://redis.io/commands/cms.info/
const info = await client.cms.info('mycms');
console.log('Count-Min Sketch info:');
// info looks like this:
// {
// width: 2000,
// depth: 7,
// count: 1000
// }
console.log(info);
await client.quit();