wp_cache_get returns false instead of string ‘0’
-
I’ve created PHP script to confirm this issue:
<?php include_once( __DIR__ . '/public_html/wp-load.php' ); global $wp_object_cache; $my_key = 'can_compress_scripts'; if ( isset( $argv[1] ) ) { $my_key = $argv[1]; } $my_group = 'options'; echo 'wp_cache_add : '; var_dump( wp_cache_add( $my_key, '0', $my_group ) ); echo 'wp_cache_get : '; var_dump( wp_cache_get( $my_key, $my_group, false, $found ) ); echo ' found = '; var_dump( $found ); $mc = $wp_object_cache->get_mc( $my_group ); echo 'Memcached::get : '; var_dump( $mc->get( $wp_object_cache->key( $my_key, $my_group ) ) ); echo 'Memcached::getResultCode : '; var_dump($mc->getResultCode());
Results of the script:
[~]# wp --path=public_html cache flush Success: The cache was flushed. [~]# php test-mc.php wp_cache_add : bool(false) wp_cache_get : string(1) "0" found = bool(true) Memcached::get : string(1) "0" Memcached::getResultCode : int(0) [~]# php test-mc.php wp_cache_add : bool(false) wp_cache_get : bool(false) found = bool(true) Memcached::get : string(1) "0" Memcached::getResultCode : int(0)
First run, the function wp_cache_add updates memcache and local cache ($this->cache property). The function wp_cache_get retrieves value from local cache (‘0’).
Second run, the function wp_cache_add returns false (it’s correct because the key is already cached) and local cache isn’t populated. The function wp_cache_get retrieves value from memcache which is converted to
false
(which is wrong).The issue is the consequence of “empty” conditional (the method get in object cache drop-in):
$value = $mc->get( $key ); if ( empty( $value ) || ( is_integer( $value ) && -1 == $value ) ){ $value = false; $found = $mc->getResultCode() !== Memcached::RES_NOTFOUND; } else { $found = true; }
Empty returns true for
'0'
,”,0
,array()
,false
… So, these values can’t be cached… Also-1
can’t be cached.The method Memcached::get returns only false if the value isn’t stored:
Returns the value stored in the cache or false otherwise.
Please return false only if it’s “real boolean false” (which often means “not found”). Other drop-ins return proper value. I didn’t notice similar issues with Redis Object Cache or other memcache drop-ins.
- The topic ‘wp_cache_get returns false instead of string ‘0’’ is closed to new replies.