From fdccba9b52e873dde4e8c6a33597959013a7129d Mon Sep 17 00:00:00 2001 From: divyesh-addweb Date: Thu, 3 Dec 2026 16:27:23 +0530 Subject: [PATCH 1/2] Cache API: Respect the expiration time in the default object cache. Keys stored with an expiration now expire during long-running requests. See #55847. --- src/wp-includes/class-wp-object-cache.php | 38 ++++++-- tests/phpunit/tests/cache.php | 104 ++++++++++++++++++++++ 2 files changed, 134 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/class-wp-object-cache.php b/src/wp-includes/class-wp-object-cache.php index 0ac9e10dcde3c..1db403a654abf 100644 --- a/src/wp-includes/class-wp-object-cache.php +++ b/src/wp-includes/class-wp-object-cache.php @@ -34,6 +34,14 @@ class WP_Object_Cache { */ private $cache = array(); + /** + * Expiration timestamps, only for keys stored with an expiration. + * + * @since 7.3.0 + * @var array + */ + private $expirations = array(); + /** * The amount of times the cache data was already stored in the cache. * @@ -183,6 +191,11 @@ protected function is_valid_key( $key ) { * @return bool Whether the key exists in the cache for the given group. */ protected function _exists( $key, $group ) { + if ( isset( $this->expirations[ $group ][ $key ] ) && time() >= $this->expirations[ $group ][ $key ] ) { + unset( $this->cache[ $group ][ $key ], $this->expirations[ $group ][ $key ] ); + return false; + } + return isset( $this->cache[ $group ] ) && ( isset( $this->cache[ $group ][ $key ] ) || array_key_exists( $key, $this->cache[ $group ] ) ); } @@ -292,17 +305,18 @@ public function replace( $key, $data, $group = 'default', $expire = 0 ) { * the group should be used with care and should follow normal function * naming guidelines outside of core WordPress usage. * - * The $expire parameter is not used, because the cache will automatically - * expire for each time a page is accessed and PHP finishes. The method is - * more for cache plugins which use files. + * The cache is discarded when PHP finishes. An $expire value greater than + * zero also expires the contents during long-running requests. * * @since 2.0.0 * @since 6.1.0 Returns false if cache key is invalid. + * @since 7.3.0 The $expire parameter is respected. * * @param int|string $key What to call the contents in the cache. * @param mixed $data The contents to store in the cache. * @param string $group Optional. Where to group the cache contents. Default 'default'. - * @param int $expire Optional. Not used. + * @param int $expire Optional. When to expire the cache contents, in seconds. + * Default 0 (no expiration). * @return bool True if contents were set, false if key is invalid. */ public function set( $key, $data, $group = 'default', $expire = 0 ) { @@ -323,6 +337,13 @@ public function set( $key, $data, $group = 'default', $expire = 0 ) { } $this->cache[ $group ][ $key ] = $data; + + if ( (int) $expire > 0 ) { + $this->expirations[ $group ][ $key ] = time() + (int) $expire; + } else { + unset( $this->expirations[ $group ][ $key ] ); + } + return true; } @@ -447,7 +468,7 @@ public function delete( $key, $group = 'default', $deprecated = false ) { return false; } - unset( $this->cache[ $group ][ $key ] ); + unset( $this->cache[ $group ][ $key ], $this->expirations[ $group ][ $key ] ); return true; } @@ -565,7 +586,8 @@ public function decr( $key, $offset = 1, $group = 'default' ) { * @return true Always returns true. */ public function flush() { - $this->cache = array(); + $this->cache = array(); + $this->expirations = array(); return true; } @@ -579,7 +601,7 @@ public function flush() { * @return true Always returns true. */ public function flush_group( $group ) { - unset( $this->cache[ $group ] ); + unset( $this->cache[ $group ], $this->expirations[ $group ] ); return true; } @@ -626,7 +648,7 @@ public function reset() { // Clear out non-global caches since the blog ID has changed. foreach ( array_keys( $this->cache ) as $group ) { if ( ! isset( $this->global_groups[ $group ] ) ) { - unset( $this->cache[ $group ] ); + unset( $this->cache[ $group ], $this->expirations[ $group ] ); } } } diff --git a/tests/phpunit/tests/cache.php b/tests/phpunit/tests/cache.php index 7ca6de5ec8eda..0d625103557c6 100644 --- a/tests/phpunit/tests/cache.php +++ b/tests/phpunit/tests/cache.php @@ -502,4 +502,108 @@ public function test_wp_cache_delete_multiple() { // Verify that foo3 in group2 was untouched. $this->assertSame( 'bar', wp_cache_get( 'foo3', 'group2' ) ); } + + /** + * Moves all stored expiration timestamps into the past. + */ + private function expire_cache_keys() { + $expirations = $this->cache->expirations; + + foreach ( $expirations as $group => $keys ) { + foreach ( $keys as $key => $time ) { + $expirations[ $group ][ $key ] = time() - 1; + } + } + + $this->cache->expirations = $expirations; + } + + /** + * @ticket 55847 + */ + public function test_expired_key_is_a_cache_miss() { + if ( wp_using_ext_object_cache() ) { + $this->markTestSkipped( 'This test requires that an external object cache is not in use.' ); + } + + $this->cache->set( 'foo', 'bar', 'group', 60 ); + $this->assertSame( 'bar', $this->cache->get( 'foo', 'group' ) ); + + $this->expire_cache_keys(); + $misses = $this->cache->cache_misses; + + $this->assertFalse( $this->cache->get( 'foo', 'group', false, $found ) ); + $this->assertFalse( $found ); + $this->assertSame( $misses + 1, $this->cache->cache_misses ); + } + + /** + * @ticket 55847 + */ + public function test_expiration_is_cleared_when_key_is_set_without_one() { + if ( wp_using_ext_object_cache() ) { + $this->markTestSkipped( 'This test requires that an external object cache is not in use.' ); + } + + $this->cache->set( 'foo', 'bar', 'group', 60 ); + $this->cache->set( 'foo', 'baz', 'group' ); + $this->expire_cache_keys(); + + $this->assertSame( 'baz', $this->cache->get( 'foo', 'group' ) ); + } + + /** + * @ticket 55847 + */ + public function test_expired_key_can_be_added_but_not_replaced_or_deleted() { + if ( wp_using_ext_object_cache() ) { + $this->markTestSkipped( 'This test requires that an external object cache is not in use.' ); + } + + $this->cache->set( 'foo', 'bar', 'group', 60 ); + $this->expire_cache_keys(); + + $this->assertFalse( $this->cache->replace( 'foo', 'baz', 'group' ) ); + $this->assertFalse( $this->cache->delete( 'foo', 'group' ) ); + $this->assertTrue( $this->cache->add( 'foo', 'baz', 'group' ) ); + $this->assertSame( 'baz', $this->cache->get( 'foo', 'group' ) ); + } + + /** + * @ticket 55847 + */ + public function test_incr_and_decr_keep_the_expiration() { + if ( wp_using_ext_object_cache() ) { + $this->markTestSkipped( 'This test requires that an external object cache is not in use.' ); + } + + $this->cache->set( 'foo', 5, 'group', 60 ); + $this->assertSame( 6, $this->cache->incr( 'foo', 1, 'group' ) ); + $this->assertSame( 5, $this->cache->decr( 'foo', 1, 'group' ) ); + $this->expire_cache_keys(); + + $this->assertFalse( $this->cache->incr( 'foo', 1, 'group' ) ); + $this->assertFalse( $this->cache->decr( 'foo', 1, 'group' ) ); + } + + /** + * @ticket 55847 + */ + public function test_delete_and_flush_clear_expirations() { + if ( wp_using_ext_object_cache() ) { + $this->markTestSkipped( 'This test requires that an external object cache is not in use.' ); + } + + $this->cache->set( 'one', 1, 'group', 60 ); + $this->cache->delete( 'one', 'group' ); + $this->assertEmpty( array_filter( $this->cache->expirations ) ); + + $this->cache->set( 'two', 2, 'group', 60 ); + $this->cache->flush_group( 'group' ); + $this->assertEmpty( array_filter( $this->cache->expirations ) ); + + $this->cache->set( 'three', 3, 'group', 60 ); + $this->cache->flush(); + $this->assertSame( array(), $this->cache->expirations ); + } } From e3fb8ae6170bd6f068d917e3aee2c91efbbb4964 Mon Sep 17 00:00:00 2001 From: divyesh-addweb Date: Thu, 3 Dec 2026 17:05:03 +0530 Subject: [PATCH 2/2] Cache API: Keep cached values for at least their full expiration time. See #55847. --- src/wp-includes/class-wp-object-cache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-object-cache.php b/src/wp-includes/class-wp-object-cache.php index 1db403a654abf..fb4d347153b98 100644 --- a/src/wp-includes/class-wp-object-cache.php +++ b/src/wp-includes/class-wp-object-cache.php @@ -191,7 +191,7 @@ protected function is_valid_key( $key ) { * @return bool Whether the key exists in the cache for the given group. */ protected function _exists( $key, $group ) { - if ( isset( $this->expirations[ $group ][ $key ] ) && time() >= $this->expirations[ $group ][ $key ] ) { + if ( isset( $this->expirations[ $group ][ $key ] ) && time() > $this->expirations[ $group ][ $key ] ) { unset( $this->cache[ $group ][ $key ], $this->expirations[ $group ][ $key ] ); return false; }