Advertisement
In the previous blog on Redis, We learned about how to install Redis on Windows 10. This time we are going to take a look at the Redis Get and Set Commands.
Get and Set Commands are the basic commands in Redis used to Set and Get the Key-Value Data.
Start the Redis-server and let’s go for Redis’s journey.
Redis Set
The First command is the Set Command. Let’s first add some keys so that we can access them using the get command.
set key value
> 127.0.0.1:6379> set name "Divyanshu Shekhar"
OK
> 127.0.0.1:6379> set marks 90
OK
Redis Set Key with an expiry
Redis has a setex command that helps us to set a key with expiry in seconds.
setex key expiry(in seconds) value
> 127.0.0.1:6379> setex name 20 "DS"
OK
The above commands set the name key with the value “DS” for 20 seconds. We can also know how much time is left for the expiry of the key using the TTL command.
ttl key
> 127.0.0.1:6379> ttl name
(integer) 17
> 127.0.0.1:6379> ttl name
(integer) 8
> 127.0.0.1:6379> ttl name
(integer) 1
> 127.0.0.1:6379> ttl name
(integer) -2
> 127.0.0.1:6379> get name
(nil)
> 127.0.0.1:6379> keys *
(empty list or set)
Redis Set Expiry in Milliseconds
Use the psetex command to set the expiry of a key in milliseconds.
> 127.0.0.1:6379> psetex subject 10000 "CS"
OK
> 127.0.0.1:6379> ttl subject
(integer) 1
> 127.0.0.1:6379> ttl subject
(integer) -2
> 127.0.0.1:6379> get subject
(nil)
Redis Set Multiple Key Value
We can also set multiple key values in a single command using the mset command.
mset key value [key value ...]
> 127.0.0.1:6379> keys *
(empty list or set)
> 127.0.0.1:6379> mset name1 "DS" name2 "AS" name3 "SR"
OK
> 127.0.0.1:6379> keys *
1) "name3"
2) "name1"
3) "name2"
Redis Get
The Get command is used to retrieve value of a key.
get key
> 127.0.0.1:6379> get name
"Divyanshu Shekhar"
> 127.0.0.1:6379> get marks
"90"
Redis Overwrite Value
The key’s value can be set as many times as you want. The last set value will be taken as the current value of the key.
> 127.0.0.1:6379> set name "Divyanshu Shekhar"
OK
> 127.0.0.1:6379> get name
"Divyanshu Shekhar"
> 127.0.0.1:6379> set name "Hritul Priya"
OK
> 127.0.0.1:6379> get name
"Hritul Priya"
Redis Setnx
This may lead to a change in the value of a key that we don’t want. There is another command is Redis i.e setnx which checks whether the value of the key is already set or not.
setnx key value
> 127.0.0.1:6379> set name "DS"
OK
> 127.0.0.1:6379> setnx name "HP"
(integer) 0
> 127.0.0.1:6379> get name
"DS"
When we use the already set key with setnx it returns integer 0 which means no change is done in the database, and thus don’t overwrite the key.
Setnx command in Redis sets the value of a key that is not present in the database. Example:
> 127.0.0.1:6379> setnx anothervalue 10
(integer) 1
> 127.0.0.1:6379> get anothervalue
"10"
Redis Get all Keys
We can also get all the keys that we have set.
keys *
> 127.0.0.1:6379> keys *
1) "marks"
2) "name"
The last set key will be at the top.
Delete a key in Redis
There is also a delete command in Redis to delete the key value.
del key
> 127.0.0.1:6379> del marks
(integer) 1
> 127.0.0.1:6379> keys *
1) "name"
Deleting all keys in Redis – flushall
This command deletes all the keys from the database.
> 127.0.0.1:6379> keys *
1) "name"
2) "math"
3) "cs"
> 127.0.0.1:6379> flushall
OK
> 127.0.0.1:6379> keys *
(empty list or set)
Redis Length of Value
Get the length of the value using the strlen command in Redis.
strlen key
> 127.0.0.1:6379> strlen name
(integer) 2
> 127.0.0.1:6379> set name "Divyanshu Shekhar"
OK
> 127.0.0.1:6379> strlen name
(integer) 17
Redis Increment and Decrement
We can also increment and decrement the value of a key.
> 127.0.0.1:6379> set num 17
OK
> 127.0.0.1:6379> incr num
(integer) 18
> 127.0.0.1:6379> decr num
(integer) 17
The values can be incremented and decremented by any number of values using the incrby and decrby value.
> 127.0.0.1:6379> incrby num 10
(integer) 27
> 127.0.0.1:6379> decrby num 5
(integer) 22
These commands only work in the numerical value.
> 127.0.0.1:6379> set name "DS"
OK
> 127.0.0.1:6379> incr name
(error) ERR value is not an integer or out of range
Redis Append
This command is used to append a value to a pre-existing key.
append key value
> 127.0.0.1:6379> set name "Divyanshu"
OK
> 127.0.0.1:6379> append name " Shekhar"
(integer) 17
> 127.0.0.1:6379> get name
"Divyanshu Shekhar"
Learn more about Redis Commands from the official documentation.