decr operation in Memcached is used when you want to decrease a numeric item's value. This can be particularly useful in scenarios such as rate limiting or managing counters where you need to reduce a count by a certain amount.
Here's an example of using
decr with PHP and Memcached:
In this script, we first create an instance of Memcached and add a server. We then set a 'counter' with initial value of 20. The
decrement command decreases the 'counter' value by 5, and finally we retrieve and print the updated value of 'counter', which will output '15'.
FALSEif something went wrong.
decrcannot be applied on non-numeric values.
Q: What happens if I try to decrement the value below 0?
A: If a decrement operation would result in a negative value, Memcached will return and store the value as 0.
Q: Can I use
decr on a string value?
decr can only be used on numeric values. If used on string or other non-numeric types, it will return an error.