Skip to content

Commit 2b30d7c

Browse files
committed
Key validation + logging
1 parent 74360ac commit 2b30d7c

File tree

1 file changed

+47
-25
lines changed

1 file changed

+47
-25
lines changed

object-cache.php

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,6 @@ function rotate_global_keys() {
302302

303303
function get( $id, $group = 'default', $force = false, &$found = null ) {
304304
$key = $this->key( $id, $group );
305-
$mc =& $this->get_mc( $group );
306305
$found = true;
307306

308307
if ( isset( $this->cache[ $key ] ) && ( ! $force || in_array( $group, $this->no_mc_groups ) ) ) {
@@ -327,22 +326,12 @@ function get( $id, $group = 'default', $force = false, &$found = null ) {
327326
$flags = false;
328327
$this->timer_start();
329328

330-
// Sending as an array so we can verify the key being returned is what we asked for.
331-
$value = $mc->get( [ $key ], $flags );
332-
if ( is_array( $value ) && isset( $value[ $key ] ) ) {
333-
$value = $value[ $key ];
334-
} else {
335-
$value = false;
336-
}
329+
$validated_get = $this->validated_get( $key, $group );
330+
$value = $validated_get['value'];
331+
$found = $validated_get['found'];
337332

338333
$elapsed = $this->timer_stop();
339334

340-
// Value will be unchanged if the key doesn't exist.
341-
if ( false === $flags ) {
342-
$found = false;
343-
$value = false;
344-
}
345-
346335
$this->cache[ $key ] = [
347336
'value' => $value,
348337
'found' => $found,
@@ -379,7 +368,6 @@ function get_multi( $groups ) {
379368
);
380369

381370
foreach ( $groups as $group => $ids ) {
382-
$mc =& $this->get_mc( $group );
383371
$keys = array();
384372
$this->timer_start();
385373

@@ -412,18 +400,14 @@ function get_multi( $groups ) {
412400

413401
continue;
414402
} else {
415-
// Sending as an array so we can verify the key being returned is what we asked for.
416-
$fresh_get = $mc->get( [ $key ] );
417-
if ( is_array( $fresh_get ) && isset( $fresh_get[ $key ] ) ) {
418-
$fresh_get = $fresh_get[ $key ];
419-
} else {
420-
$fresh_get = false;
421-
}
403+
$validated_get = $this->validated_get( $key, $group );
404+
$value = $validated_get['value'];
405+
$found = $validated_get['found'];
422406

423-
$return[ $key ] = $fresh_get;
407+
$return[ $key ] = $value;
424408
$return_cache[ $key ] = [
425-
'value' => $fresh_get,
426-
'found' => false !== $fresh_get,
409+
'value' => $value,
410+
'found' => $found,
427411
];
428412
}
429413
}
@@ -437,6 +421,44 @@ function get_multi( $groups ) {
437421
return $return;
438422
}
439423

424+
/**
425+
* Retrieve item from the memcached server, but with extra validation.
426+
* Sends the key in an array so we can validate that the key being returned is what was requested.
427+
*
428+
* @param $key Memcached key being requested.
429+
* @param $group The key's group.
430+
* @return array Key's value, and whether it was $found in memcached.
431+
*/
432+
private function validated_get( $key, $group ) {
433+
$mc =& $this->get_mc( $group );
434+
435+
$flags = false;
436+
$raw_value = $mc->get( [ $key ], $flags );
437+
438+
$value = false;
439+
if ( is_array( $raw_value ) && ! empty( $raw_value ) ) {
440+
if ( array_key_exists( $key, $raw_value ) ) {
441+
// For backwards-compatability, NULL needs to be set to FALSE due to how non-array params were prev returned.
442+
$value = is_null( $raw_value[ $key ] ) ? false : $raw_value[ $key ];
443+
} else {
444+
$key_recieved = array_keys( $raw_value )[0];
445+
trigger_error( "Memcache Inconsistency: Requested '$key', recieved '$key_recieved'", E_USER_WARNING );
446+
}
447+
}
448+
449+
$found = true;
450+
if ( false === $flags ) {
451+
// The key was not found in memcached.
452+
$value = false;
453+
$found = false;
454+
}
455+
456+
return [
457+
'value' => $value,
458+
'found' => $found,
459+
];
460+
}
461+
440462
function flush_prefix( $group ) {
441463
if ( 'WP_Object_Cache' === $group || 'WP_Object_Cache_global' === $group ) {
442464
// Never flush the flush numbers.

0 commit comments

Comments
 (0)