Introduction
INCRBY and DECRBY help you add or subtract numbers stored in Redis keys easily.
INCRBY and DECRBY help you add or subtract numbers stored in Redis keys easily.
INCRBY key increment DECRBY key decrement
key is the name of the stored number.
increment and decrement are integers.
INCRBY visits 5DECRBY stock 3INCRBY score 10DECRBY balance 2This sets 'counter' to 10, adds 7, subtracts 3, then gets the final value.
SET counter 10 INCRBY counter 7 DECRBY counter 3 GET counter
If the key does not exist, INCRBY and DECRBY start it at 0 before changing.
These commands only work on keys holding integer values.
Using these commands is faster than getting the value, changing it in your app, then setting it back.
INCRBY adds a number to a Redis key's value.
DECRBY subtracts a number from a Redis key's value.
They are simple and fast ways to update numbers in Redis.
INCRBY key 5 do?INCRBY command increases the integer value stored at the specified key by the given amount.INCRBY key 5 adds 5 to the current value of key.counter?DECRBY key number.DECRBY counter 3 correctly subtracts 3 from counter. INCR counter -3 is invalid (INCR takes only one argument). DECR counter 3 and SUBTRACT counter 3 are invalid.SET score 10INCRBY score 7DECRBY score 4score?SET score 10 sets the value of score to 10.INCRBY score 7 adds 7, making it 17. Then DECRBY score 4 subtracts 4, resulting in 13.DECRBY visits 2 but get an error. Which is the most likely cause?visits exists but is not an integer, causing DECRBY to fail.page_views. Initially, it does not exist. You run:INCRBY page_views 10DECRBY page_views 3INCRBY page_views 5page_views and why?INCRBY page_views 10 sets it to 10. Then DECRBY page_views 3 subtracts 3, making 7. Finally, INCRBY page_views 5 adds 5, resulting in 12.Pass the test and this pattern moves unknown