Skip to content

Commit eed3696

Browse files
author
John Spellman
committed
[BUGS-6127] Add support for flush_runtime and flush_group (#405)
1 parent 1db2b1c commit eed3696

File tree

5 files changed

+130
-5
lines changed

5 files changed

+130
-5
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
**Tags:** cache, plugin, redis
66
**Requires at least:** 3.0.1
77
**Tested up to:** 6.2
8-
**Stable tag:** 1.3.6-dev
8+
**Stable tag:** 1.4.0-dev
99
**License:** GPLv2 or later
1010
**License URI:** http://www.gnu.org/licenses/gpl-2.0.html
1111

@@ -105,6 +105,7 @@ There's a known issue with WordPress `alloptions` cache design. Specifically, a
105105
## Changelog ##
106106

107107
### Latest ###
108+
* Add support for `flush_runtime` and `flush_group` functions [[#405](https://github.com/pantheon-systems/wp-redis/pull/405)]
108109
* Update Composer dependencies [[#401](https://github.com/pantheon-systems/wp-redis/pull/394)]
109110

110111
### 1.3.5 (April 6, 2023) ###
@@ -229,3 +230,6 @@ Declare `wp_cache_supports` function and support features. [[#378](https://githu
229230

230231
### 0.1 ###
231232
* Initial commit of working code for the benefit of all.
233+
234+
## Latest ##
235+
WP Redis 1.4.0-dev adds support for the `flush_runtime` and `flush_group` functions. If you've copied `object-cache.php` and made your own changes, be sure to copy these additions over as well.

object-cache.php

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,35 @@ function wp_cache_get_multiple( $keys, $group = '', $force = false ) {
157157
return $wp_object_cache->get_multiple( $keys, $group, $force );
158158
}
159159

160+
/**
161+
* Removes all cache items from the in-memory runtime cache.
162+
*
163+
* @see WP_Object_Cache::flush()
164+
*
165+
* @return bool True on success, false on failure.
166+
*/
167+
function wp_cache_flush_runtime() {
168+
return wp_cache_flush();
169+
}
170+
171+
/**
172+
* Removes all cache items in a group, if the object cache implementation supports it.
173+
*
174+
* Before calling this function, always check for group flushing support using the
175+
* `wp_cache_supports( 'flush_group' )` function.
176+
*
177+
* @see WP_Object_Cache::flush_group()
178+
* @global WP_Object_Cache $wp_object_cache Object cache global instance.
179+
*
180+
* @param string $group Name of group to remove from cache.
181+
* @return bool True if group was flushed, false otherwise.
182+
*/
183+
function wp_cache_flush_group( $group ) {
184+
global $wp_object_cache;
185+
186+
return $wp_object_cache->flush_group( $group );
187+
}
188+
160189
/**
161190
* Increment numeric cache item's value
162191
*
@@ -304,13 +333,13 @@ function wp_cache_reset() {
304333
function wp_cache_supports( $feature ) {
305334
switch ( $feature ) {
306335
case 'get_multiple':
336+
case 'flush_runtime':
337+
case 'flush_group':
307338
return true;
308339

309340
case 'add_multiple':
310341
case 'set_multiple':
311342
case 'delete_multiple':
312-
case 'flush_runtime':
313-
case 'flush_group':
314343
default:
315344
return false;
316345
}
@@ -630,6 +659,31 @@ public function flush( $redis = true ) {
630659
return true;
631660
}
632661

662+
/**
663+
* Removes all cache items in a group.
664+
*
665+
* @param string $group Name of group to remove from cache.
666+
* @return true Always returns true.
667+
*/
668+
public function flush_group( $group ) {
669+
if ( ! $this->_should_use_redis_hashes( $group ) ) {
670+
return false;
671+
}
672+
673+
$multisite_safe_group = $this->multisite && ! isset( $this->global_groups[ $group ] ) ? $this->blog_prefix . $group : $group;
674+
$redis_safe_group = $this->_key( '', $group );
675+
if ( $this->_should_persist( $group ) ) {
676+
$result = $this->_call_redis( 'del', $redis_safe_group );
677+
if ( 1 !== $result ) {
678+
return false;
679+
}
680+
} elseif ( ! $this->_should_persist( $group ) && ! isset( $this->cache[ $multisite_safe_group ] ) ) {
681+
return false;
682+
}
683+
unset( $this->cache[ $multisite_safe_group ] );
684+
return true;
685+
}
686+
633687
/**
634688
* Retrieves the cache contents, if it exists
635689
*

readme.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Contributors: getpantheon, danielbachhuber, mboynes, Outlandish Josh, jspellman,
33
Tags: cache, plugin, redis
44
Requires at least: 3.0.1
55
Tested up to: 6.2
6-
Stable tag: 1.3.6-dev
6+
Stable tag: 1.4.0-dev
77
License: GPLv2 or later
88
License URI: http://www.gnu.org/licenses/gpl-2.0.html
99

@@ -103,6 +103,7 @@ There's a known issue with WordPress `alloptions` cache design. Specifically, a
103103
== Changelog ==
104104

105105
= Latest =
106+
* Add support for `flush_runtime` and `flush_group` functions [[#405](https://github.com/pantheon-systems/wp-redis/pull/405)]
106107
* Update Composer dependencies [[#401](https://github.com/pantheon-systems/wp-redis/pull/394)]
107108

108109
= 1.3.5 (April 6, 2023) =
@@ -224,3 +225,8 @@ There's a known issue with WordPress `alloptions` cache design. Specifically, a
224225

225226
= 0.1 =
226227
* Initial commit of working code for the benefit of all.
228+
229+
== Upgrade Notice ==
230+
231+
= Latest =
232+
WP Redis 1.4.0-dev adds support for the `flush_runtime` and `flush_group` functions. If you've copied `object-cache.php` and made your own changes, be sure to copy these additions over as well.

tests/phpunit/test-cache.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,67 @@ public function test_flush() {
391391
}
392392
}
393393

394+
public function test_wp_cache_flush_runtime() {
395+
// Add some data to the cache
396+
$data = array(
397+
rand_str() => rand_str(),
398+
rand_str() => rand_str()
399+
);
400+
401+
foreach ( $data as $key => $value ) {
402+
wp_cache_set( $key, $value, 'test_wp_cache_flush_runtime' );
403+
}
404+
405+
// Verify that the cache contains the data
406+
foreach ( $data as $key => $value ) {
407+
$this->assertEquals( $value, wp_cache_get( $key, 'test_wp_cache_flush_runtime' ) );
408+
}
409+
410+
// Flush the cache
411+
wp_cache_flush_runtime();
412+
413+
// Verify that the cache is now empty
414+
foreach ($data as $key => $value) {
415+
$this->assertFalse( wp_cache_get( $key, 'test_wp_cache_flush_runtime' ) );
416+
}
417+
}
418+
419+
public function test_wp_cache_flush_group() {
420+
$key1 = rand_str();
421+
$val1 = rand_str();
422+
$key2 = rand_str();
423+
$val2 = rand_str();
424+
$key3 = rand_str();
425+
$val3 = rand_str();
426+
$group = 'foo';
427+
$group2 = 'bar';
428+
429+
if ( ! defined( 'WP_REDIS_USE_CACHE_GROUPS' ) || ! WP_REDIS_USE_CACHE_GROUPS ) {
430+
$GLOBALS['wp_object_cache']->add_redis_hash_groups( array( $group, $group2 ) );
431+
}
432+
433+
// Set up the values
434+
wp_cache_set( $key1, $val1, $group );
435+
wp_cache_set( $key2, $val2, $group );
436+
wp_cache_set( $key3, $val3, $group2 );
437+
$this->assertEquals( $val1, wp_cache_get( $key1, $group ) );
438+
$this->assertEquals( $val2, wp_cache_get( $key2, $group ) );
439+
$this->assertEquals( $val3, wp_cache_get( $key3, $group2 ) );
440+
441+
$this->assertTrue( wp_cache_flush_group( $group ) );
442+
443+
$this->assertFalse( wp_cache_get( $key1, $group ) );
444+
$this->assertFalse( wp_cache_get( $key2, $group ) );
445+
$this->assertEquals( $val3, wp_cache_get( $key3, $group2 ) );
446+
447+
// _call_redis( 'delete' ) always returns true when Redis isn't available
448+
if ( class_exists( 'Redis' ) ) {
449+
$this->assertFalse( wp_cache_flush_group( $group ) );
450+
} else {
451+
$this->assertTrue( wp_cache_flush_group( $group ) );
452+
}
453+
}
454+
394455
// Make sure objects are cloned going to and from the cache
395456
public function test_object_refs() {
396457
$key = rand_str();

wp-redis.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Plugin Name: WP Redis
44
* Plugin URI: http://github.com/pantheon-systems/wp-redis/
55
* Description: WordPress Object Cache using Redis. Requires the PhpRedis extension (https://github.com/phpredis/phpredis).
6-
* Version: 1.3.6-dev
6+
* Version: 1.4.0-dev
77
* Author: Pantheon, Josh Koenig, Matthew Boynes, Daniel Bachhuber, Alley Interactive
88
* Author URI: https://pantheon.io/
99
*/

0 commit comments

Comments
 (0)