[wp-trac] [WordPress Trac] #62236: Why do wp_cache_incr / wp_cache_decr not allow negative values?
WordPress Trac
noreply at wordpress.org
Wed Oct 16 08:14:33 UTC 2024
#62236: Why do wp_cache_incr / wp_cache_decr not allow negative values?
--------------------------------------------+------------------------------
Reporter: docjojo | Owner: (none)
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Cache API | Version: 6.6.2
Severity: normal | Keywords: changes-
Focuses: performance, php-compatibility | requested
--------------------------------------------+------------------------------
Both functions have this line of code:
[https://developer.wordpress.org/reference/classes/wp_object_cache/decr/
]
[https://developer.wordpress.org/reference/classes/wp_object_cache/incr/
]
{{{#!php
<?php
if ( $this->cache[ $group ][ $id ] < 0 )
$this->cache[ $group ][ $id ] = 0;
}}}
== Why are negative values turned to zero, making it impossible to inc/dec
negative values?
wp_cache_set accepts any integer – so negative values can be set, but once
incr/decr are called this value will be set to zero or zero + offset.
No other programming language handles inc/dec this way.
PHP, Javascript, C++ and so on have the inc/dec operator ++, -- that work
with negative values.
When it comes to caching and APCu is used for example.
[https://www.php.net/manual/en/function.apcu-inc.php
]
APCu handles negative values.
Same with redis and memcached:
[https://redis.io/docs/latest/commands/incr/
]
[https://www.php.net/manual/en/memcache.increment.php
]
I think this is inconsistent unless there is a special reason to prevent
negative values – but then, negative values should not be allow in the
first place (wp_cache_set).
--
Ticket URL: <https://core.trac.wordpress.org/ticket/62236>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list