- what is redis?
- what is the use?
- setup redis on windows?
- Play with some commands over data structures
What is Redis?
Redis is a in- memory data store, where we can save the data which is processed. Redis supports good amount of data structures strings, lists, maps, sets, sorted lists etc.,
Redis stores the values as a key value pair, we will see some examples how it is going to store in this blog below. so basically we will use redis for cache the data.
Cache the data means to store the data which is processed or API calls response data etc., files, images all data that is stored is called Caching.
What is the use of Redis?
We can see above as it will store or cache the data and also it supports different types of data structures, we can reuse the data again without processing again.
so we can use this functionality when the parameters to process to get the data don’t change frequently, if it don’t change frequently, we can cache the data or store the data in some structure to reuse again, so here comes Redis server.
Setup Redis server on windows
you can download this windows version form here.
Download the zip and extract the folder, you can see 2 files in it redis.server and redis.client, open both the applications
By default Redis server run on localhost and port 6379
when you open both redis server and redis client we will see like this,
this is redis server , which wont stop it will accept the client connection
now we will see some operations to store data in redis server from redis client
This is how by default redis-client will look
Commands over data structures:
set and get:
Here in the below image we set the key value pair and get the value by key
for example: set is the command here, username is the key and puneeth is the value
next we can get the value by calling key using get command
to delete the key we use “del” command to delete the key
This command will helps to append the string to existing value
set multiple key value using “mset-multiple set”
mset followed by key and value pairs
you can get the value by specific key,
Redis can store the list values, here we will see the key words to set the list
- lpush- push from left
- rpush- push from right
here we first push from left in the key usernames and the value is puneeth
next again we push from left to the same key and the value is midhun, now you might figure it out the order of usernames is “midhun” and next “puneeth”
next we will try to add from right side and check the order,
to check the order we have a key to use command ” lrange- list range ” and next we need to set the key and the range of values to get “start and stop offset”, then we will get the values from the server
If you want to get by index there is a command called “lindex – list index” , below screenshot will give you clear explanation
get list index of the list which is a key and the index on that list to retrieve the value.
To find the length of the list we can use the command “llen- list length”
Remove the leftmost of the list we use “lpop-left pop” and to remove right most element of the list we use “rpop-right pop” and the output it gives the removed elements
To remove the elements from the list we can use “lrem- list remove”, I inserted some elements into the list, and you can see below to see the elements we used lrange
so we will remove the puneeth from the above list using lrem
usernames-list or key
1- no of occurences to remove
puneeth-value of the occurence to remove
so basically it will remove 1 “puneeth” from left
So, now we will do another example
Now we removed 2 occurences of “puneeth” from left and the output left in the list ,
if you want to remove from right side we can just add negative value to the no of occurences then it will remove the elements from right side
we have a commond rpoplpush to remove the right most element of one list and add to left side of another list
In the above example created the subjects list and used command rpoplpush , first it pop from username from right side and add to the left side of another list.
Sets is a data structure to store the unique values with no order, We we will see some commands that will operate the sets
“SADD-set add” to add the element into the set
“SMEMBERS- set members” to check all the values in the set
“SREM- set remove” to remove the value present in the key
set card is used to know number of elements in the set
set pop to remove an element from the key, it will remove any element as set usually dont have the order
Redis Hashes are maps between the string fields and the string values. Hence, they are the perfect data type to represent objects.
hmset is the mapping the key value pairs and store in the key. Here in the below example mixthings is hashmap key to store all key value pairs like one-1,two-2, machine learning-AI
hashmap get all command used to get all the key value pairs in the hashmap key, so for the above example we will get all the key value pairs from hashmap key
hget to get the single keys value from the hashmap key
hexists to let us know that key is present in the hashmap or not, if it gives output as 1 then present, 0-not present
hkeys to get all the keys present in the hashmap
hdel to delete the key from the hashmap
Here we have seen what is redis, what is the use of using this redis-server and redis-client , how to setup redis both client and server and workout on some basic commands,
In the next we are going to have a small example with nodejs API, how redis helps to fast the response
If you like this blog, please share to others, If you have any suggestions to improve in my blog, please write down in the comments section below