Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates examples. #2219

Merged
merged 3 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ When adding a new example, please follow these guidelines:
* Use semicolons
* Use `async` and `await`
* Use single quotes, `'hello'` not `"hello"`
* Place your example code in a single `async` function where possible, named according to the file name e.g. `add-to-stream.js` would contain `const addtoStream = async () => { ... };`, and call this function at the end of the file e.g. `addToStream();`
* Use [template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) when embedding expressions in strings
* Unless your example requires a connection string, assume Redis is on the default localhost port 6379 with no password
* Use meaningful example data, let's not use `foo`, `bar`, `baz` etc!
* Leave an empty line at the end of your `.js` file
Expand All @@ -71,18 +71,17 @@ Here's a starter template for adding a new example, imagine this is stored in `d

// Set up the data in redis-cli using these commands:
// <add your command(s) here, one per line>
//
// Alternatively, add code that sets up the data.

import { createClient } from 'redis';

async function doSomething() {
const client = createClient();
const client = createClient();

await client.connect();
await client.connect();

// Add your example code here...
// Add your example code here...

await client.quit();
}
await client.quit();

doSomething();
```
32 changes: 15 additions & 17 deletions examples/blocking-list-pop.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,25 @@

import { createClient, commandOptions } from 'redis';

async function blockingListPop() {
const client = createClient();
const client = createClient();

await client.connect();
await client.connect();

const keyName = 'keyName';
const keyName = 'keyName';

const blpopPromise = client.blPop(
commandOptions({ isolated: true }),
keyName,
0
);
const blpopPromise = client.blPop(
commandOptions({ isolated: true }),
keyName,
0
);

await client.lPush(keyName, 'value');
await client.lPush(keyName, 'value');

await blpopPromise;
const listItem = await blpopPromise;

console.log('blpopPromise resolved');
console.log(keyName);
console.log('blpopPromise resolved');
// listItem will be:
// {"key":"keyName","element":"value"}
console.log(`listItem is '${JSON.stringify(listItem)}'`);

await client.quit();
}

blockingListPop();
await client.quit();
127 changes: 64 additions & 63 deletions examples/bloom-filter.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,80 @@
// This example demonstrates the use of the Bloom Filter
// in the RedisBloom module (https://redisbloom.io/)
// in the RedisBloom module (https://redis.io/docs/stack/bloom/)

import { createClient } from 'redis';

async function bloomFilter() {
const client = createClient();
const client = createClient();

await client.connect();
await client.connect();

// Delete any pre-existing Bloom Filter.
await client.del('mybloom');
// Delete any pre-existing Bloom Filter.
await client.del('mybloom');

// Reserve a Bloom Filter with configurable error rate and capacity.
// https://oss.redis.com/redisbloom/Bloom_Commands/#bfreserve
try {
await client.bf.reserve('mybloom', 0.01, 1000);
console.log('Reserved Bloom Filter.');
} catch (e) {
if (e.message.endsWith('item exists')) {
console.log('Bloom Filter already reserved.');
} else {
console.log('Error, maybe RedisBloom is not installed?:');
console.log(e);
}
// Reserve a Bloom Filter with configurable error rate and capacity.
// https://redis.io/commands/bf.reserve/
try {
await client.bf.reserve('mybloom', 0.01, 1000);
console.log('Reserved Bloom Filter.');
} catch (e) {
if (e.message.endsWith('item exists')) {
console.log('Bloom Filter already reserved.');
} else {
console.log('Error, maybe RedisBloom is not installed?:');
console.log(e);
}
}

// Add items to Bloom Filter individually with BF.ADD command.
await Promise.all([
client.bf.add('mybloom', 'leibale'),
client.bf.add('mybloom', 'simon'),
client.bf.add('mybloom', 'guy'),
client.bf.add('mybloom', 'suze'),
client.bf.add('mybloom', 'brian'),
client.bf.add('mybloom', 'steve'),
client.bf.add('mybloom', 'kyle'),
client.bf.add('mybloom', 'josefin'),
client.bf.add('mybloom', 'alex'),
client.bf.add('mybloom', 'nava'),
]);

// Add multiple items to Bloom Filter at once with BF.MADD command.
await client.bf.mAdd('mybloom', [
'kaitlyn',
'rachel'
]);
// Add items to Bloom Filter individually with BF.ADD command.
// https://redis.io/commands/bf.add/
await Promise.all([
client.bf.add('mybloom', 'leibale'),
client.bf.add('mybloom', 'simon'),
client.bf.add('mybloom', 'guy'),
client.bf.add('mybloom', 'suze'),
client.bf.add('mybloom', 'brian'),
client.bf.add('mybloom', 'steve'),
client.bf.add('mybloom', 'kyle'),
client.bf.add('mybloom', 'josefin'),
client.bf.add('mybloom', 'alex'),
client.bf.add('mybloom', 'nava'),
]);

console.log('Added members to Bloom Filter.');
// Add multiple items to Bloom Filter at once with BF.MADD command.
// https://redis.io/commands/bf.madd/
await client.bf.mAdd('mybloom', [
'kaitlyn',
'rachel'
]);

// Check whether a member exists with the BF.EXISTS command.
const simonExists = await client.bf.exists('mybloom', 'simon');
console.log(`simon ${simonExists ? 'may' : 'does not'} exist in the Bloom Filter.`);
console.log('Added members to Bloom Filter.');

// Check whether multiple members exist with the BF.MEXISTS command:
const [ lanceExists, leibaleExists ] = await client.bf.mExists('mybloom', [
'lance',
'leibale'
]);
// Check whether a member exists with the BF.EXISTS command.
// https://redis.io/commands/bf.exists/
const simonExists = await client.bf.exists('mybloom', 'simon');
console.log(`simon ${simonExists ? 'may' : 'does not'} exist in the Bloom Filter.`);

console.log(`lance ${lanceExists ? 'may' : 'does not'} exist in the Bloom Filter.`);
console.log(`leibale ${leibaleExists ? 'may' : 'does not'} exist in the Bloom Filter.`);
// Check whether multiple members exist with the BF.MEXISTS command.
// https://redis.io/commands/bf.mexists/
const [ lanceExists, leibaleExists ] = await client.bf.mExists('mybloom', [
'lance',
'leibale'
]);

// Get stats for the Bloom Filter with the BF.INFO command:
const info = await client.bf.info('mybloom');
// info looks like this:
//
// {
// capacity: 1000,
// size: 1531,
// numberOfFilters: 1,
// numberOfInsertedItems: 12,
// expansionRate: 2
// }
console.log(info);
console.log(`lance ${lanceExists ? 'may' : 'does not'} exist in the Bloom Filter.`);
console.log(`leibale ${leibaleExists ? 'may' : 'does not'} exist in the Bloom Filter.`);

await client.quit();
}
// Get stats for the Bloom Filter with the BF.INFO command.
// https://redis.io/commands/bf.info/
const info = await client.bf.info('mybloom');
// info looks like this:
//
// {
// capacity: 1000,
// size: 1531,
// numberOfFilters: 1,
// numberOfInsertedItems: 12,
// expansionRate: 2
// }
console.log(info);

bloomFilter();
await client.quit();
33 changes: 14 additions & 19 deletions examples/command-with-modifiers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,23 @@

import { createClient } from 'redis';

async function commandWithModifiers() {
const client = createClient();
const client = createClient();

await client.connect();
await client.del('mykey');
await client.connect();
await client.del('mykey');

let result = await client.set('mykey', 'myvalue', {
EX: 60,
GET: true
});
let result = await client.set('mykey', 'myvalue', {
EX: 60,
GET: true
});

console.log(result); //nil
console.log(result); //null

result = await client.set('mykey', 'newvalue', {
EX: 60,
GET: true
});
result = await client.set('mykey', 'newvalue', {
EX: 60,
GET: true
});

console.log(result); //myvalue

await client.quit();
}

commandWithModifiers();
console.log(result); //myvalue

await client.quit();
28 changes: 12 additions & 16 deletions examples/connect-as-acl-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,20 @@

import { createClient } from 'redis';

async function connectWithACLUser() {
const client = createClient({
url: 'redis://testuser:testpassword@127.0.0.1:6379'
});
const client = createClient({
url: 'redis://testuser:testpassword@127.0.0.1:6379'
});

await client.connect();
await client.connect();

// Returns PONG
console.log(`Response from PING command: ${await client.ping()}`);
// Returns PONG
console.log(`Response from PING command: ${await client.ping()}`);

try {
// This will error as this user is not allowed to run this command...
console.log(`Response from GET command: ${await client.get('somekey')}`);
} catch (e) {
console.log(`GET command failed: ${e.message}`);
}

await client.quit();
try {
// This will error as this user is not allowed to run this command...
console.log(`Response from GET command: ${await client.get('somekey')}`);
} catch (e) {
console.log(`GET command failed: ${e.message}`);
}

connectWithACLUser();
await client.quit();
Loading