Redis fundamentals and cheatsheet.

Redis fundamentals and cheatsheet.

Pramit Marattha

Published on Jun 30, 2021

3 min read

Subscribe to my newsletter and never miss my upcoming articles

Listen to this article

Helloooo !! Everyone !!! . In this tutorial, I have compiled all the necessary commands and cheatsheets required for you to get started with Redis.

So, without any further ado let's get started.

What is Redis?

Redis is often referred to as a data structures server. What this means is that Redis provides access to mutable data structures via a set of commands, which are sent using a server-client model with TCP sockets and a simple protocol. So different processes can query and modify the same data structures in a shared way.

At first, download and install Redis.

For linux and max user visit here => https://redis.io/download

For windows, user visit here => Installing Redis on Windows

If you are still unable to install Redis there are a bunch of videos out there.

For more installation guides => visit here


Start the CLI by

redis-cli

Add a key to database

(it is in key-value pair)

SET name pramit

Get/ read the value in database

GET name

Deleting the value in redis

DEL name

Check the existence of the keys

EXISTS name

Display all the inserted keys

KEYS  *

Delete everything inside the databases

( by flushing all)

FLUSHALL

Set an Expiration time to certain keys

EXPIRE name 10

name keys expire in 10 second

Add an expiration time immediately at the time of the creation of the key.

SETEX name 20 pramit

Check time to live for that key.

ttl name

Redis also supports handling arrays in the form of Lists.

Adding item to start of the Lists.

(left push) => push to the left side of the array

LPUSH car Toyota

To print out the list you need to.

(lrange key startIndex endIndex)

LRANGE car 0 -1

Let's Add another key to the list.

LPUSH car tata

Adding item to the right of the array.

( right push) => push to the right side of the array.

RPUSH car tesla

POP / remove the item from the array(Lists).

LPOP to pop from the left side of array(Lists).

RPOP to pop from the right side of array(Lists).

LPOP car
RPOP car

Sets in Redis are the same as the array but it's every value is unique.

Add an item to sets.

(sadd) => It means sets+add

SADD games "forza horizon"

To list out all the items.

SMEMBERS

To remove the items from the sets.

(srem) => sets+remove

SREM games "forza horizon"

Redis also have Hashes to store JSON objects.

(NOTE: Hashes does not provide nesting)

Add an item to the hash.

(HSET key field value)

HSET games name forza
HGET games name

TO print/ get everything.

HGETALL games

Adding another field for that same key.

HSET games release 2019

Print/get the individual properties.

HGET games name

HGET games release

Deleting certain properties.

HDEL games release

checking the existence of a field in hashes.

HEXISTS games name

HEXISTS games release

Follow me on Github and Twitter

 
Share this
Proudly part of