A SnapShot of Redis

Image for post
Image for post
Photo by Heather Ford on Unsplash

What is it?

Popular methods:

//app.jsvar redis = require('redis');
var client = redis.createClient(); //creates a new client
const PORT = process.env.PORT || 3000;
const REDIS_PORT = process.env.REDIS_PORT || 6379;
client.on('connect', function() {
console.log('connected');
});
//console: connected
//store simple string 
client.set('email', 'elfi@test.com');
//store string with callback
client.set('email', 'elfi@test.com', function(err, result) {
console.log(result);
});
//store hash table
client.hset("email", data.id, JSON.stringify(data));
//store hash with expiry time
client.hset(key, fieldname, result, "EX", 10);
//store list/array
client.rpush(['email', 'elfi@test.com', 'julia@test.com'], function(err, result) {
console.log(result); //prints 2 --> the len of the list
});
//"email" as the first item is the key
//store set (non duplicated list)
client.sadd(['email', 'elfi@test.com', 'julia@test.com'], function(err, result) {
console.log(result); // 2
});
//retrieve simple key-value
client.get('email', function(err, res) {
console.log(res);
});
//retrieve hash table
client.hgetall('email', function(err, result) {
console.log(result);
});
//retrieve list
client.lrange('email', 0, -1, function(err, result) {
console.log(result); // ['elfi@test.com', 'julia@test.com']
});
//retrieve set
client.smembers('email', 0, -1, function(err, result) {
console.log(result); // ['elfi@test.com', 'julia@test.com']
});
//check if a key-value pair existsclient.exists('key', function(err, result) {
if (result === 1) {
//note this will come back as 1 or 0
//1==> exist; 0 ==> not exist
console.log('exists');
} else {
console.log('doesn\'t exist');
}
});
//delete certain key-value
client.del(‘key’)
client.set(‘user’, 
JSON.stringify({ firstname: ‘elfi’, email: ‘elfi@test.com’})
)
client.get(‘user’, (err, val) => {
console.log(JSON.parse(val))
})

Common Use Cases

Example implementation of caching

Image for post
Image for post
app.get(“/api/blogs”, requireLogin, async (req, res) => {// 1. init Redis client
const redis = require(“redis”);
const redisUrl = “redis://127.0.0.1:6379”;
const client = redis.createClient(redisUrl);
const util = require(“util”);
client.get = util.promisify(client.get); //instead of callback //returns a promise//2. do we have cache data in redis?
const cachedBlogs = await client.get(req.user.id);
//3. if yes ==> return data
if (cachedBlogs) {
console.log(“serving from Redis”);
return res.send(JSON.parse(cachedBlogs));
}
//4. if no ==> query MongoDB, return response, and update redis
const blogs = await Blog.find({ _user: req.user.id });
console.log(“serving from Mongo”);
res.send(blogs);
client.set(req.user.id, JSON.stringify(blogs));
});

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store