GETSET

Introduction

In Dragonfly, as well as in Redis and Valkey, the GETSET command is used to atomically set a key to a new string value and return its old string value. This command is useful when you need to replace a value but also want to keep track of the previous value before overwriting it, making it an atomic and efficient way to perform this operation.

Syntax

  1. GETSET key value

Parameter Explanations

  • key: The key whose value should be replaced.
  • value: The new value to set the key to.

Return Values

The command returns the old value that was stored at the key before it was set to the new value. If the key did not exist, it returns nil.

Code Examples

Basic Example

Set a key to a new value and retrieve the old value:

  1. dragonfly> SET mykey "old_value"
  2. OK
  3. dragonfly> GETSET mykey "new_value"
  4. "old_value"

If the key does not exist yet, GETSET will return nil:

  1. dragonfly> GETSET mykey2 "first_value"
  2. (nil)

Using GETSET for Atomic Value Updates

This command is particularly useful when you want to update a key’s value atomically while preserving the old value for further use or inspection:

  1. # User balance system where balance is first saved, then updated atomically
  2. dragonfly> SET balance "100"
  3. OK
  4. dragonfly> GETSET balance "200"
  5. "100" # Old balance
  6. dragonfly> GET balance
  7. "200" # New balance

GETSET in Cache Systems

In cache systems, GETSET is great for refreshing stale values atomically:

  1. # Set initial cache value
  2. dragonfly> SET cache_item "stale_value"
  3. OK
  4. # Atomically refresh the cache with a new value, returning the old one
  5. dragonfly> GETSET cache_item "fresh_value"
  6. "stale_value"
  7. # Verify that the value has been updated
  8. dragonfly> GET cache_item
  9. "fresh_value"

Best Practices

  • Use GETSET when you need atomicity in replacing a value and retrieving the previous one.
  • Be cautious when using GETSET in high-write workloads, as it can be slower than using SET on its own, due to the need to return the old value.

Common Mistakes

  • Trying to use GETSET on non-string values like lists, sets, or hashes; it only works on string values.
  • Assuming that it will return something other than nil for non-existent keys.

FAQs

What happens if the key does not exist?

If the key does not exist, GETSET will return nil and set the key to the new value.

Can GETSET be used with non-string values such as lists or sets?

No, GETSET only works with string values. For non-string data types, consider using other commands like LINDEX for lists or HGETSET for hashes.

Is GETSET atomic?

Yes, GETSET is atomic, meaning it safely sets the new value and retrieves the old value in one single operation. This helps avoid race conditions in concurrent environments.