So in this session, we are going to talk about various data lives which are available in Redis and also
some of the operations which are available to perform by using these different Data Types.
Okay.
So let's begin.
I'm going to connect to Redis by using Rediscli and if you remember the service is running on Port
6 3 7 0 in this case.
So we need to specify the port explicitly
and now let's begin with String Data Type. So in a String Data Type or a String.
Key Value Pair, usually just write Set Command and we provide the key name and also we provide the value
name.
So let's say, our key is a product which is Chair
And also we want to set the price as a value for these key type.
Okay.
So typically we run command like Set and then space and then we provide the Key Name then space
and also we provide the Value.
So once you do this, the value has been set, which means that this value or key value pair has been written
to Redis. Now to get the same value back you use Get command and also you provided the key name.
So if I do Get Chair, I'm going to get the value back.
Okay.
So similarly you can continue performing this option with different key value pairs
and you will get back the response, you know, depending on the key information you want to get back.
Now let's say, if you want to see all the available key types. For that you can use keys and specify the
pattern.
In this case I'm going to give *
So it is going to be me all the keys which are available in Redis.
So right now we see, we only have 2 keys available and now let's say if you want to delete one of the
keys.
So the command to do that is del space followed by the key and if you want to delete multiple keys
you can also specify Multiple Keys.
But in this case let's say, I want to delete just one key. So I write delete chair and it is giving us
the value of 1, which means the operation is successful.
So one of the Keys has been removed.
Now if I do Keys * again. This time I see only 1 value which is Table, because chair has been
removed.
Now let's see whether Redis is Key Sensitive or not.
Okay.
So I'm setting a value of chair with value 100 and then we'll set value of chair again, but this type
I'll keep C in capitals and remaining in lowercase and we type the same amount and this time I'm going
to keep everything in uppercase and I'm going to hit enter.
Now let's see how many different keys we have.
Now you see in this case, different keys we have are more.
So basically the point which I wanted to tell you, that Redis is Key Sensitive. So there is a difference
between this chair and the way this chair is treated in Redis. So similarly when
you are performing a delete operation.
And if I give
CHAIR in uppercase. it is only going to delete one of the key, which is matching this value.
Okay.
So now if I see keys *, I'm going to get the remaining values and one of the value, one of the
Key Value, which has been removed is the CHAIR, which was in uppercase.
Now let's say, you have some kind of you know, counter or Sequence ID number, which you want to store
and also with each operation you want to increment the value of that counter. So you can do that by using
String key value pair as well.
So the way you do is, going to be same.
Let me clear the screen first and I'm going to set a counter or it can be a Sequence ID. let's call
it Sequence ID and let's say we start the value with 1
Okay.
Now if I see the current value of Sequence ID, it is set to 1 and now let's say, with each operation
or whenever I query Redis, I want to increment the value by 1.
So for that we have a command, which is called incr, Increment and also you provide the keys which
you want to increment.
So in the response, we have got value of 2 which means the existing value of Sequence ID was 1 and
now it has incremented it with 2.
And if you run the same operation again.This time you're going to get value of 3, which means again
it has been incremented by 1
Okay.
So that is the way you can run Increment or Counters or Sequencer.
However, please remember whenever you do get sequence underscore id, you are always going to get the
latest value of sequence ID
Okay so all of these 3 values have not been set.
So each time, this Increment command, it looks for the current value and then it adds it, adds the current
value with 1 and then it writes the same value back to Redis.
That's why you always get the latest Counter Information.
So if we do Key *
total number of value which we see are 4. Other way to count the total number of keys or total number of
records whatever you want to say it.
So available in the Redis right now is, using dbsize command.
So if I write dbsize , hit enter. I get a value of 4, which means right now Redis is storing total 4
keys
okay.
So right now whatever operations, which we have performed these are based on single key value pair
But let's say we want to increase the performance and we want to write multiple operations in one set
or in one go.
So we can do that. The way to do that is by using mset command.
So mset stands for Multiple Set.
So basically you are setting multiple values in one command. So you can set up different product prices.
So let me set something like this.
And then there is another product. Let's set it to 200 and then we can set another key value pair.
So in this one I am setting actually 3 key value pairs, using mset command.
And once you hit enter, you get a response of
Okay.
Which means, the data has been inserted to Redis.
So if you do Keys *. You see 3 values or 3 items has been added.
So one was Sofa, the other one was TV.
And the third one was Bed. So all the three items has been added and by mistake
Sofa we wrote as Soft, so the same has been operated here. Okay, now let's say when you want to get Multiple
Values.
Okay.
So basically you want to ask Redis to provide the multiple, you know, you are going to request Redis
with multiple keys and you want the values back for all of those keys.
So the way to do that is using, mget command and let's say I want value of chair, I want value of TV,
I want value of latest sequence ID. So once you hit enter,you are going to get the values of all of
these three items. OK.
So that's a way to perform mset or mget operation.
And this is going to be faster than Set operation.
The reason being is, you are actually requesting multiple items to Redis and Redis has to perform
this operation and have to respond you back only ones.
So this operation is faster than using Get or Set command.
Now there can be scenario, when you want to see whether a key already exist or not.
So in that kind of scenario, you can use exists command and with exists you need to provide the key name
and also you can provide Multiple Key Name.
So it is going to check whether all of those keys already exist or not.
So let's say before inserting to Redis, I want to see that whether those keys already exist.
So I can do something like exist and then I want to see whether any key with the name sequence_id
exist or not.
So once I hit enter. I'm going to get value of 1
So here 1 means value already exists.
Now let's say if I do the same with something called counter. So Counter is a key here and we want
to see whether this counter already exists in Redis or not.
So once we hit enter, we are getting a response of 0, which means it doesn't already exist.
Okay.
Let me clear the screen and let's again see the total keys we have available right now. So let's say
you have a scenario in which you want keys to Auto expire. You don't want keys to be deleting
manually or programmatically. You don't want to write those things. So Redis has a feature which is called
TTL, which is a time to live.
So you can set up TTL for any keys.
Now the way to do that, is so basically when you are using set command, so along with the set command,
you give key value pair. So let's say, it is a color which is Red.
And now you also pass EX, which denotes that you are setting an expiration for this key.
And let's say, here you need to provide the value in seconds. If you want to provide the value in milliseconds,
then you need to use a PX. If you want to provide value in seconds,
then you want to, or you need to use EX
Now let's say I want this value of Color to be set to Red, only for 60 seconds.
Okay.
So I'm going to write like this.
And once I hit enter, this value has been written.
And once you do Get color, you are going to get Red back.
But if the same command you try, after 60 seconds, the response which you are going to get is Nil
because what Redis is going to do is, it is going to delete this key after 60 seconds
okay let's check
now, we still have the value. So the Threshold still has not reached and also the same thing can
be actually done on, done through programmatically.
So you have like Python or using JAVA, you can write the data along with the Expiration Time.
okay.
Right now if I'm doing Keys * I can see the keys still exist.
So let's wait for some more time and then we will check the value again
okay.
Now if you see, when I type Keys * , we do not see Color.
And also if I type Get Color, we are getting Nil which means, this key has been expired. Okay.
So that is the way to use or set a key value pair along with a TTL.
So this is very good.
This is used more in the use cases, where you want some value to be existing only for some particular
time.
For example let's say, you you want a user to avail some, you know, benefits or some services ones in last
2 hours. So you can set a value or you can set a status flag which you can set it to expire every 2
hours.
Okay.
So if the same person tries to avail the same service again within 2 hours, then you are going to check
in Redis, that the value still exists.
It means the user has already availed the benefits.
And you are basically programmatically going to return or show the error message to the user.
And if it tries again with after 2 hours, then you should be able to check that the value doesn't exist in Redis
And then the user can be allowed to avail that service again. So similarly there can be another
use cases, like if you want to check Duplication, Data Duplication or File Duplication, then you can
use the same feature. Let's say, you want to see Duplication only within last 2 days or 3 days.
So you can use this, this kind of feature where the keys expire after two or three days.
Now let's say there are some existing keys and I want to set the expiration on those key.
So the way to do that is by using Expire
command.
So you provide Expire and you provide the name of the key.
So in this case we are providing
let's make it Bed and let's provide the total number of seconds. So let's again keep it 60 seconds.
So now this value or this key value pair is going to expire in 60 seconds.
So if I do Get command and I do Get space Bed, I'm going to get the price of Bed but after 60 seconds
if I do the same
command.
It is not,
It is going to give us response of null or nil, which means the key has been deleted.
Now let's say if there was a scenario, in which we wrongly set the expiration and actually we don't want
the key to expire.
So you can use persist
command and with the persist command you can also provide the Key Name. So i am providing the Key Name.
Now this value you know, whatever expiration which we had set has been reset and now this value is
or this key value pair is not going to be deleted.
OK.
And now this is going to be basically staying in Redis forever until someone is deleting it manually
or someone is setting expiry on this key.
So if I do Get Bed, I get the value back.
OK.
So these are the, you know, various options which we have available for strings types. Now let's explore about various
Lists option available in Redis. So i will clear the screen and let's also do Flush all. So Flush all
command is going to delete whatever number of keys which we had in Redis. it is a destructive
command.
So please use it with care. OK.
So now if I do dbsize, I'm going to get 0, which means we do not have anything stored in Redis right
now.
or if I do keys *, you see we get empty list in response which means we do not have
anything stored in Redis. Now let's begin with, working with Redis lists. So in Redis list you actually
perform Push Operations. So you can insert a value from left side or right side in a list. For the first
time, when you are creating a list, for the first time it doesn't matter whether you use R push command
or L push command. It is going to work the same way.
So let's say I want to write a value or a key value appear to Redis list and my list is called, let's
say Color and it is having different color names.
Let's say we have Red, we have Blue, we have White and once I hit enter, now three values has been inserted
to Redis and once we do Keys * we can see we have a key which is called Color and if we do,
so if you run Get command with this key, it is not going to work because when you are actually storing
a key, you need to use L range to get the value back.
So right now we have stored 3 Values and remember Array always starts with 0. So this value is
position 0.
And this value is at position 1
And this Value is stored at position 2, in an Array
So if I specify L range starting from 0 and let's say I want to see all the values which is available
in or which are stored in this key.
So I need to type L range. Specify the key name. Specify the starting Array position.
In this case it is going to be 0, because I want to see from beginning and let's say I want to see
everything.
So whatever value which
this list has. I want to see all the values.
So in that case you specify -1
So what this going to do is, it is going to list all the values which is stored in key, which has a name
of Color. Okay.
Now if you want to see only top 2 values. So you can do starting from 0 until 2.
Okay.
This is giving us 3 values because index, remember index starts from 0 so 0 to 2 is going to give
us 3 values.
So if you want to see top 2, you need to provide from 0 until 1.
So as you see now we are getting only top 2 values
now once again let's see all the values.
And now let's see how the L push or R push operation works.
So when I do L push, values are going to be inserted at the top.
Okay.
So if I do L push color and I type Black. Okay I want to set the value of black in this color list the
black is going to be inserted at the top because we are inserting the value from the left.
So you can imagine a list which is actually horizontal and left is actually the top side and right is
at the bottom side.
So once I do this and if I run L range command again.
Now you see we have got the List and Black was inserted at the top.
Okay you are at the top of the list. Now let's say if I use R push and I give a new color, which is let's
say called Magenta.
OK.
So in this case this value is going to be inserted after Red.
Okay.
And also whenever you use L push or R push command,
It tells you the total number of values, which are available in this list. So it has returned us 5
which means current size of list is 5. Now let's run L range again.
And now we see the new value which we had inserted in this list which was Magenta and since we used
R push, this value was inserted at the bottom.
So again depending on the use case, you can use R push or L push and now let's say if I want to always
see top 3 values. I can run, so I just want to see, Yeah, you need to run 0 space 2
So starting index is 0 in last index is 2
So it is always going to give you top 3 values
and let's say if you want to know the total length of the List. You can use a Llen command and you need
to provide the key name.In this case key name is Color.
So once I hit enter, I get a value of 5 which means that right now I have 5 values available in this
keys
okay.
So that's about Singular Operation.
Now let's say we want to provide Multiple Key Value Pairs to L push, so you can do that.
so you type L push space key space all the values
so let's say I have Ping, then I have White,
then we have Dark Brown.
So once I hit enter.
Right now the size of the list has changed from 5 to 8.
So we see total number of elements which are available is 8
And if I do L range color starting from 0 ending with -1, we are going to get all the values.
okay.
Also please notice since we used L push command, so Pink was inserted first.
So our Existing List was until here, until Black and then we inserted Pink.
So that's why Pink is at position this.
And after that White was inserted and then Dark Brown was inserted.
That's why Dark Brown is, at the top of the list.
And now let's see if you want to remove one of the values.
You can do that as well in Lists
So the way to do that is you use a pop command.
So use L pop to pop a value from left.
OK.
So if I do L pop and i provide the key. I don't need to provide any value here.
So the only thing is I can either remove the value from left side or right side.
So I'm going to use L pop, to remove the value from left side.
And I'm going to use R pop to remove the value from right side.
So if we do L pop and provide the name of the key and hit enter. It is returning as Dark Brown which means,
it has removed Dark Brown from the list and we can verify the same by doing L range Color 0
-1
OK.
We see now List doesn't contain Dark Brown and you can perform this operation multiple times. So each
time it is going to pop out, one of the elements from the left.
So if I do L pop color again, it has removed White.
And if I do L pop again, it has removed Pink color. So if I do L range color 0 space
-1, this time we only get 5 items
now R pop is also very similar.
So you use R pop, you provide the name of the key and you hit enter.
Now in this case you see, we get value of Magenta because that was at the right side of the list and
now this has been removed from the list.
And now if we see the total amount of available values, we see only 4 values and Magenta has been removed
from the right side of the list
and now let's
put some more values back to this color.
So I'm going to use L push
Okay and let's see the values again
okay.
So actually, I did not specify the name.
So what it has done is, it has assumed Dark Brown as a key and then it has inserted the value of Magenta and Pink
to this list.
So that's not what we wanted.
So the correct way to do that is, L push, give the name of the key which is Color in our case
and then specify all the values.
Now you see it has changed to 7
and now we see total we have value of 7.
Now let's say we have a scenario that we always just want to keep you know, only top 3 colors regardless
of whatever the values being inserted.
So for that one you can use Ltrim Command.
So now the way to do, or the Syntax to use that is Ltrim and you specify the key name of the
list.
So in this case the name of the key is Color and we specify the starting position which is 0 and we specify
the ending position.
So if you want top 3, you are going to write from 0 until 2 because 0 is the first item in the
Array
And 2 is the third item in the Array.
So what this is going to do is, it is going to remove all the values and going to keep only the values
specified by you, in this list.
OK.
So once we hit enter, it says okay.
which means it has performed the operation.
If I do Lrange Color from 0 until -1.
Now we see our list has been reduced with only the top 3 items. OK.
So when you have you use case of you know, keeping only some certain range of items in the list, you can
use Ltrim command. So it will remove all the values you know, which is outside of that range.
So let's say if you want only items from fifth until tenth, so you could have set ltrim space color
and since you want fifth item. You need to give index position of 4
And since you want only until tenth item, you have to give index position of 9. Okay
So that's the way to get, from fifth item to 10th item
okay.
Let's clear the screen.
Let's connect
back to Redis cli
Okay now we are going to learn about Hashes. so Hashes are very simple and also in operations
It is kind of similar to Strings.
So what it does is, you type h set command to set a value or Key Value Pair.
So rather than usually in use, when you are performing strings data type operations, you are providing
Key and Value Pair.
However, when you are working with Hashes, you provide a Key and then you also provide a Field Value
Pair.
Okay.
So in this case let's say, we want all the product prices to be stored.
So product is going to be the Key in this case.
And then I need to provide all the Field and Value combination. So let's say, the field which I have
is Chair which is having value of 100 or price of 100
And then we have Table which is having price of 200.
We have TV which is having a price of 300.
Okay.
Something like this.
So once you hit enter, you get a response of 3 which means you have put 3 items or 3 field
values in this H set. Okay.
Now to get all the items,
you can use H Get
So you provide the name of the key and you provide the field which you want to see.
So in this case if I do Table, I'm going to get a value of 200
okay.
If I do TV, I'm going to get a value of 300.
Now let's say if you want to see all the values. In that case rather than hget, you can use hmget
which is going to give you multiple fields back. So you specify the Key Name, which is Product in
this case and then you specify all the Field Names. So it is going to be Chair, Table and TV and then you
get 3 values in return.