Skip to content

Instantly share code, notes, and snippets.

@loicdescotte
Forked from leommoore/node_redis.md
Last active September 8, 2020 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loicdescotte/8cccab4312b32094ae01ead432a28212 to your computer and use it in GitHub Desktop.
Save loicdescotte/8cccab4312b32094ae01ead432a28212 to your computer and use it in GitHub Desktop.
Node - Redis

Node - Redis

Redis is a simple key, value pair database. The common commands include:

Data StructureCommands
StringsSET, GET, DEL, APPEND, DECR, INCR...
HashsHSET, HGET, HDEL, HGETALL...
ListsLPUSH, LREM, LTRIM, RPOP, LINSERT...
SetsSADD, SREM, SMOVE, SMEMBERS...
Sorted SetsZADD, ZREM, ZSCORE, ZRANK...

Installing Redis

Install instructions from: http://redis.io/download

Download, extract and compile Redis with:

$ wget http://download.redis.io/releases/redis-2.8.2.tar.gz
$ tar xzf redis-2.8.2.tar.gz
$ cd redis-2.8.2
$ make

To start the Redis server:

$ src/redis-server

To access the Redis client:

$ src/redis-cli
redis>

Using Redis

redis>KEYS *
redis>(empty list or set)

redis>SET my.key test
redis>OK

redis>GET my.key
redis>"test"

redis>KEYS *
redis>"my.key"

Strings

redis>SET online.users 0
redis>OK
redis>INCR online.users
redis>1
redis>INCR online.users
redis>2

Hashs

redis>HSET profile.1 name Leo
redis>HGETALL
      "name"
      "Leo"

redis>HSET profile.1 lastname Moore
redis>HGETALL
      "name"
      "Leo"
      "lastname"
      "Moore"

redis>HDEL profile.1
      //Deleted

Lists

redis>RPUSH profile.1.jobs "job 1"
redis>RPUSH profile.1.jobs "job 2"

redis>LRANGE profile.1 0 -1
      "job 1"
      "job 2"

redis>LPUSH profile.1.jobs "job 0"
redis>LRANGE profile.1 0 -1
      "job 0"
      "job 1"
      "job 2"

Sets

redis>SADD myset "a member"
redis>SMEMBERS myset
      "a member"
      
redis>SADD myset "a member"
redis>SMEMBERS myset         //will not add same member twice
      "a member"

redis>SREM myset "a member"  //deletes member

Install NPM Redis Module

The NPM Redis Module provide non-blocking commands to enable access to the Redis database.

npm install redis

Documentation: https://github.com/mranney/node_redis

Redis Strings

Storing strings using SET and GET.

var redis = require('redis');
var client = redis.createClient(); //redis.createClient(port, host, options)

//Error handling
client.on("error", function (err) {
  console.log("Error " + err);
});

//Set
client.set("message1", "Hello, my name is Jack");
client.set("message2", "Goodbye from Jane");

//Get
client.get("message1", function(err, reply){
  console.log(reply); //"Hello, my name is Jack"
});

client.quit(); //To close the connection gracefully
//client.end() closes connection immediately

Redis Lists

Add a string to the "messages" list using LPUSH.

var message = "Hello, my name is Jack";
  client.lpush("messages", message, function(err, reply){
  console.log(reply);
  //"1"
});

Add another string to "messages"

var message = "Goodbye from Jane";
  client.lpush("messages", message, function(err, reply){
  console.log(reply);
  //"2"
});

Reading from a list using LPUSH and LTRIM.

var message = "Sorry, wrong number";
  client.lpush("messages", message, function(err, reply){
  client.ltrim("messages", 0, 1); //ltrim keeps the first two strings and removes the rest
});

Reading from a list using LRANGE

client.lrange("messages", 0, -1, function(err, messages){
  console.log(messages); //replies with all strings in the list
  //["Hello, my name is Jack", "Sorry, wrong number"]
})

Redis Sets

Add and remove members of the names set

client.sadd("names", "John");
client.sadd("names", "Jane");
client.sadd("names", "Fred");

client.srem("names", "Fred");

List all members of the set

client.smembers("names", function(err, names){
  console.log(names); //["John", "Jane"]
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment