Redis Object Cache - Version 1.6.0

Version Description

  • Improved group name sanitization (thanks @naxvog)
  • Prevent fatal error when replacing foreign dropin
  • Added HTML footer comment with optional debug information
  • Removed prefix suggestions

The HTML footer comment only prints debug information when WP_DEBUG is enabled. To disable the comment entirely, set the `WPREDIS_DISABLE_COMMENTconstant totrue`._

Download this release

Release Info

Developer tillkruess
Plugin Icon 128x128 Redis Object Cache
Version 1.6.0
Comparing to
See all releases

Code changes from version 1.5.9 to 1.6.0

includes/admin-page.php CHANGED
@@ -66,12 +66,6 @@
66
  <th><?php _e( 'Key Prefix:', 'redis-cache' ); ?></th>
67
  <td>
68
  <code><?php echo esc_html( $redisPrefix ); ?></code>
69
-
70
- <?php if ( strlen( (string) $redisPrefix ) > 20 || ! ctype_alnum( $redisPrefix ) ) : ?>
71
- <p class="description" style="color: #d54e21;">
72
- <?php _e( 'Consider using a shorter, human-readable prefix.', 'redis-cache' ); ?>
73
- </p>
74
- <?php endif; ?>
75
  </td>
76
  </tr>
77
  <?php endif; ?>
66
  <th><?php _e( 'Key Prefix:', 'redis-cache' ); ?></th>
67
  <td>
68
  <code><?php echo esc_html( $redisPrefix ); ?></code>
 
 
 
 
 
 
69
  </td>
70
  </tr>
71
  <?php endif; ?>
includes/object-cache.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: Redis Object Cache Drop-In
4
  Plugin URI: http://wordpress.org/plugins/redis-cache/
5
  Description: A persistent object cache backend powered by Redis. Supports Predis, PhpRedis, HHVM, replication, clustering and WP-CLI.
6
- Version: 1.5.9
7
  Author: Till Krüss
8
  Author URI: https://till.im/
9
  License: GPLv3
@@ -419,15 +419,15 @@ class WP_Object_Cache
419
  }
420
 
421
  if (defined('WP_REDIS_GLOBAL_GROUPS') && is_array(WP_REDIS_GLOBAL_GROUPS)) {
422
- $this->global_groups = WP_REDIS_GLOBAL_GROUPS;
423
  }
424
 
425
  if (defined('WP_REDIS_IGNORED_GROUPS') && is_array(WP_REDIS_IGNORED_GROUPS)) {
426
- $this->ignored_groups = WP_REDIS_IGNORED_GROUPS;
427
  }
428
 
429
  if (defined('WP_REDIS_UNFLUSHABLE_GROUPS') && is_array(WP_REDIS_UNFLUSHABLE_GROUPS)) {
430
- $this->unflushable_groups = WP_REDIS_UNFLUSHABLE_GROUPS;
431
  }
432
 
433
  $client = defined('WP_REDIS_CLIENT') ? WP_REDIS_CLIENT : null;
@@ -707,7 +707,7 @@ class WP_Object_Cache
707
  $derived_key = $this->build_key($key, $group);
708
 
709
  // save if group not excluded and redis is up
710
- if (! in_array($group, $this->ignored_groups) && $this->redis_status()) {
711
  try {
712
  $exists = $this->redis->exists($derived_key);
713
 
@@ -761,7 +761,7 @@ class WP_Object_Cache
761
  $result = true;
762
  }
763
 
764
- if ($this->redis_status() && ! in_array($group, $this->ignored_groups)) {
765
  try {
766
  $result = $this->parse_redis_response($this->redis->del($derived_key));
767
  } catch (Exception $exception) {
@@ -1013,7 +1013,7 @@ LUA;
1013
  $this->cache_hits++;
1014
 
1015
  return $this->get_from_internal_cache($derived_key, $group);
1016
- } elseif (in_array($group, $this->ignored_groups) || ! $this->redis_status()) {
1017
  $found = false;
1018
  $this->cache_misses++;
1019
 
@@ -1078,7 +1078,7 @@ LUA;
1078
  $cache = array();
1079
 
1080
  foreach ($groups as $group => $keys) {
1081
- if (in_array($group, $this->ignored_groups) || ! $this->redis_status()) {
1082
  foreach ($keys as $key) {
1083
  $cache[$this->build_key($key, $group)] = $this->get($key, $group);
1084
  }
@@ -1143,7 +1143,7 @@ LUA;
1143
  $derived_key = $this->build_key($key, $group);
1144
 
1145
  // save if group not excluded from redis and redis is up
1146
- if (! in_array($group, $this->ignored_groups) && $this->redis_status()) {
1147
  $expiration = apply_filters('redis_cache_expiration', $this->validate_expiration($expiration), $key, $group);
1148
 
1149
  try {
@@ -1187,7 +1187,7 @@ LUA;
1187
  $offset = (int) $offset;
1188
 
1189
  // If group is a non-Redis group, save to internal cache, not Redis
1190
- if (in_array($group, $this->ignored_groups) || ! $this->redis_status()) {
1191
  $value = $this->get_from_internal_cache($derived_key, $group);
1192
  $value += $offset;
1193
  $this->add_to_internal_cache($derived_key, $value);
@@ -1236,7 +1236,7 @@ LUA;
1236
  $offset = (int) $offset;
1237
 
1238
  // If group is a non-Redis group, save to internal cache, not Redis
1239
- if (in_array($group, $this->ignored_groups) || ! $this->redis_status()) {
1240
  $value = $this->get_from_internal_cache($derived_key, $group);
1241
  $value -= $offset;
1242
  $this->add_to_internal_cache($derived_key, $value);
@@ -1296,16 +1296,60 @@ LUA;
1296
  }
1297
 
1298
  $salt = defined('WP_CACHE_KEY_SALT') ? trim(WP_CACHE_KEY_SALT) : '';
1299
- $prefix = in_array($group, $this->global_groups) ? $this->global_prefix : $this->blog_prefix;
1300
 
1301
- $key = str_replace(':', '-', $key);
1302
- $group = str_replace(':', '-', $group);
1303
 
1304
  $prefix = trim($prefix, '_-:$');
1305
 
1306
  return "{$salt}{$prefix}:{$group}:{$key}";
1307
  }
1308
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1309
  /**
1310
  * Convert data types when using Redis MGET
1311
  *
3
  Plugin Name: Redis Object Cache Drop-In
4
  Plugin URI: http://wordpress.org/plugins/redis-cache/
5
  Description: A persistent object cache backend powered by Redis. Supports Predis, PhpRedis, HHVM, replication, clustering and WP-CLI.
6
+ Version: 1.6.0
7
  Author: Till Krüss
8
  Author URI: https://till.im/
9
  License: GPLv3
419
  }
420
 
421
  if (defined('WP_REDIS_GLOBAL_GROUPS') && is_array(WP_REDIS_GLOBAL_GROUPS)) {
422
+ $this->global_groups = array_map([$this, 'sanitize_key_part'], WP_REDIS_GLOBAL_GROUPS);
423
  }
424
 
425
  if (defined('WP_REDIS_IGNORED_GROUPS') && is_array(WP_REDIS_IGNORED_GROUPS)) {
426
+ $this->ignored_groups = array_map([$this, 'sanitize_key_part'], WP_REDIS_IGNORED_GROUPS);
427
  }
428
 
429
  if (defined('WP_REDIS_UNFLUSHABLE_GROUPS') && is_array(WP_REDIS_UNFLUSHABLE_GROUPS)) {
430
+ $this->unflushable_groups = array_map([$this, 'sanitize_key_part'], WP_REDIS_UNFLUSHABLE_GROUPS);
431
  }
432
 
433
  $client = defined('WP_REDIS_CLIENT') ? WP_REDIS_CLIENT : null;
707
  $derived_key = $this->build_key($key, $group);
708
 
709
  // save if group not excluded and redis is up
710
+ if (! $this->is_ignored_group($group) && $this->redis_status()) {
711
  try {
712
  $exists = $this->redis->exists($derived_key);
713
 
761
  $result = true;
762
  }
763
 
764
+ if ($this->redis_status() && ! $this->is_ignored_group($group)) {
765
  try {
766
  $result = $this->parse_redis_response($this->redis->del($derived_key));
767
  } catch (Exception $exception) {
1013
  $this->cache_hits++;
1014
 
1015
  return $this->get_from_internal_cache($derived_key, $group);
1016
+ } elseif ($this->is_ignored_group($group) || ! $this->redis_status()) {
1017
  $found = false;
1018
  $this->cache_misses++;
1019
 
1078
  $cache = array();
1079
 
1080
  foreach ($groups as $group => $keys) {
1081
+ if ($this->is_ignored_group($group) || ! $this->redis_status()) {
1082
  foreach ($keys as $key) {
1083
  $cache[$this->build_key($key, $group)] = $this->get($key, $group);
1084
  }
1143
  $derived_key = $this->build_key($key, $group);
1144
 
1145
  // save if group not excluded from redis and redis is up
1146
+ if (! $this->is_ignored_group($group) && $this->redis_status()) {
1147
  $expiration = apply_filters('redis_cache_expiration', $this->validate_expiration($expiration), $key, $group);
1148
 
1149
  try {
1187
  $offset = (int) $offset;
1188
 
1189
  // If group is a non-Redis group, save to internal cache, not Redis
1190
+ if ($this->is_ignored_group($group) || ! $this->redis_status()) {
1191
  $value = $this->get_from_internal_cache($derived_key, $group);
1192
  $value += $offset;
1193
  $this->add_to_internal_cache($derived_key, $value);
1236
  $offset = (int) $offset;
1237
 
1238
  // If group is a non-Redis group, save to internal cache, not Redis
1239
+ if ($this->is_ignored_group($group) || ! $this->redis_status()) {
1240
  $value = $this->get_from_internal_cache($derived_key, $group);
1241
  $value -= $offset;
1242
  $this->add_to_internal_cache($derived_key, $value);
1296
  }
1297
 
1298
  $salt = defined('WP_CACHE_KEY_SALT') ? trim(WP_CACHE_KEY_SALT) : '';
1299
+ $prefix = $this->is_global_group($group) ? $this->global_prefix : $this->blog_prefix;
1300
 
1301
+ $key = $this->sanitize_key_part($key);
1302
+ $group = $this->sanitize_key_part($group);
1303
 
1304
  $prefix = trim($prefix, '_-:$');
1305
 
1306
  return "{$salt}{$prefix}:{$group}:{$key}";
1307
  }
1308
 
1309
+ /**
1310
+ * Replaces the set group separator by another one
1311
+ *
1312
+ * @param string $part The string to sanitize.
1313
+ * @return string Sanitized string.
1314
+ */
1315
+ protected function sanitize_key_part( $part )
1316
+ {
1317
+ return str_replace(':', '-', $part);
1318
+ }
1319
+
1320
+ /**
1321
+ * Checks if the given group is part the ignored group array
1322
+ *
1323
+ * @param string $group Name of the group to check
1324
+ * @return bool
1325
+ */
1326
+ protected function is_ignored_group($group)
1327
+ {
1328
+ return in_array($this->sanitize_key_part($group), $this->ignored_groups, true);
1329
+ }
1330
+
1331
+ /**
1332
+ * Checks if the given group is part the global group array
1333
+ *
1334
+ * @param string $group Name of the group to check
1335
+ * @return bool
1336
+ */
1337
+ protected function is_global_group($group)
1338
+ {
1339
+ return in_array($this->sanitize_key_part($group), $this->global_groups, true);
1340
+ }
1341
+
1342
+ /**
1343
+ * Checks if the given group is part the unflushable group array
1344
+ *
1345
+ * @param string $group Name of the group to check
1346
+ * @return bool
1347
+ */
1348
+ protected function is_unflushable_group($group)
1349
+ {
1350
+ return in_array($this->sanitize_key_part($group), $this->unflushable_groups, true);
1351
+ }
1352
+
1353
  /**
1354
  * Convert data types when using Redis MGET
1355
  *
readme.txt CHANGED
@@ -1,11 +1,11 @@
1
  === Redis Object Cache ===
2
  Contributors: tillkruess
3
  Donate link: https://github.com/sponsors/tillkruss
4
- Tags: redis, predis, phpredis, hhvm, pecl, caching, cache, object cache, performance, replication, clustering
5
  Requires at least: 3.3
6
  Tested up to: 5.4
7
  Requires PHP: 5.4
8
- Stable tag: 1.5.9
9
  License: GPLv3
10
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
11
 
@@ -218,6 +218,15 @@ The following commands are supported:
218
 
219
  == Changelog ==
220
 
 
 
 
 
 
 
 
 
 
221
  = 1.5.9 =
222
 
223
  - Fixed missing `$info` variable assignment in constructor
1
  === Redis Object Cache ===
2
  Contributors: tillkruess
3
  Donate link: https://github.com/sponsors/tillkruss
4
+ Tags: redis, predis, phpredis, hhvm, pecl, caching, cache, object cache, performance, replication, clustering, keydb
5
  Requires at least: 3.3
6
  Tested up to: 5.4
7
  Requires PHP: 5.4
8
+ Stable tag: 1.6.0
9
  License: GPLv3
10
  License URI: http://www.gnu.org/licenses/gpl-3.0.html
11
 
218
 
219
  == Changelog ==
220
 
221
+ = 1.6.0 =
222
+
223
+ - Improved group name sanitization (thanks @naxvog)
224
+ - Prevent fatal error when replacing foreign dropin
225
+ - Added HTML footer comment with optional debug information
226
+ - Removed prefix suggestions
227
+
228
+ _The HTML footer comment only prints debug information when `WP_DEBUG` is enabled. To disable the comment entirely, set the `WP_REDIS_DISABLE_COMMENT` constant to `true`._
229
+
230
  = 1.5.9 =
231
 
232
  - Fixed missing `$info` variable assignment in constructor
redis-cache.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: Redis Object Cache
4
  Plugin URI: https://wordpress.org/plugins/redis-cache/
5
  Description: A persistent object cache backend powered by Redis. Supports Predis, PhpRedis, HHVM, replication, clustering and WP-CLI.
6
- Version: 1.5.9
7
  Text Domain: redis-cache
8
  Domain Path: /languages
9
  Author: Till Krüss
@@ -17,7 +17,7 @@ if ( ! defined( 'ABSPATH' ) ) {
17
  exit;
18
  }
19
 
20
- define( 'WP_REDIS_VERSION', '1.5.9' );
21
 
22
  if ( defined( 'WP_CLI' ) && WP_CLI ) {
23
  require_once dirname( __FILE__ ) . '/includes/wp-cli-commands.php';
@@ -49,6 +49,7 @@ class RedisObjectCache {
49
  add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
50
  add_action( 'load-' . $this->screen, array( $this, 'do_admin_actions' ) );
51
  add_action( 'load-' . $this->screen, array( $this, 'add_admin_page_notices' ) );
 
52
  add_action( 'wp_ajax_roc_dismiss_notice', array( $this, 'dismiss_notice' ) );
53
 
54
  add_filter( sprintf(
@@ -211,7 +212,7 @@ class RedisObjectCache {
211
  return;
212
  }
213
 
214
- if ( $this->validate_object_cache_dropin() ) {
215
  return $wp_object_cache->redis_status();
216
  }
217
 
@@ -477,6 +478,36 @@ class RedisObjectCache {
477
  );
478
  }
479
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
480
  public function initialize_filesystem( $url, $silent = false ) {
481
 
482
  if ( $silent ) {
3
  Plugin Name: Redis Object Cache
4
  Plugin URI: https://wordpress.org/plugins/redis-cache/
5
  Description: A persistent object cache backend powered by Redis. Supports Predis, PhpRedis, HHVM, replication, clustering and WP-CLI.
6
+ Version: 1.6.0
7
  Text Domain: redis-cache
8
  Domain Path: /languages
9
  Author: Till Krüss
17
  exit;
18
  }
19
 
20
+ define( 'WP_REDIS_VERSION', '1.6.0' );
21
 
22
  if ( defined( 'WP_CLI' ) && WP_CLI ) {
23
  require_once dirname( __FILE__ ) . '/includes/wp-cli-commands.php';
49
  add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
50
  add_action( 'load-' . $this->screen, array( $this, 'do_admin_actions' ) );
51
  add_action( 'load-' . $this->screen, array( $this, 'add_admin_page_notices' ) );
52
+ add_action( 'shutdown', array( $this, 'maybe_print_comment' ), 0 );
53
  add_action( 'wp_ajax_roc_dismiss_notice', array( $this, 'dismiss_notice' ) );
54
 
55
  add_filter( sprintf(
212
  return;
213
  }
214
 
215
+ if ( $this->validate_object_cache_dropin() && method_exists( $wp_object_cache, 'redis_status' ) ) {
216
  return $wp_object_cache->redis_status();
217
  }
218
 
478
  );
479
  }
480
 
481
+ public function maybe_print_comment() {
482
+ global $wp_object_cache;
483
+
484
+ if ( defined( 'WP_REDIS_DISABLE_COMMENT' ) && WP_REDIS_DISABLE_COMMENT ) {
485
+ return;
486
+ }
487
+
488
+ $message = sprintf(
489
+ __( 'Performance optimized by Redis Object Cache. Learn more: %s', 'redis-cache' ),
490
+ 'https://wprediscache.com'
491
+ );
492
+
493
+ if (! WP_DEBUG) {
494
+ printf("\n<!-- %s -->\n", $message);
495
+
496
+ return;
497
+ }
498
+
499
+ $bytes = strlen(serialize($wp_object_cache->cache));
500
+
501
+ $debug = sprintf(
502
+ __( 'Retrieved %d objects (%s) from Redis using %s.', 'redis-cache' ),
503
+ $wp_object_cache->cache_hits,
504
+ function_exists( 'size_format' ) ? size_format($bytes) : "{$bytes} bytes",
505
+ $wp_object_cache->redis_client
506
+ );
507
+
508
+ printf("<!--\n%s\n\n%s\n-->\n", $message, $debug);
509
+ }
510
+
511
  public function initialize_filesystem( $url, $silent = false ) {
512
 
513
  if ( $silent ) {