Redis stores the data as key value pair. Keys can be any binary value and it is represented as ASCII strings for easily understandable and readability purpose. Redis supports five different datatypes and anyone of the datatype can be represented as value.
Redis supports five different datatypes and they are given below. Each datatype can be used in Redis with different set of commands.
- Sorted Set
String is a datatype used in Redis to store the Key value pair. The value can be represented as String. String is a sequence of characters and binary safe in Redis.
set and get
The Set command is used to create a new key value pair data in Redis. Execute the below command on the Redis client to create a new value pair data.
Syntax set <keyname> <value> Example set employee John
The Get command is used to fetch the value by using key name. Execute the below command on the Redis client to fetch the value by using key name.
Syntax get <keyname> Example get employee
If you want to set the new value for existing key, then you can use the same Set command. The Set command doesn’t append the new value with existing one. It overrides the existing value with new one.
Example set employee Mike
If you want to set the values for the key like employee First Name and Last Name, then use the below command on the Redis client. The value must be provided within double quotes.
Syntax set <keyname> "<value>" Example set employee "John Mike"
naming a key
If you want to store the more than one employee details with unique ID, then use the key name like below.
Syntax set <keyname>:<uniqueID> value Example set employee:1 John set employee:2 Mike set employee:3 Lawrence
Execute the below command on the Redis client to fetch all available keys.
Setting up new key value pair data once again to see the new key on the list of available keys.
set student "James" get student
Now, execute the below command once again to view the list of available keys.
Use the below command, to fetch the list of available keys starts with sequence of characters.
Syntax keys <pattern> Example keys emp*
time to live
Use the below command to set the expire time for the key. The expire time must be provided by using seconds or milliseconds.
Syntax set <keyname> <value> ex <no:of:seconds> Example set employee:4 Kite ex 30
Use the below command to view the remaining time of the key after set the expire time. The command name is “Time To Live”. The negative value on the TTL represented key has expired. The NIL value will return, while fetching the value by using key after expired.
Syntax ttl <keyname> Example ttl employee:4
key not exists – nx
Use the below command to set key value pair data, if the key is not exists on the Redis database.
Syntax set <keyname> <value> NX Example set employee:4 Fidel NX
key exists – xx
Use the below command to set the key value pair data, if the key is already exists on the Redis database. If the Key is already not exists, then the Redis client will return “NIL”.
Syntax set <keyname> <value> XX Example set employee:4 Nathan XX set employee:5 Nathan XX
Use the below command to check the key is already exists or not on the Redis database.
Syntax exists <keyname> Example exists employee:4 exists employee:5
Execute the below command to append the value for existing key value pair data.
Syntax append <keyname> <newvalue> Example set employee:5 Vikram get employee:5 append employee:5 " Karthik" get employee:5
To find the length of the value by using key, execute the below command. The return value will be Integer.
Syntax strlen <keyname> Example strlen employee:5
incr and decr
Use the below command to Increment and Decrements the Integer value by using key. By default Increment increase the value by 1 and Decrements decreases the value by 1.
Syntax - Increment incr <keyname> incrby <keyname> <incrementvalue>
Syntax - Decrement decr <keyname> decrby <keyname> <decrementvalue> Example set testkey 1 incr testkey incrby testkey 20 decr testkey decrby testkey 10 get testkey
mset and mget
Use the below commands, to set and get the multiple key value pair by using single command.
Syntax mset <keyname1> <value1> <keyname2> <value2> mget <keyname1> mget <keyname1> <keyname2> Example mset firstkey firstvalue secondkey secondvalue mget firstkey mget firstkey secondkey
We have learned the basic Redis String commands.