Type set name "subham"
to set a key-value pair in the Redis server.
Type get name
to retrieve the value of the key from the Redis server.
Note: If you open your Redis stack in the browser, you will see the key and value set. You can also update the value from there. Just click on the key and update the value on the right side. The Best way to do this is
set user:1 "subham"
set user:2 "codexam"
set user:3 "xamcodexam"
set msg:1 "hello"
set msg:2 "hi"
set msg:3 "hey"
If you group on the redis-stack you will see the data like this
user
1: "subham"
2: "codexam"
3: "xamcodexam"
msg
1: "hello"
2: "hi"
3: "hey"
set if not exists(nx)
set user:1 "subham" nx
if you don't use nx, then it will overwrite the value
set user:1 "codexam"
set multiple values
mset user:1 "subham" user:2 "codexam" msg:1 "hello" msg:2 "hi"
get multiple values
mget user:1 user:2 msg:1 msg:2
1) "subham"
2) "codexam"
3) "hello"
4) "hi"
increment the value by 1
set user:1 10
incr user:1
(integer) 11
increment the value by 5
set user:1 10
incrby user:1 5
(integer) 15
Note: By default a single Redis string can be a maximum of 512MB in size.
get the value from the range(SUBSTRING)
set user:1 "subham"
getrange user:1 0 3
"subh"
set the value from the range(SUBSTRING)
set user:1 "subham"
setrange user:1 0 "codexam"
(integer) 7
get user:1
"codexam"
get the value from the range(SUBSTRING)
set user:1 "subham"
getrange user:1 0 3
"subh"
get the length of the value
set user:1 "subham"
strlen user:1
(integer) 6
append the value
set user:1 "subham"
append user:1 " codexam"
(integer) 14
get user:1
"subham codexam"
set the value with expiration time (in seconds)
setex user:1 10 "subham"
(integer) 1
get user:1
"subham"
get user:1
(nil)
set the value if the key doesn't exist
setnx user:1 "subham"
(integer) 1
get user:1
"subham"
setnx user:1 "codexam"
(integer) 0
get user:1
"subham"
set multiple values if the key doesn't exist
msetnx user:1 "subham" user:2 "codexam" msg:1 "hello" msg:2 "hi"
(integer) 1
get user:1
"subham"
get user:2
"codexam"
get msg:1
"hello"
get msg:2
"hi"
set the value from the range(SUBSTRING)
set user:1 "subham"
setrange user:1 0 "codexam"
(integer) 7
get user:1
"codexam"
get the value from the range(SUBSTRING)
set user:1 "subham"
getrange user:1 0 3
"subh"
get the length of the value
set user:1 "subham"
strlen user:1
(integer) 6
This command decreases the value of a key by 1. If the key does not exist, it is set to -1.
set user:1 10
decr user:1
(integer) 9
This command decreases the value of a key by the given number. If the key does not exist, it is set to negative the given number.
set user:1 10
decrby user:1 5
(integer) 5
This command sets the value and expiration in milliseconds of a key.
psetex user:1 10000 "subham"
(integer) 1
get user:1
"subham"
get user:1
(nil)