redis image

Redis- what is redis, Commands with examples

Objectives:

  1. what is redis?
  2. what is the use?
  3. setup redis on windows?
  4. 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

redis server start

now we will see some operations to store data in redis server from redis client

This is how by default redis-client will look

redis client start

Commands over data structures:

Strings:

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

set command

del:

to delete the key we use “del” command to delete the key

del command

append:

This command will helps to append the string to existing value

append command

mset:

set multiple key value using “mset-multiple set”

multiple set

mset followed by key and value pairs

you can get the value by specific key,

get command to get from multiple

lists:

Redis can store the list values, here we will see the key words to set the list

lpush,rpush:

  1. lpush- push from left
  2. rpush- push from right
lpush list

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,

lrange:

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

rpush and lrange

lindex:

If you want to get by index there is a command called “lindex – list index” , below screenshot will give you clear explanation

lindex command

get list index of the list which is a key and the index on that list to retrieve the value.

llen:

To find the length of the list we can use the command “llen- list length”

lpop,rpop:

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

lpop and rpop

lrem:

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

lrem command

so we will remove the puneeth from the above list using lrem

lrem negative command

lrem-command

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

rpoplpush:

we have a commond rpoplpush to remove the right most element of one list and add to left side of another list

rpoplpush

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:

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:

“SADD-set add” to add the element into the set

sadd

SMEMBERS:

“SMEMBERS- set members” to check all the values in the set

smembers

SREM:

“SREM- set remove” to remove the value present in the key

srem

SCARD:

set card is used to know number of elements in the set

scard

SPOP:

set pop to remove an element from the key, it will remove any element as set usually dont have the order

spop

HashMaps:

Redis Hashes are maps between the string fields and the string values. Hence, they are the perfect data type to represent objects.

Hmset:

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

hmset

Hgetall:

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

hgetall

Hget:

hget to get the single keys value from the hashmap key

hget

Hexists:

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

hexists

Hkeys:

hkeys to get all the keys present in the hashmap

hkeys

Hdel:

hdel to delete the key from the hashmap

hdel

Conclusion:

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

About the author

puneethrdy

View all posts