DEV Community

vinayak
vinayak

Posted on • Originally published at itsvinayak.hashnode.dev on

Redis For Javascript

Image description

Introduction to Redis

Redis is an open-source, in-memory, and single-threaded database used for cache, message broker, and streaming engine. it's the most popular key-value data store. Which provides data structures such as strings, hashes, lists, sets, sorted sets, bitmaps, and more.

Redis was started in early 2009 by an Italian developer named Salvatore Sanfilippo. it is written in C and licensed under BSD.

Install Redis

  • Install on Ubuntu

In ubuntu, we can install Redis using the official packages.redis.io APT repository.

curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg

echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list

sudo apt-get update
sudo apt-get install redis

Enter fullscreen mode Exit fullscreen mode
  • Install on Mac

In Mac, we can use the brew package manager

brew install redis

Enter fullscreen mode Exit fullscreen mode

Redis for Javascript

There are several packages in the Javascript world that will allow us to connect with Redis, To name few of the packages are :

  • Node-Redis
  • ioredis
  • node-resque
  • connect-redis

We will use Node-Redis for now. one of the benefits of the node-Redis client is that it automatically queues all commands we send before the connection is ready.

To install Node-Redis -

npm install redis

Enter fullscreen mode Exit fullscreen mode

We have our Node-Redis package installed, we can import it into our project.

const redis = require('redis');

Enter fullscreen mode Exit fullscreen mode

For connecting to Redis, we need a client for creating a Redis client we will use the createClient function from Redis, Which takes an object of Redis Host and Port

const client = redis.createClient(
  {
    host: process.env.REDIS_HOST,
    port: process.env.REDIS_PORT
  }
);

Enter fullscreen mode Exit fullscreen mode

if Redis is installed on the local system, the Redis host will be localhost and the Redis port will be 6379.

Now that client is created, we can connect to Redis using the connect function.

client.on('connect', () => {
  console.log('connected to redis');
});
client.on('error', (err) => {
  console.log('error: ', err);
});

client.connect();

Enter fullscreen mode Exit fullscreen mode

Here client.on is used to test whether Redis is connected or not.

Redis: Hello word

For writing this simple Hello word problem, we will use the GET and SET commands of Redis. Other Redis commands

SET command - The SET command puts a key and a value in the Redis database. if a key already exists in the database, it overwrites the existing value irrespective of its type. it returns 'OK' if the key and value are successfully added to the Redis database.

Redis command

SET mykey "Hello"

Enter fullscreen mode Exit fullscreen mode

javascript code

client.set('name', 'vinayak').then((res) => {
  console.log('res: ', res);
}).catch((err) => {
  console.log('err: ', err);
});

Enter fullscreen mode Exit fullscreen mode

OUTPUT

res: OK

Enter fullscreen mode Exit fullscreen mode

GET command - The GET command retrieves a value based on a given key and returns nil if the key doesn't exist.

GET mykey

Enter fullscreen mode Exit fullscreen mode

javascript code

client.get('name').then((reply) => {
  console.log(reply);
});

Enter fullscreen mode Exit fullscreen mode

OUTPUT

vinayak

Enter fullscreen mode Exit fullscreen mode

With these two commands, we will write our first javascript program

const redis = require('redis');
require('dotenv').config();

const client = redis.createClient(
  {
    host: process.env.REDIS_HOST,
    port: process.env.REDIS_PORT
  }
);

client.on('connect', () => {
  console.log('connected to redis');
});
client.on('error', (err) => {
  console.log('error: ', err);
});

client.connect();

client.set('name', 'vinayak').then((res) => {
  console.log('res: ', res);
}).catch((err) => {
  console.log('err: ', err);
});

client.get('name').then((reply) => {
  console.log(reply);
});
client.quit();

Enter fullscreen mode Exit fullscreen mode

OUTPUT

connected to redis
res: OK
vinayak

Enter fullscreen mode Exit fullscreen mode

Top comments (0)