SET
Syntax
SET key value [EX seconds] [PX milliseconds] [EXAT timestamp] [PXAT timestamp] [XX] [NX] [KEEPTTL]
SET puts or updates an existing <key, value> pair.
SET identifies the type of the value based on the value itself. If the value is an integer, it will be stored as an integer. Otherwise, it will be stored as a string.
- EX seconds: Set the expiration time in seconds
- PX milliseconds: Set the expiration time in milliseconds
- EXAT timestamp: Set the expiration time in seconds since epoch
- PXAT timestamp: Set the expiration time in milliseconds since epoch
- XX: Only set the key if it already exists
- NX: Only set the key if it does not already exist
- KEEPTTL: Keep the existing TTL of the key
- GET: Return the value of the key after setting it
Returns “OK” if the key was set or updated. Returns (nil) if the key was not set or updated. Returns the value of the key if the GET option is provided.
Examples
localhost:7379> SET k 43OK OKlocalhost:7379> SET k 43 EX 10OK OKlocalhost:7379> SET k 43 PX 10000OK OKlocalhost:7379> SET k 43 EXAT 1772377267OK OKlocalhost:7379> SET k 43 PXAT 1772377267000OK OKlocalhost:7379> SET k 43 XXOK OKlocalhost:7379> SET k 43 NXOK (nil)localhost:7379> SET k 43 KEEPTTLOK OKlocalhost:7379> SET k 43 GETOK 43