Redis String: Set key value
Description
Redis SET command is used to set some string value in redis key. If the key already holds a value, it is overwritten, regardless of its type. Any previous time to live associated with the key is discarded on a successful SET operation.
Syntax:
Basic syntax of redis SET command is shown below:
redis 127.0.0.1:6379> SET KEY_NAME VALUE
Available since
1.0.0.
Return Value
Simple string reply. OK, if a value is set to key or Null, if the value does not set.
Return Value Type
String
Example:
redis 127.0.0.1:6379> SET w3resource redis OK
Options
In SET command there are many options available, that modify the behavior of command. Basic syntax of SET command with available options is shown below:
redis 127.0.0.1:6379> SET KEY VALUE [EX seconds] [PX milliseconds] [NX|XX] (integer) 1
- EX seconds - Set the specified expire time, in seconds.
- PX milliseconds - Set the specified expire time, in milliseconds.
- NX - Only set the key if it does not already exist.
- XX - Only set the key if it already exist.
Example:
redis 127.0.0.1:6379> SET w3resource redis EX 60 NX OK
Above example will set the key w3resource, with an expiry of 60 seconds, if the key does not exist.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join