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:

  1. If the key exists, set the value of the key to the specified value and return the old value of the key.
  2. 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:

  1. The return value is oldvalue.
  2. 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.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds