Skip to content

Commit 8dcd4ee

Browse files
committed
Always strip keys
object-cache.php might be used in places filters haven't been loaded yet. Let's make the behavior explicit and strip based on the presence of `key_salt`.
1 parent 4d4aac1 commit 8dcd4ee

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

object-cache.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/*
44
Plugin Name: Memcached
55
Description: Memcached backend for the WP Object Cache.
6-
Version: 3.2.1
6+
Version: 3.2.2
77
Plugin URI: http://wordpress.org/extend/plugins/memcached/
88
Author: Ryan Boren, Denis de Bernardy, Matt Martz, Andy Skelton
99
@@ -811,10 +811,7 @@ function group_ops_stats( $op, $keys, $group, $size, $time, $comment = '' ) {
811811

812812
$this->size_total += $size;
813813

814-
$strip_keys = apply_filters( 'memcached_strip_keys', true );
815-
if ( $strip_keys ) {
816-
$keys = $this->strip_memcached_keys( $keys );
817-
}
814+
$keys = $this->strip_memcached_keys( $keys );
818815

819816
if ( $time > $this->slow_op_microseconds && 'get_multi' !== $op ) {
820817
$this->increment_stat( 'slow-ops' );
@@ -829,17 +826,24 @@ function group_ops_stats( $op, $keys, $group, $size, $time, $comment = '' ) {
829826
}
830827

831828
/**
832-
* Key format: key:flush_timer:table_prefix:key_name
833-
* We want to strip the `key:flush_timer` part to not leak the memcached keys.
829+
* Key format: key_salt:flush_timer:table_prefix:key_name
830+
*
831+
* We want to strip the `key_salt:flush_timer` part to not leak the memcached keys.
832+
* If `key_salt` is set we strip `'key_salt:flush_timer`, otherwise just strip the `flush_timer` part.
834833
*/
835834
function strip_memcached_keys( $keys ) {
836835
if ( ! is_array( $keys ) ) {
837836
$keys = [ $keys ];
838837
}
839838

840839
foreach ( $keys as $key => $value ) {
841-
$second_colon = strpos( $value, ':', strpos( $value, ':' ) + 1 );
842-
$keys[ $key ] = substr( $value, $second_colon + 1 );
840+
$offset = 0;
841+
if ( ! empty( $this->key_salt ) ) {
842+
$offset = strpos( $value, ':' ) + 1;
843+
}
844+
845+
$start = strpos( $value, ':', $offset );
846+
$keys[ $key ] = substr( $value, $start + 1 );
843847
}
844848

845849
if ( 1 === count( $keys ) ) {

readme.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Contributors: ryan, sivel, andy, nacin, barry, ethitter, nickdaugherty, batmoo,
33
Tags: cache, memcached
44
Requires at least: 5.3
55
Tested up to: 5.4.1
6-
Stable tag: 3.2.1
6+
Stable tag: 3.2.2
77
Requires PHP: 5.6.20
88

99
Use memcached and the PECL memcache extension to provide a backing store for the WordPress object cache.
@@ -21,7 +21,7 @@ Memcached Object Cache provides a persistent backend for the WordPress object ca
2121
1. Add the `WP_CACHE_KEY_SALT` constant to the `wp-config.php`:
2222

2323
```php
24-
define('WP_CACHE_KEY_SALT', '...long random string...');
24+
define( 'WP_CACHE_KEY_SALT', '...long random string...' );
2525
```
2626

2727
This helps prevent cache pollution when multiplte WordPress installs are using the same Memcached server. The value must be unique for each WordPress install.
@@ -83,6 +83,9 @@ widget
8383

8484
== Changelog ==
8585

86+
= 3.2.2 =
87+
* Remove filter, and base key stripping on presence of `key_salt`
88+
8689
= 3.2.1 =
8790
* Fix bug allowing **slow-ops** entries to have the same key, so toggling doesn't work
8891

0 commit comments

Comments
 (0)