Showing posts with label Redis. Show all posts
Showing posts with label Redis. Show all posts

Wednesday, February 17, 2021

Nodejs Data Caching with Redis Part 3: Performance Testing

This testing is running on 12GB RAM Window PC. NodeJS, MongoDB and Redis are installed on standalone PC.

The test is querying 10,000 documents.

It takes 1005ms response time query from MongoDB Server.


40% faster where 594ms response time from Redis Cache.



Here is all about Redis Cache.

Are you interested in live scoreboard build with Redis Sorted Set and Socket Programming? Feel free to let me know, probably my next sharing.

Tuesday, February 16, 2021

Nodejs Data Caching with Redis Part 2: Ultimate Way

I have shown an example of a direct approach on how to use Redis on NodeJS in Part 1. The disadvantage of the direct approach is redundant code where you have to initial Redis Server and to check whether there is a cache whenever you use it.

In this part 2, I am going to centralise the intitial Redis Server and add new method in mongoose:

.cache() = Cache value into redis memory database.

clearCache() = Delete value from redis memory database.

You can download the code at https://github.com/htsiah/node-redis-mongodb

The Redis Code store in utils/useCacheUtil.js and the sample code at controllers/TeacherController.js

Wednesday, February 10, 2021

Nodejs Data Caching with Redis Part 1: Caching in Action

You have to install Redis, Another Redis Desktop Manager and NPM Redis for this example.

Install Redis on Linux

Install Redis on Window

Install free version Another Redis Desktop Manager to view the cache data. 

Install NPM Redis
npm install redis

This is how you connect redis in NodeJS:
const redis = require('redis')
const redisURL = 'redis://127.0.0.1:6379'
const client = redis.createClient(redisURL)

For basic value, you can use set and get method. The first parameter is the search key and second parameter value stored.

// Store 'hi' as search key, and value is 'there'.
client.set('hi','there');

// Output: there
client.get('hi', ( err, val) => { console.log(val) } )

Redis support hset where you can have 2 search keys. Generally, I think it is practical to use hset for mongodb where the search keys are collection name and search query.



You may refer example on CRUD in StudentController:

Create - http://localhost/api/student
- When there is new document, delete collection in the cache.
redisClient.del(StudentModel.collection.collectionName)


Read - http://localhost/api/student/6028c376ddf84b2bd0a6b6b3
- If there is cache data, return cache data.
      const cacheStudent = await redisClient.hget(
        StudentModel.collection.collectionName
        JSON.stringify(query.getQuery())
      )

      if (cacheStudent) {
        // Remember convert to javascript object
        console.log('Get from cache.')
        return res.json(JSON.parse(cacheStudent)); 
      }  
- If not, find the data in mongodb, store the data to redis and return the data.
      const student = await query.exec();

      redisClient.hset(
        StudentModel.collection.collectionName
        JSON.stringify(query.getQuery()), 
        JSON.stringify(student.toObject({ getters: false }))
      )

Update - http://localhost/api/student/6028c376ddf84b2bd0a6b6b3
- Same as create, delete the collection.

Delete - http://localhost/api/student/6028c376ddf84b2bd0a6b6b3
- Same as create, delete the collection.

The above is an example using collection as key. The different requirement may use different key.