📌What is Redis
- Redis is a lightning-fast in-memory database, which means that it stores data in RAM rather than on disk. It's mainly used for quick data retrieval, like caching, to boost the performance of applications and websites.
📌How to install Redis
🪟Windows
You need to install a distros(distribution) for Linux let's take Ubuntu
First, you need wsl (Also disable your antivirus, because sometimes it cries. This is 100% safe)
Open cmd or PowerShell as admin
wsl --install
List out all distros
wsl --list --online
Install Ubuntu
wsl --install -d Ubuntu
Open Ubuntu from the search bar
For the first time, it will ask you to set a name and password
Then install package universe
sudo add-apt-repository universe
, this will allow you to install all the open-source packages like Redis which do not come under UbuntuFinally, install Redis
sudo apt-get intall redis
Congratulations 🎉 You have installed Redis, to start Redis Server on your local machine type
redis-server
in your Ubuntu terminal.Now open another Ubuntu terminal and type
redis-cli
, now you can communicate with your Redis serverRemember not to close your Redis Server, when using Redis
Basic Commands
Note - < abc > needs to be replaced by appropriate data KV - Key-Value
SET <KEY_NAME> <VALUE>
🔸Set a key value in Redis volatile storageGET <KEY_NAME>
🔸Get the value for given KEY_NAMEKEYS *
🔸 Get all keysEXISTS <KEY_NAME>
🔸Check if a key existsFLUSHALL
🔸Removes all saved-up key-value pairsCLEAR
🔸 Clear the consoleEXPIRE <KEY_NAME> <SECONDS>
🔸For existing key set expiration timeSETEX <KEY_NAME> <SECONDS> <VALUE>
🔸Set a new key with expiration time
Array
LPUSH <KEY_NAME> <VALUE>
🔸Add's KV at the startRPUSH <KEY_NAME> <VALUE>
🔸Add's KV at the endLPOP <KEY_NAME>
🔸 Remove KV from the startRPOP <KEY_NAME>
🔸 Remove KV from the endLRANGE <KEY_NAME> <START> <END>
🔸 Log all the KV's from range start to end
Set (Does not contain duplicates)
SADD <KEY_NAME> <VALUE>
🔸Add KV to setSMEMBERS <KEY_NAME>
🔸 Log all the KV pairs in the setSREM <KEY_NAME> <VALUE>
🔸 Remove KV from the set
Hashes
Why even need Hash, as Redis is already built upon key-value pairs?
Hash in Redis gives users a way to store multiple fields and values for a key. Additionally, it allows users to store complex data.
HSET <KEY> <FIELD> <VALUE>
🔸 Add's key-{field, value} in hashHGET <KEY> <FIELD>
🔸Get the value for a key's fieldHGETALL <KEY>
🔸Get all Fields and Values for a KeyHDEL <KEY> <FIELD>
🔸Removes Field and Value for a given fieldHEXISTS <KEY> <FIELD>
🔸Check if a field exists
Connect with me🙋♂️: