DECR

Introduction

In Dragonfly, as well as in Redis and Valkey, the DECR command is used to decrement the integer value of a key by 1. This is especially useful when implementing counters, rate-limiters, or managing resources where you need to count down.

Syntax

  1. DECR key

Parameter Explanations

  • key: The name of the key whose value will be decremented by 1. The key must hold a string that is an integer.

Return Values

The command returns the value of the key after the decrement operation.

Code Examples

Basic Example

Decrement the value of a key:

  1. dragonfly> SET mycounter 5
  2. OK
  3. dragonfly> DECR mycounter
  4. (integer) 4

In this example, the value of mycounter is decremented from 5 to 4.

Decrement Non-Existent Key

If the key does not exist, DECR initializes the value to 0 before performing the decrement:

  1. dragonfly> DECR not_yet_set
  2. (integer) -1

Here, since not_yet_set did not previously exist, it is initialized to 0 and then decremented to -1.

Using DECR for Countdown Timer

You can easily implement a countdown timer using DECR, which decrements until a limit is reached:

  1. dragonfly> SET countdown 10
  2. OK
  3. dragonfly> DECR countdown
  4. (integer) 9
  5. dragonfly> DECR countdown
  6. (integer) 8
  7. dragonfly> DECR countdown
  8. (integer) 7
  9. dragonfly> DECR countdown
  10. (integer) 6

Each DECR call reduces the value by 1, simulating a countdown from 10 to 6.

Best Practices

  • Ensure that the value stored under the key is a valid integer before calling DECR. If the value cannot be parsed as an integer, an error will be returned.
  • You can use DECR in combination with INCR to create counters that can increase or decrease based on specific application logic.

Common Mistakes

  • Using DECR on a key that holds a non-integer, such as a string that cannot be parsed as a number. This will result in an error.
  1. dragonfly> SET mykey "abc"
  2. OK
  3. dragonfly> DECR mykey
  4. (error) ERR value is not an integer or out of range
  • Forgetting that if the key does not exist, DECR will treat its value as 0 and then decrement it.

FAQs

What happens if the key does not exist?

If the key does not exist, DECR behaves as if the key’s value is 0, then decrements to -1.

Is it possible to decrement a key while also checking if the value is a valid number?

No, DECR will fail if the key already holds a value that is not parsable as an integer. You need to ensure beforehand that the key contains a valid number or handle errors gracefully when calling DECR.

Can I use DECR to decrement a very large number?

Yes, DECR can handle very large integers, but keep in mind there is a limit imposed by Redis’s storage of integers. The maximum and minimum values typically correspond to the range of signed 64-bit integers.