How Redis Support as a Cache for Application?

How to Redis as Cache for Applications
What is Cache: Cache is a process of saving data to memory which is frequently called by application. This may reduce the execution and data processing time of application and hence data will be processed very fast.

The necessity of cache:

Since technology is advancing too fast and time is one of the critical aspect related with application operations, this is where cache plays a vital role. Most frequently called data will be saved in cache and application will call it when it requires. Cache is not to be a persistent storage, applications have to update it on a frequent basis. Using cache, application will run very fast and it will be stable. Cache can be used in Mobile apps, Desktop apps, websites etc.

Why use redis as cache when there is other memcache methods which are available?

For simple key-value pairs, memcached is more memory efficient than Redis. If you use Redis hashes, it is more memory efficient and persistence, If you are using Memcached, data could be lost with a restart and rebuilding cache is a costly process.
Soruce : Quora

Illustration: Using redis as cache

We will look how redis is working cache in node backend. Here, I’m going to use nodejs with redis to build cache program.


We need check and install prerequisites before building the application. Let’s do that, we have already installed linux and we will now install prerequisites over it.

Install Nodejs & NPM

sudo apt-get update
sudo apt-get install nodejs

Soruce : Digital Ocean

Install Redis
You need to perform some processes while installing Redis on Ubuntu.

Use command line prompt for prior set of development stage.
Create folder named `node-redis-cache:`

$ mkdir node-redis-cache
$ cd node-redis-cache

Install Node modules in project folder:

$ npm install express --save
$ npm install redis --save

Create app.js in project folder:

$ touch app.js

Open app.js in text editor:

var express = require('express');
var app = express();
var redis = require("redis"),
    client = redis.createClient();
//Express call
app.get('/', function (req, res) {
	//Checking for key exist
	client.exists('some_key', function(err, exist) {
		if(err) console.log(err);
		if(exist) {
			//Get cached data
			client.get('some_key', function(err, result) {
				res.send('Cached data! :- some_key : ' + result);
		} else {
			//Set data to redis
			client.set('some_key','some_value', redis.print);
			//Set expire for key
			res.send('Not cached! :- some_key : some_value');
//Server running on portal
var server = app.listen(3000, function () {
  var host = server.address().address;
  var port = server.address().port;

  console.log('Node redis cache app listening at http://%s:%s', host, port);

To download this sample application, Please follow the GITHub Link
Languages supported by Redis
Redis support many languages to implement cache feature.

ActionScript, Bash, C, C#, C++, Clojure, Common, Lisp, Crystal, D, Dart, Elixir, emacs, lisp, Erlang, Fancy, gawk, GNU, Prolog, Go, Haskell, Haxe, Io, Java, Javascript, Julia, Lua, Matlab, Nim, Node.js, Objective-C, OCaml, Pascal, Perl, PHP, Pure, Data, Python, R, Racket, Rebol, Ruby, Rust, Scala, Scheme, Smalltalk, Swift, Tcl, VB, VCL

Soruce : redis

Know More About This Topic from our Techies

Latest Post