Using Redis with Node.js


Using Redis with Node.js

Redis is a fast and efficient in-memory key-value store. It is also known as a data structure server, as the keys can contain strings, lists, sets, hashes and other data structures. If you are using Node.js, you can use the node_redis module to interact with Redis. This tutorial explains various ways of interacting with Redis from a Node.js app using the node_redis library.

Installing node_redis

node_redis, as you may have guessed, is the Redis client for Node.js. You can install it via npm using the following command.

[code]
npm install redis
[/code]

Getting Started

Once you have installed node_redis module you are good to go. Let’s create a simple file, app.js, and see how to connect with Redis from Node.js.

app.js

[js]
var redis = require(‘redis’);
var client = redis.createClient(); //creates a new client
[/js]

By default, redis.createClient() will use 127.0.0.1 and 6379 as the hostname and port respectively. If you have a different host/port you can supply them as following:

[js]
var client = redis.createClient(port, host);
[/js]

Now, you can perform some action once a connection has been established. Basically, you just need to listen for connect events as shown below.

[js]
client.on(‘connect’, function() {
console.log(‘connected’);
});
[/js]

So, the following snippet goes into app.js:

[js]
var redis = require(‘redis’);
var client = redis.createClient();

client.on(‘connect’, function() {
console.log(‘connected’);
});
[/js]

Now, type node app in the terminal to run the app. Make sure your Redis server is up and running before running this snippet.

Storing Key-Value Pairs

Now that you know how to connect with Redis from Node.js, let’s see how to store key-value pairs in Redis storage.

Storing Strings

All the Redis commands are exposed as different functions on the client object. To store a simple string use the following syntax:

[js]
client.set(‘framework’, ‘AngularJS’);
[/js]

Or

[js]
client.set([‘framework’, ‘AngularJS’]);
[/js]

The above snippets store a simple string AngularJS against the key framework. You should note that both the snippets do the same thing. The only difference is that the first one passes a variable number of arguments while the later passes an args array to client.set() function. You can also pass an optional callback to get a notification when the operation is complete:

[js]
client.set(‘framework’, ‘AngularJS’, function(err, reply) {
console.log(reply);
});
[/js]

Continue reading
Using Redis with Node.js
on SitePoint.

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Using Redis with Node.js

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.