Skip to content

gorankarlic/ok-redis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

62 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Redis client

Fast and efficient Redis standalone client for Node without any dependencies.

Basic usage

Connect to localhost

const Redis = require("Redis");

const client = await Redis.connect(opts);
const pong = await client.ping();
console.log(pong); //prints "PONG"

Connect to a Redis cluster

Connect to any replica - other replicas are automatically discovered and connected.

const Redis = require("Redis");

const opts = Redis.opts().host("localhost").port(30001);
const cluster = await Redis.connectCluster(opts);
await cluster.set("foo", "bar");
await cluster.quit();

Use promises

const Redis = require("Redis");

const opts = Redis.opts().host("localhost").port(6379);
const client = await Redis.connect(opts);
await client.set("foo", "bar");
const bar = await client.get("foo");
console.log(bar); //prints <Buffer 62 61 72>
await client.quit();

Use callbacks

const Redis = require("Redis");

const opts = Redis.opts().host("localhost").port(6379);
Redis.connect(opts, (err, client) =>
{
    client.set("foo", "bar");
    client.get("foo", (err, value) => console.log(value)); //prints <Buffer 62 61 72>
});

Switch between returning buffers or UTF-8 strings

const Redis = require("Redis");

const opts = Redis.opts().host("localhost").port(6379);
const client = await Redis.connect(opts);
await client.set("foo", "bar");

client.return(String); //switch to string mode
const string = await client.get("foo");
console.log(string); //prints "bar"

client.return(Buffer); //switch to buffer mode
const buffer = await client.get("foo");
console.log(buffer); //prints <Buffer 62 61 72>

await client.quit();

Return UTF-8 strings instead of buffers for all commands

const Redis = require("Redis");

const opts = Redis.opts().host("localhost").port(6379).return(String);
const client = Redis.connect(opts);
await client.set("foo", "bar");

const string = await client.get("foo");
console.log(string); //prints "bar"

await client.quit();

Issue commands immediately

Create an unconnected instance to issue commands before a connection has been established.

const Redis = require("Redis");

const opts = Redis.opts().host("localhost").port(30001);
const client = Redis.create(opts);
const promises = [];
promises.push(cluster.connect());
promises.push(cluster.set("foo", "bar"));
promises.push(cluster.quit());
await Promise.all(promises);

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published