What is Redis?
Rdis is a database, most specifically it is NoSql database. Definitely not similar to other NoSql databases like Mongos and NoSql. Note that redis don't have idea of tables and documents. All data stored in key values pairs in JSON format. Other Databases run in disk but redis run/ work in RAM thats why it is incredibly fast. It run on top of other traditional databases. Fastly to get data from it.
This storage is not persistance storage mean dta lose if system crash or turn off, therefore is is use for caching.
Before start redis service in redis
Before start redis service set Linux Kernal overcommit memory to 1 by adding vm.overcommit_memory = 1 to /etc/sysctl.conf file.
Why we should do that: As we know redis is in memory data store. It primarily uses RAM to store data. Redis and other processes use RAM memory. We set vm.overcommit_memory = 1 to make sure Kernal not overcommit the memory by allocating memory to new program which required more memory than available memory.
In this scenario OOM (Out Of Memory) killer terminate the rest of processes.
We also restricting Kernal not to terminate the important services running and also stop swapping redis and other importance service to device i/o device, where performance of redis effected.
What is redis and how is use and purpose and how to use it all these things will be discuss later on first practice different data type in redis-cli
we all know redis store data in key value pair.
set command use to store data in redis
String
set name Adnan
"OK"
get name
"Adnan"
Update the existing value of key
127.0.0.1:6379> set name "Adnan Zaib"
OK
127.0.0.1:6379> get name
"Adnan Zaib"
Set multiple key values.
127.0.0.1:6379> mset profile laraveldeveloper company BrainTel
OK
127.0.0.1:6379> mget profile company
1) "laraveldeveloper"
2) "BrainTel"
get the length of key
127.0.0.1:6379> strlen profile
(integer) 16
We can also increment in integer key
127.0.0.1:6379> incr count
(integer) 2
127.0.0.1:6379> incrby count 10
(integer) 12
We can also decrement key by number
127.0.0.1:6379> decr count
(integer) 11
127.0.0.1:6379> decrby count 6
(integer) 5
We can set expiration time for any key value pair
We use TTL(Time to live) which define time, after that time the value will no longer exist in database.
set session adnan
expire session 20 // time in seconds
no check remaining expiry time.
ttl session // session is key name
after expire time this key return nil
we can do same in single command
127.0.0.1:6379> setex session 15 adnan
OK
List
we can create list to add multiple data in sigle list. A list can contain 2^23 -1 more than 4 billion elements.
Create list
lpush databases mysli
add another element to list
lpush databases sql
get all element in the list
lrange databases 0 -1
get range in list
lrange 0 10 // it will return 0 to 10 elements
get specific index element in list
lindex databases 2 // index 2 value will be return
put element data at botton of list
rpush databases redis
To remove elemnet from list
lpop databases //first element will remove
remove at the bottom of list
rpop databases
Update value of element in list
lset <listname> <index> <newvalue>
lset databases 0 newvalue
Add After/Before element in list
lpush databses before/after uk uae
check if key(list) exist before add element
lpushx movies doctor_strainge
add element at bottom of list but defore check
rpush movies avengers
Sort list accending order
sort databases ALPHA
Sort list in descending order
sort databases desc ALPHA
BLPOP
The blpop block the execution till it has an value to remove.
It remove element from list if elements are available. Or it wait till expire time for new element to come in list, than remove it.
If there is no element in list than it return nil after timeout frame.
Set in redis.
Create a new set
sadd coding java
it will create set with name 'coding' and add key named 'java'
Add multiple keys at a time
sadd coding php python c++
Retraive all keys in set.
smembers coding // it will display all set members(keys).
Comments
Post a Comment