Redis- String Data type

  • The Redis string is the simplest type of key value data type.
  • String is not the best term for this type because its hold the numeric values as well.
  • Internally, it will held the byte array.
  • A string value can't be bigger than 512 MB.
  • Redis string data type can be used for caching or storing the data into DB.



1. String
Create string key value: SET <key> <value>
Get String key value : GET<key>
List all keys: keys *
Check the type of data type: TYPE<key> <value>

pawan@LAPTOP-SG31RIME : /mnt/d/PAWAN$ redis-cli
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> set key1 helloworld
OK
127.0.0.1:6379> keys *
1) "key1"
127.0.0.1:6379> get key1
"helloworld"
127.0.0.1:6379>
127.0.0.1:6379> set key2 10
OK
127.0.0.1:6379> get key2
"10"
127.0.0.1:6379> type key1
string
127.0.0.1:6379> type key2
string


2. Set Integer and Incr/Decr the counting- 

Increase the count by 1 : INCR <key> 
Decrease the count by 1 : DECR  <key>
Increase the count by defined value : INCRBY <key> <value>
Decrease the count by defined value :DECRBY <key> <value>

pawan@LAPTOP-SG31RIME : /mnt/d/PAWAN$ redis-cli
127.0.0.1:6379> incr key2
(integer) 11
127. 0. 0.1 : 6379> get key2
"11"
127.0.0.1:6379>
127.0.0.1:6379> incr key2
(integer) 12
127.0.0.1:6379> incr key2
(integer) 13
127.0.0.1:6379> incr key2
(integer) 14
127.0.0.1:6379> incr key2
(integer) 15
127.0.0.1:6379> incr key2
(integer) 16
127.0.0.1: 6379> get key2
"16"
127.0.0.1:6379> decr key2
(integer) 15
127.0.0.1:6379> decr key2
(integer) 14
127.0.0.1:6379> decr key2
(integer) 13
127.0.0.1:6379> get key2
"13"
127.0.0.1:6379> incrby key2 10
(integer) 23
127.0.0.1:6379>
127.0.0.1:6379> get key2
"23"
127.0.0.1: 6379> decrby key2 5
(integer) 18
127.0.0.1:6379>
127.0.0.1:6379> get key2
"18"
127.0.0.1:6379>

3. Count using Float number
Create Float key value: SET <key> <value>
Check the type of data type: TYPE<key> <value>
Increase the count by defined value : INCRBY <key> <value>
Decrease the count by defined value :INCRBY <key> -<value>

pawan@LAPTOP-SG31RIME: /mnt/d/PAWAN$ redis-cli
127.0.0.1:6379> set k3 2.5
OK
127.0.0.1:6379> get k3
"2.5"
127.0.0.1:6379> type k3
string
127.0.0.1:6379> incrbyfloat k3 1
"3.5"
127.0.0.1:6379> incrbyfloat k3 25.
"28.5"
127.0.0.1:6379> incrbyfloat k3 5.1
"33.60000000000000142"
127.0.0.1:6379>
127.0.0.1:6379> incrbyfloat k3 -5.1
"28.5"
127.0.0.1:6379>
127.0.0.1:6379> incrbyfloat k3 -20
"8.5"


4. Create Multiple Key at time
Create multiple string key values: MSET <key1> <value1> <key2> <value2> <key3> <value3>
Get multiple string key values.: GET <key1>  <key2>  <key3>
List all keys: keys *

pawan@LAPTOP-SG31RIME:/mnt/d/PAWAN$ redis-cli
127.0.0.1:6379> MSET key1 10 key2 20 key3 30 key4 40
OK
127.0.0.1:6379>
127.0.0.1:6379> keys *
1) "key4"
2) "key3"
3) "key2"
4) "key1"
127.0.0.1:6379>
127.0.0.1:6379> mget key1 key4
1) "10"
2) "40"
127.0.0.1:6379>


Create multiple string key values: MSETNX<key1> <value1> <key2> <value2> <key3> <value3> // if not exists.. means if value of any key defined it won't overwrite whereas MSET will overwrite the value even though its exists.

pawan@LAPTOP-SG31RIME:/mnt/d/PAWAN$ redis-cli
127.0.0.1:6379> MSET key1 10 key2 20 key3 30 key4 40
OK
127.0.0.1:6379> get key1
"10"
127.0.0.1:6379>
127.0.0.1:6379> MSET key1 50 key2 20                                //overwrite the existing value now new value of key is 50
OK
127.0.0.1:6379> get key1
"50"
127.0.0.1:6379> MSETNX key1 60 key2 205D                      //didn't overwrite the existing value
(integer) 0
127.0.0.1:6379> get key1
"50"
127.0.0.1:6379>


5. GETSET: Get a current value of key and Set a new value to key.
GETSET <key> <value>

pawan@LAPTOP-SG31RIME:/mnt/d/PAWAN$ redis-cli
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> set k1 10
OK
127.0.0.1:6379> get k1
"10"
127.0.0.1:6379> getset k1 20
"10"
127.0.0.1:6379> getset k1 30
"20"
127.0.0.1:6379>

6. GETRANGE: get the value of string with particular range.
GETRANGE <key> <start> <end>

127.0.0.1:6379> set databasename redislearn
OK
127.0.0.1:6379> getrange databasename 0 1
"re"
127.0.0.1:6379> getrange databasename 0 5
"redisl"
127.0.0.1:6379> getrange databasename 0 10
"redislearn"
127.0.0.1:6379> getrange databasename -1 -5
""
127.0.0.1:6379> getrange databasename 5 9                 //range from left-right (0 is first char )
"learn"
127.0.0.1:6379> getrange databasename -5 -1              //range from right-left (-1 is last char )
"learn"


7. Redis provide feasibility to expire the key which we are creating using SETEX command.
SETNX <key> <expiretime>  <value> 


pawan@LAPTOP-SG31RIME:/mnt/d/PAWAN$ redis-cli
127.0.0.1:6379> set n1 10
OK
127.0.0.1:6379> expire n1 20
(integer) 1
127.0.0.1:6379> ttl n1                             // time to check the key expiry
(integer) 13
127.0.0.1:6379> ttl n1
(integer) 11
127.0.0.1:6379> ttl n1
(integer) 8
127.0.0.1:6379> ttl n1
(integer) 6
127.0.0.1:6379> ttl n1
(integer) 3
127.0.0.1:6379> ttl n1
(integer) 1
127.0.0.1:6379> ttl n1
(integer) -2
127.0.0.1:6379> get n1
(nil)
127.0.0.1:6379> setex n2 10 30        // combination of above two commands set + expire =setnx ( Time is in second)
OK
127.0.0.1:6379> ttl n2
(integer) 4
127.0.0.1:6379> ttl n2
(integer) 2
127.0.0.1:6379> ttl n2
(integer) 1
127.0.0.1:6379> ttl n2
(integer) -2
127.0.0.1:6379> get n2
(nil)
127.0.0.1:6379>