What is the purpose of the redis getset command?
The GETSET command is used to set the value of a specified key and return the old value of the key. Its specific function is as follows:
- If the key exists, set the value of the key to the specified value and return the old value of the key.
- If the key does not exist, create a new key, set its value to the specified value, and return nil.
Suppose there is a key named mykey with a value of oldvalue, perform the following operation:
If you execute GETSET mykey newvalue, then:
- The return value is oldvalue.
- The value of the key mykey will be set to newvalue.
In this way, the GETSET command can update the value of a key without overriding the existing value and also retrieve the previous value.