Redis incr function behaves erratically. When trying to increment any positive integer key that has not been set yet, it results in the following error. However, when first setting it by set then incrementing using incr for the same key, the problem is no longer there. Any reason why it behaves this way? Any workarounds?
127.0.0.1:6379[5]> incr 100
(error) ERR value is not an integer or out of range
127.0.0.1:6379[5]> incr '100'
(error) ERR value is not an integer or out of range
127.0.0.1:6379[5]> incr "100"
(error) ERR value is not an integer or out of range
127.0.0.1:6379[5]> set 100 1
OK
127.0.0.1:6379[5]> incr 100
(integer) 2
This behavior is only true when incrementing non-existing Integer keys:
127.0.0.1:6379> get "ahmedov"
(nil)
127.0.0.1:6379> incr "ahmedov"
(integer) 1
127.0.0.1:6379> incr "ahmedov"
(integer) 2
127.0.0.1:6379> get 12.1
(nil)
127.0.0.1:6379> incr 12.1
(integer) 1
127.0.0.1:6379> get -1
(nil)
127.0.0.1:6379> incr -1
(integer) 1
Until you had actually called the SET command, you were trying to increment a key named '100' that did not contain a valid representation of an integer number.
After setting the key called '100' to the string "1", the increment succeeds and returns 2 (1+1) as expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With