In this video, Emmanuel Henri demonstrates what you can do with strings and the corresponding commands in the Redis cli.
- [Instructor] Strings serve as the basis of the Redis data sets, and in this video we'll explore how to work with them. So we've already done a couple of commands for strings. For example, we used the set, and as you can see whenever we start doing a command, it gives you some indication of what you can enter in here. So for example, for set, we can enter a key. So I've changed the name a couple of times, so let's set a new key. So let's set first name. And a first name we'll set Manny. If I get, which is the other command to get the key, first name, I will get Manny.
And the io Redis server that is running has a name, which should return Emmanuel. So this is how you set a key and then the string for that key. So this is how strings basically work. It's very simple. But there's other ways to work with strings. Let me show you a couple. So let's set an address, for example of 453, for example. You can actually increment that address by doing a command that's called incr for increment, and then the key, and it will increment that key by one automatically, and you can do that over and over and over.
You can also use another command, and by the way if you're looking for all these commands, eventually you can go back. There's a cheat sheet on the Redis site that you can find, and I'll show it to you later in one video, but you can do increment by, and then use the key, so the key is always the address, and then increment it by the number that you want, like so, so you can increment it by 100. And then we're at 554. You can also decrement the same way. By one, or decrement by, use the key, and then the number you want.
And then we're back to 453. So if you want to work with numbers, this is a good way. There's another thing that you can do. Let's say for example you want to do a first name. Or let's say for example a country. So let's set the country, but the country has a super long name with tons of strings that it wouldn't accept, so for example let's do North Africa. That's not a country, but it's part of a continent. Let's just use this for the example. If I would do this, it wouldn't work.
So when I hit Enter, syntax error. Why is that? Because it doesn't accept the actual composed name. It needs to be inside of double quotes in order to work, so whenever you want to enter composed names, things that it wouldn't accept, not a single string, then you use double quotes. So let's set the country North Africa. It's not a country, but again, just for the example, and then it would work, so if we get our country, then it would give me North Africa.
So this is very important. As we work with all kinds of data sets, and data types, inside of Redis, so you can use double quotes to enter composed names, long sentences, anything that is not a single string. Alright, so let's move to get set. So get set is basically setting something and then getting it back right away. So instead of doing the set get every single time, you could do get set and get a result right away. So let's go ahead and do get set for first name.
And then change it to Stephanie. And then you'll get Manny. Why is that? There's a reason. Because when we get the name, we actually get it before we set it, so when you so the get set, make sure that you understand that whatever value you're going get is the previous value, so you're going to get the previous value first, then set a new value for that key here. So now Stephanie has been replaced in the first name key, but before that it was Manny, so very important to know.
I did that on purpose. So another command that you can use is to set multiple values at once. So you could do mset and then enter a street. So seaward, then city ventura, and then country usa. And then let's do a zip code. And I'm just going to go on a hunch here, but I'm just going to do something 92101. I'm pretty sure that's not accurate.
And then enter a whole bunch of numbers. So I want to show you guys a string example with double quotes. And then this is all going to work. Then if you want to get them all at once, then you can do mget. And then these are the keys that I want to have, so I want to have the street. I want to have the city. I want to have the country, and I want to have the zip. And it's going to get me all the values that I just entered.
Another command that is really useful. Does something exist? And it's pretty simple like that. Is there something that exists in the street key? And if it does, then it will return an integer 1, which means true. If I delete, and this another command, the street, and then I check if it exists, then it's going to return 0, because it's no longer in data base.
So I've just shown you two commands at once, the exist, to make sure that if you want to validate that a key has some values, and then the delete key, if you want to delete something in the database. There's another that's really useful. And some of these will return in other data types, so if you're not quite familiar yet with those, again use the cheat sheet that I'm going to show you later on, or just revert back to the video and practice, and this is how you're going to get familiar with these commands.
So, one other command that I really like sometimes you may need to have a specific type of data that expires after a certain time, a password, or you know, some pictures, or whatever that you don't want to stay in the database for long, then what you could do is expire, and then enter the key that you want to disappear, zip, and then the number of seconds. Let's do five so I can actually show you live.
And then in five seconds if I do exist, zip, then oops, I actually made a typo here. So exist, zip, then it will tell me 0, because that data does no longer exist. So let's go even further and do a get zip, and I have nil, I have nothing, because it has expired. And you can actually do this in one command if you want, so you can actually set a zip, and let's set the zip to 90210, and then just add a whole bunch of numbers.
Again let's practice with strings, and I want to expire, so you expire by doing X, and then the number of seconds, so let's do ten seconds so I can actually show you that it exists before it actually expires, so let's do ten seconds, like so, and let's do a get zip before it actually expires. We have the zip. And let's get it again. Let's wait a little bit. It's probably ten seconds now, so let's get the zip, and I have nothing left. Okay? So now we've got a good understanding of the commands available to us in the Redis CLI.
Next, let's apply those within an io Redis implementation.
- Installing and setting up Redis
- Reviewing the basics of data types
- Setting up persistence and replication
- Exploring strings and lists
- The hash data structure
- Securing a Redis instance
- Publishing and subscribing with Redis