🎉Redis for beginners👶

🎉Redis for beginners👶

📌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 Ubuntu

      • Finally, 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 server

  • Remember 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 storage

  • GET <KEY_NAME>🔸Get the value for given KEY_NAME

  • KEYS *🔸 Get all keys

  • EXISTS <KEY_NAME>🔸Check if a key exists

  • FLUSHALL🔸Removes all saved-up key-value pairs

  • CLEAR🔸 Clear the console

  • EXPIRE <KEY_NAME> <SECONDS>🔸For existing key set expiration time

  • SETEX <KEY_NAME> <SECONDS> <VALUE>🔸Set a new key with expiration time

Array

  • LPUSH <KEY_NAME> <VALUE>🔸Add's KV at the start

  • RPUSH <KEY_NAME> <VALUE>🔸Add's KV at the end

  • LPOP <KEY_NAME>🔸 Remove KV from the start

  • RPOP <KEY_NAME>🔸 Remove KV from the end

  • LRANGE <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 set

  • SMEMBERS <KEY_NAME>🔸 Log all the KV pairs in the set

  • SREM <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 hash

  • HGET <KEY> <FIELD>🔸Get the value for a key's field

  • HGETALL <KEY>🔸Get all Fields and Values for a Key

  • HDEL <KEY> <FIELD>🔸Removes Field and Value for a given field

  • HEXISTS <KEY> <FIELD>🔸Check if a field exists


Connect with me🙋‍♂️: