- [Tutor] We will now use our Maxcoin application again,
and we will implement what we did before
with MongoDB now with Redis.
For that I open Maxcoin and here the main Main-js file,
and please also make sure
that you are in the Maxcoin directory on the terminal.
Let me will first just copy this mongoBackend require
and do some renaming, so I call it RedisBackend
equals require, and I will require the RedisaBackend
that exists all ready,
and then I will rename this run function into runMongo,
so that we can reuse it later again,
but I will also add a new function, runRedis,
and it will copy paste runMongo for that simply
and rename it to runRedis.
And of course, here we have to now
make an instance of RedisBackend and call RedisBackend max.
Now, we also of course, have to call runRedis
instead of run, everything else can stay the same,
right now runMongo is unused.
Now I head over to services and to the RedisBackend,
and that's for MongoDB and now also need some Node.js driver
that can communicate with the Ready Server.
And there are two modules available.
The first is NodeRedis, and the second is ioredis.
I will use ioredis for this course
because it's a little bit more modern,
for instance, it supports promises while NodeRedis
does not support them as of now.
So in my terminal, I will now npm install ioredis,
and once this is done, I can do const Redis uppercase,
in this case equals require ioredis.
And in the constructor,
I can all ready treat the member variable,
this client and it equals now.
For the connect function, that's pretty simple,
I can now just add this.client equals new Redis,
and the pass in my port, and I just have to do that
because we are not using the default port 7379,
and then I return the client.
This function does not even have to be async
because under the hood, new Redis,
we'll just return the client object
and the actually connection will take place
the first time we do a comment on the database.
So the connection is created lazily.
Disconnecting from Redis is easy,
so I return this.client.disconnect
and next we will implement the insert function,
and for that, we will, again,
as for MongoDP, do a const data equals await
this.coinAPI.fetch.
And then we will create an empty array values.
Again, we have to iterate over the object entries
and there we want to iterate over data.bpi.
And for each of those entries,
we will now fill this value array
and for that we do a values.push.
And now, if you remember the element at index zero
of entry is the date and the element at index one
is the course.
And for Redis now, we first have to pass in the course,
so it's entries one, and a second argument we passing
the date because Redis expect for our use case here
a topple, and the first element of this topple
has to be numeric.
Then we will now do our first real Redis command
and this is return this.client.
and we will use zadd because this adds elements
to a sorted set.
A set as you might know, is the list of elements,
but there are no duplicates and a sorted set is a set
where all these elements are also sorted.
The first element is now the actually key
under which we want to store this,
and I call this key maxcoin column values.
And the second argument we can all ready pass in the values.
So this will insert a value array into a sorted set.
Next, we have to implement our getMax method,
and for that, I return this client,
and now I'm using zrange,
and zrange will give me a range of elements
and I can define what is the first
and what is the last element.
And for that, I first have to, again
hit the key under which I want to look,
so it's maxcoin values,
and now it's arguments I can define the start index
and the end index of my Z that the want to return,
and the index starts with zero but the index minus one
points to the last element.
This means if I use minus one is start element.
The start element is the last element.
And if I use minus one, again, is to stop element,
it will just return this one element.
And async is a sorted Z, which is sorted by the course.
This means that they will get the max course here.
It's last argument, I have to also add now with scores,
because in Redis terms the course is a score.
Think of a ranking or a leaderboard.
And if I would not provide with scores,
the result would not contain the actual course.
Now in the end I also have to implement the max function,
and for that, I head over to MongoBackend,
and I will just copy this max function from there.
So from line 51 to line 76,
the return statement is not needed.
And back in RedisBackend,
I will paste this into my max function,
and of course I have to do a few changes here.
So first of all, I have to rename of course,
MongoDB to Redis here, where it's camel case,
So I'm just copy pasting it in.
And with that being done, I can now mark mongoDB
here in this consult time statements
to a control and comment F,
and then I can search and replace for that.
So with this little icon for replace all,
I can change that and now all this Redis.
Of course, we have to do a little bit more here
for the client.
This is now not anymore in await.
So here in line 42, I remove the await.
The isConnected method on client
is of course, specific to MongoDB.
So in this case, we will not just check
if the client is truthy, then let's crawl a little bit down.
The insert can stay the same,
but Redis will return the number of inserted record
as insert results.
So we can remove this .results at the end here in line 55.
And then in line 59, we will change doc to result
because it's not so much document, but more the result.
And then if the very end, I can return result.
So this should now work.
Let's try this out.
So I run in nmp start,
and we see that we get actually really
the maximum value back, we get the data back,
and when we look at the timings,
we see that this is really, really fast.
So the find operation just took roughly four milliseconds.
So that's super fast.
And if I execute that now again,
and we see that the incident document is now zero
because as I said before,
this is a set and this cannot contain the applicate,
so no documents where edit.
Now let's head real quick into our browser
and let's look into what now Redis insights tells us.
Maybe you have to just reload the page,
but after that you should see not this entry maxcoin values,
and if I click into that, let's see the inserted data.
So as you saw in this example,
using Redis might feel a little bit different
than using a regular database,
but on the other hand it's really super fast.
In the following videos,
we will now use Redis to extend our shopping application.