Skip to content
This repository has been archived by the owner on Feb 23, 2019. It is now read-only.

Object Cache "Get" Method incorrectly sets cache value #438

Closed
charlesLF opened this issue Mar 14, 2017 · 2 comments
Closed

Object Cache "Get" Method incorrectly sets cache value #438

charlesLF opened this issue Mar 14, 2017 · 2 comments

Comments

@charlesLF
Copy link
Collaborator

charlesLF commented Mar 14, 2017

I came over this issue from the official W3TC support forums: https://wordpress.org/support/topic/bug-in-w3-object-cache/

To reproduce:

<?php
$test_key = “test_” . uniqid();

wp_cache_delete( $test_key, “test” );

$found = NULL;

$val = wp_cache_get( $test_key, “test”, false, $found );

var_dump($val);
var_dump($found);

Expected output:

bool(false)
bool(false)

Actual outcome:

bool(false)
bool(true)

The reason behind this is that:

  1. function delete in ObjectCache_WpObjectCache_Regular.php starts with:
if ( !$force && $this->get( $id, $group ) === false ) {
    return false;
}
  1. Function get in that same file then contains the following line:
$this->cache[$key] = $value;
$this->cache_total++;

if ( $value !== false ) {
    $this->cache_hits++;
} else {
    $this->cache_misses++;
}
  1. This is setting the value for the $key even if $value is false.

My proposed fix is to only set the cache value for $key if $value is not false

$this->cache_total++;

if ( $value !== false ) {
    $this->cache[$key] = $value;
    $this->cache_hits++;
} else {
    $this->cache_misses++;
}
@nigrosimone
Copy link
Collaborator

Thanks! You are right!

@nigrosimone
Copy link
Collaborator

merged #439

thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants