0
0
Redisquery~5 mins

SET and GET commands in Redis

Choose your learning style9 modes available
Introduction

SET and GET commands let you save and find simple pieces of information quickly in Redis.

You want to remember a user's name after they log in.
You need to store a temporary code for verifying a phone number.
You want to save a small setting or preference for an app.
You want to quickly retrieve a value without searching through big data.
You want to cache a result to make your app faster.
Syntax
Redis
SET key value
GET key

SET saves a value with a name (key).

GET retrieves the value using the key.

Examples
Saves the name "alice" under the key "username" and then gets it.
Redis
SET username "alice"
GET username
Saves a session token and retrieves it later.
Redis
SET session_token "abc123"
GET session_token
Stores a favorite color and fetches it.
Redis
SET color "blue"
GET color
Sample Program

This saves the message "Hello, world!" with the key "greeting" and then retrieves it.

Redis
SET greeting "Hello, world!"
GET greeting
OutputSuccess
Important Notes

If you GET a key that does not exist, Redis returns (nil) (no value).

SET will overwrite the value if the key already exists.

Keys and values are usually strings in Redis.

Summary

SET saves a value with a key.

GET fetches the value by key.

These commands are simple and fast for storing small pieces of data.