Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions src/wp-includes/class-wp-object-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 ] ) );
}

Expand Down Expand Up @@ -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 ) {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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 ] );
}
}
}
Expand Down
104 changes: 104 additions & 0 deletions tests/phpunit/tests/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
Loading