Redis is a key-value store frequently used with Node.js. In this video, explore how to implement our bitcoin example with Redis.
- [Instructor] So, how would our Bitcoin example look like with Redis? To try that I already opened the MaxCoin project and here the file test.js and we will start will installing the Redis client, so I type npm install --save redis. Next we have to require it, so here right after the require line of the request module I add const redis equals require('redis').
Now let's scroll down a little bit to have all this Redis logic at one place. Now here after everything that had to do with MongoDB I will now add the logic for Redis and first, I will implement the function that inputs data into Redis, so I implement the function insertRedis and it should take a client, some data and a callback function to be executed when it's done.
Next, we will define some special array const values equals and then in square brackets we will create an array with one element and this one element is values. We do that because we will use a back operation to insert all our values into Redis as sorted Z and everything that is added to this array after that these are actually tuples that will be then inserted into Redis but we will see that in a second, so very similar to what we did before.
I will run object.keys on data and forEach element I will take the key. Now I will push data at the index of key, so that's the date into our values array and a second parameter I will also push the key.
What we get now is an array with the string values as first element and everything after that are tuples of a date and some key and this is added now to a so-called sorted Z, so I add client.zadd and here I add the values and the callback. Now let's proceed. We haven't even connected to Redis yet. We will do that now, so we'll add const redisCLient equals redis.createClient and I connect to port 7379 and next we will listen to the connect event of the redisClient redisClient.on('connect').
This means as soon as the client is connected, we will then execute the following callback, so let's define a callback here and here we will add the timing function console.time('redis') as we did before with MongoDb, so next we want to let the user know that we connected to Redis, so I add console.log('Successfully connected to redis') and now it's time to fetch the data from the API, so I fetch from the API, I get either an error object or the data back and in the callback of that I will now first check if there was an error, if error we will throw.
In any other case let's insert those documents into Redis, so I use insertRedis and I pass in the redisClient, then the data.bpi and then we have to provide the callback error, results and in the callback body we will first check if there was an error. We will throw in any other case, we will let the user know, console.log, that and in back ticks we successfully inserted $(results) because the results variable actually contains the number of inserted documents, key/value pairs into redis.
And what we are missing now is the query to find the maximum value here, so I add redisClient and there is the function, zrange, that can find the maximum value in a sorted Z, zrange from the collection values, minus one, minus one and we will use scores for that, withscores and we get either an error or a result.
Again, we will add some error checking if error throw error and otherwise we will console.log and this will be also in back ticks, Redis: The one month max value is $(result) and it's the index one of this result and it was reached on $(result) index zero because that's the date in this key value pair.
Let's close that here. Also, let's end this timing function here for Redis, so I add console.timeEnd('redis') and then let's close the Redis connection, redisClient.end. Now let's try this out, so I run node test.js and we get Redis. Oh, I have a typo here, let's use The one month's max value.
Let's try it again with proper English and here we see Redis: The one month max value was and it was reached on. So, it's the same as with MongoDB which is a good sign and we see that Redis in this scenario actually is even slightly slower than MongoDb but really that depends always a little bit on the scenario.
- Basics of relational and nonrelational databases
- Preparing your environment
- When to use document databases
- Inserting data into MongoDB
- Basic insert operations for MongoDB
- Querying data from MongoDB with Node.js
- Using key-value stores
- Using relational databases with Node.js
- Adding MySQL and Sequelize to a project