Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGS-6127] Add support for flush_runtime and flush_group #405

Merged
merged 13 commits into from May 8, 2023
6 changes: 5 additions & 1 deletion README.md
Expand Up @@ -5,7 +5,7 @@
**Tags:** cache, plugin, redis
**Requires at least:** 3.0.1
**Tested up to:** 6.2
**Stable tag:** 1.3.6-dev
**Stable tag:** 1.4.0-dev
**License:** GPLv2 or later
**License URI:** http://www.gnu.org/licenses/gpl-2.0.html

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

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

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

### 0.1 ###
* Initial commit of working code for the benefit of all.

## Latest ##
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.
58 changes: 56 additions & 2 deletions object-cache.php
Expand Up @@ -155,6 +155,35 @@ function wp_cache_get_multiple( $keys, $group = '', $force = false ) {
return $wp_object_cache->get_multiple( $keys, $group, $force );
}

/**
* Removes all cache items from the in-memory runtime cache.
*
* @see WP_Object_Cache::flush()
*
* @return bool True on success, false on failure.
*/
function wp_cache_flush_runtime() {
return wp_cache_flush();
}

/**
* Removes all cache items in a group, if the object cache implementation supports it.
*
* Before calling this function, always check for group flushing support using the
* `wp_cache_supports( 'flush_group' )` function.
*
* @see WP_Object_Cache::flush_group()
* @global WP_Object_Cache $wp_object_cache Object cache global instance.
*
* @param string $group Name of group to remove from cache.
* @return bool True if group was flushed, false otherwise.
*/
function wp_cache_flush_group( $group ) {
global $wp_object_cache;

return $wp_object_cache->flush_group( $group );
}

/**
* Increment numeric cache item's value
*
Expand Down Expand Up @@ -302,13 +331,13 @@ function wp_cache_reset() {
function wp_cache_supports( $feature ) {
switch ( $feature ) {
case 'get_multiple':
case 'flush_runtime':
case 'flush_group':
return true;

case 'add_multiple':
case 'set_multiple':
case 'delete_multiple':
case 'flush_runtime':
case 'flush_group':
default:
return false;
}
Expand Down Expand Up @@ -637,6 +666,31 @@ public function flush( $redis = true ) {
return true;
}

/**
* Removes all cache items in a group.
*
* @param string $group Name of group to remove from cache.
* @return true Always returns true.
*/
public function flush_group( $group ) {
if ( ! $this->_should_use_redis_hashes( $group ) ) {
return false;
}

$multisite_safe_group = $this->multisite && ! isset( $this->global_groups[ $group ] ) ? $this->blog_prefix . $group : $group;
$redis_safe_group = $this->_key( '', $group );
if ( $this->_should_persist( $group ) ) {
$result = $this->_call_redis( 'del', $redis_safe_group );
if ( 1 !== $result ) {
return false;
}
} elseif ( ! $this->_should_persist( $group ) && ! isset( $this->cache[ $multisite_safe_group ] ) ) {
return false;
}
unset( $this->cache[ $multisite_safe_group ] );
return true;
}

/**
* Retrieves the cache contents, if it exists
*
Expand Down
8 changes: 7 additions & 1 deletion readme.txt
Expand Up @@ -3,7 +3,7 @@ Contributors: getpantheon, danielbachhuber, mboynes, Outlandish Josh, jspellman,
Tags: cache, plugin, redis
Requires at least: 3.0.1
Tested up to: 6.2
Stable tag: 1.3.6-dev
Stable tag: 1.4.0-dev
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

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

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

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

= 0.1 =
* Initial commit of working code for the benefit of all.

== Upgrade Notice ==

= Latest =
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.
61 changes: 61 additions & 0 deletions tests/phpunit/test-cache.php
Expand Up @@ -391,6 +391,67 @@ public function test_flush() {
}
}

public function test_wp_cache_flush_runtime() {
// Add some data to the cache
$data = array(
rand_str() => rand_str(),
rand_str() => rand_str()
);

foreach ( $data as $key => $value ) {
wp_cache_set( $key, $value, 'test_wp_cache_flush_runtime' );
}

// Verify that the cache contains the data
foreach ( $data as $key => $value ) {
$this->assertEquals( $value, wp_cache_get( $key, 'test_wp_cache_flush_runtime' ) );
}

// Flush the cache
wp_cache_flush_runtime();

// Verify that the cache is now empty
foreach ($data as $key => $value) {
$this->assertFalse( wp_cache_get( $key, 'test_wp_cache_flush_runtime' ) );
}
}

public function test_wp_cache_flush_group() {
$key1 = rand_str();
$val1 = rand_str();
$key2 = rand_str();
$val2 = rand_str();
$key3 = rand_str();
$val3 = rand_str();
$group = 'foo';
$group2 = 'bar';

if ( ! defined( 'WP_REDIS_USE_CACHE_GROUPS' ) || ! WP_REDIS_USE_CACHE_GROUPS ) {
$GLOBALS['wp_object_cache']->add_redis_hash_groups( array( $group, $group2 ) );
}

// Set up the values
wp_cache_set( $key1, $val1, $group );
wp_cache_set( $key2, $val2, $group );
wp_cache_set( $key3, $val3, $group2 );
$this->assertEquals( $val1, wp_cache_get( $key1, $group ) );
$this->assertEquals( $val2, wp_cache_get( $key2, $group ) );
$this->assertEquals( $val3, wp_cache_get( $key3, $group2 ) );

$this->assertTrue( wp_cache_flush_group( $group ) );

$this->assertFalse( wp_cache_get( $key1, $group ) );
$this->assertFalse( wp_cache_get( $key2, $group ) );
$this->assertEquals( $val3, wp_cache_get( $key3, $group2 ) );

// _call_redis( 'delete' ) always returns true when Redis isn't available
if ( class_exists( 'Redis' ) ) {
$this->assertFalse( wp_cache_flush_group( $group ) );
} else {
$this->assertTrue( wp_cache_flush_group( $group ) );
}
}

// Make sure objects are cloned going to and from the cache
public function test_object_refs() {
$key = rand_str();
Expand Down
2 changes: 1 addition & 1 deletion wp-redis.php
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: WP Redis
* Plugin URI: http://github.com/pantheon-systems/wp-redis/
* Description: WordPress Object Cache using Redis. Requires the PhpRedis extension (https://github.com/phpredis/phpredis).
* Version: 1.3.6-dev
* Version: 1.4.0-dev
* Author: Pantheon, Josh Koenig, Matthew Boynes, Daniel Bachhuber, Alley Interactive
* Author URI: https://pantheon.io/
*/
Expand Down