Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen INCR and DECR Commands | Die Wesentlichen Redis-Befehle
Einführung in Redis

INCR and DECR Commands

Swipe um das Menü anzuzeigen

Note
Definition

INCR(increment) and DECR(decrement) are two Redis commands used to increase or decrease numeric values by one, stored in the Redis database.

These operations are atomic, meaning they are either fully completed or not executed at all, ensuring data consistency even when multiple processes access Redis simultaneously.

Note
Note

Both commands work exclusively with numeric values.

INCR Command

The INCR command increases a numeric value by one. It is particularly useful for scenarios like counting page views or tracking the number of likes.

Let's simulate a situation where we track the number of views on a webpage:

INCR page:main

If the key does not exist, Redis will create it and set its value to 1. Each subsequent call to the command will increase the value by 1, updating the counter to 2, 3, and so on.

If you need to increment the value of the same key multiple times, you can use the INCRBY command and specify how much you want to increase the value.

INCRBY page:main 5

In this example, the INCRBY command increases the value of the page:main key by 5. If the initial value was 0, it will become 5 after executing the command.

DECR Command

The DECR command works similarly to INCR, but it decreases the value by one. This is useful in scenarios like inventory tracking, where you need to reduce the stock count each time an item is sold.

Let's simulate tracking the number of available products in stock:

DECR product:count

On the first call to the command, the value will decrease to -1. Each subsequent call will decrease the value by 1 (to -2, -3, and so on).

If you need to decrement the value of the same key multiple times, you can use the DECRBY command and specify how much you want to decrease the value.

DECRBY product:count 3

In this example, the DECRBY command decreases the value of the product:count key by 3. If the initial value was 10, it will become 7 after executing the command.

Note
Note

If the key holds a non-numeric value, such as a string, trying to use INCR or DECR will result in an error because these commands only work with numeric values.

1. What does the INCR command do in Redis?

2. What happens if the key does not exist when you use INCR or DECR?

3. What happens if the key contains a non-numeric value and you use INCR or DECR?

question mark

What does the INCR command do in Redis?

Wählen Sie die richtige Antwort aus

question mark

What happens if the key does not exist when you use INCR or DECR?

Wählen Sie die richtige Antwort aus

question mark

What happens if the key contains a non-numeric value and you use INCR or DECR?

Wählen Sie die richtige Antwort aus

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 5

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Abschnitt 2. Kapitel 5
some-alt