Redis is elegant solution to provide fast storage while its supported by Node.js. However, with its different data structures such as List, Set and Hashes, the API of Redis takes time to memorize. Here is quick cheat sheet to help
Installation
Step 1 – Download & Compile
To install Redis:
wget http://download.redis.io/redis-stable.tar.gz tar xvzf redis-stable.tar.gz cd redis-stable make
Here, we installed Stable version of Redis, but if you need some older version then see Google Code Redis repo for version of your need
Step 2 – Test It
Try if your build works correctly by typing “make test”
Step 3 – Configure Redis
After the compilation the src directory inside the Redis distribution is populated with the different executables that are part of Redis:
- redis-server is the Redis Server itself.
- redis-sentinel is the Redis Sentinel executable (monitoring and failover).
- redis-cli is the command line interface utility to talk with Redis.
- redis-benchmark is used to check Redis performances.
- redis-check-aof and redis-check-dump are useful in the rare event of corrupted data files.
It is a good idea to copy both the Redis server and the command line interface in proper places, either manually using the following commands:
- sudo cp src/redis-server /usr/local/bin/
- sudo cp src/redis-cli /usr/local/bin/
Or just using:
cd src/ make install.
NOTE: We assume that /usr/local/bin is in your PATH environment variable so that you can execute both the binaries without specifying the full path.
Starting Redis
To start redis with default configurations:
redis-server
Or if you like to start with custom settings then pass them in at the start up as following:
redis-server /etc/redis.conf
You should use the redis.conf file included in the root directory of the Redis source code distribution as a template to write your configuration file.
Testing Redis
Redis provides a command line utility “redis-cli” that can be used to send commands to Redis. To check if Redis is working properly is sending a PING command using redis-cli
redis-cli ping
This should respond with “PONG” if Redis working properly
To see all options with redis-cli utility such as running different port,host, etc:
redis-cli --help
To start Redis in interactive mode so you can type different commands and see their replies.
$ redis-cli redis 127.0.0.1:6379> ping PONG redis 127.0.0.1:6379> set mykey somevalue OK redis 127.0.0.1:6379> get mykey "somevalue"
If able set values, then Redis is working properly and your are ready to next step
Connecting And Other
Command | Redis | Node.js |
---|---|---|
CREATE CONNECTION |
redis-cli |
db = redis.createClient(); |
CLOSE CONNECTION |
exit |
db.end |
FLUSH DATABASE |
FLUSHALL |
db.flushdb(function (err, succes) { ... } |
Command | Redis | Node.js |
---|---|---|
SET Single Attribute |
HSET myhash field1 "Hello" |
db.hset('cache:review:1', {id: '1', title: 'some title'}, function(err, args){..}) |
GET Singe Attributes |
HGET myhash field1 |
??? |
SET Multiple Attributes |
HMSET myhash field1 "Hello" field2 "World" |
db.hmset('review:2', this, function(err, args){..}) |
GET Multiple Attributes |
HMGET myhash field1 field2 |
db.hmget('cache:review:6', 'remote', 'local', function(err, args){..}) |
LIST ALL attribute = values |
HGETALL myhash |
db.hgetall('review:3', function(err,review){..} |
LIST ALL ONLY values |
HVALS myhash |
??? |
DELETE |
HDEL myhash field2 |
db.del('review:2') |
Command | Redis | Node.js |
---|---|---|
ADD |
ZADD myzset 1 "one" |
db.zadd('myzset', 1, "one", function(err, args){ if(err) {...}); |
LIST |
ZRANGE myzset 0 -1 |
db.zrange('reviews', from, to, callBackFunc) |
DELETE |
ZREM myzset "two" |
db.zrem('Å—eviews', this.id) |