diff --git a/README.md b/README.md index 82fdb8a..40df3d9 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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) ### @@ -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. diff --git a/object-cache.php b/object-cache.php index e849c2c..5211a69 100644 --- a/object-cache.php +++ b/object-cache.php @@ -157,6 +157,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 * @@ -304,13 +333,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; } @@ -630,6 +659,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 * diff --git a/readme.txt b/readme.txt index 9d5ccfc..552219b 100644 --- a/readme.txt +++ b/readme.txt @@ -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 @@ -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) = @@ -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. \ No newline at end of file diff --git a/tests/phpunit/test-cache.php b/tests/phpunit/test-cache.php index 1e5edca..fac2e28 100644 --- a/tests/phpunit/test-cache.php +++ b/tests/phpunit/test-cache.php @@ -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(); diff --git a/wp-redis.php b/wp-redis.php index 95fc714..ae9dd48 100644 --- a/wp-redis.php +++ b/wp-redis.php @@ -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/ */