Skip to content

Commit ce9fd87

Browse files
committed
Feeds: Cache RSS feeds in global transients.
Moves the caching of RSS feeds requested via `fetch_feed()` from single site transients (`get|set|delete_transient()`) to global transients (`get|set|delete_site_transient()`). On multisite installs of WordPress, this replaces per site caching with the global multisite cache to allow a single cache to be shared between all sites. This reduces the amount of data stored in the database and improves performance of feeds when multiple sites are ingesting the same URL. Props rollybueno, spacedmonkey, peterwilsoncc. Fixes #63719. git-svn-id: https://develop.svn.wordpress.org/trunk@60524 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 965ee61 commit ce9fd87

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

src/wp-includes/class-wp-feed-cache-transient.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*
1313
* @since 2.8.0
1414
* @since 6.7.0 Now properly implements the SimplePie\Cache\Base interface.
15+
* @since 6.9.0 Switched to Multisite's global cache via the `*_site_transient()` functions.
1516
*/
1617
#[AllowDynamicProperties]
1718
class WP_Feed_Cache_Transient implements SimplePie\Cache\Base {
@@ -84,8 +85,8 @@ public function save( $data ) {
8485
$data = $data->data;
8586
}
8687

87-
set_transient( $this->name, $data, $this->lifetime );
88-
set_transient( $this->mod_name, time(), $this->lifetime );
88+
set_site_transient( $this->name, $data, $this->lifetime );
89+
set_site_transient( $this->mod_name, time(), $this->lifetime );
8990
return true;
9091
}
9192

@@ -97,7 +98,7 @@ public function save( $data ) {
9798
* @return array Data for `SimplePie::$data`.
9899
*/
99100
public function load() {
100-
return get_transient( $this->name );
101+
return get_site_transient( $this->name );
101102
}
102103

103104
/**
@@ -108,7 +109,7 @@ public function load() {
108109
* @return int Timestamp.
109110
*/
110111
public function mtime() {
111-
return get_transient( $this->mod_name );
112+
return get_site_transient( $this->mod_name );
112113
}
113114

114115
/**
@@ -119,7 +120,7 @@ public function mtime() {
119120
* @return bool False if value was not set and true if value was set.
120121
*/
121122
public function touch() {
122-
return set_transient( $this->mod_name, time(), $this->lifetime );
123+
return set_site_transient( $this->mod_name, time(), $this->lifetime );
123124
}
124125

125126
/**
@@ -130,8 +131,8 @@ public function touch() {
130131
* @return true Always true.
131132
*/
132133
public function unlink() {
133-
delete_transient( $this->name );
134-
delete_transient( $this->mod_name );
134+
delete_site_transient( $this->name );
135+
delete_site_transient( $this->mod_name );
135136
return true;
136137
}
137138
}

tests/phpunit/tests/feed/fetchFeed.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,31 @@ public function test_fetch_feed_cached() {
5757
$this->assertEquals( 1, $filter->get_call_count(), 'The feed should be cached on the second call. For SP 1.8.x upgrades, backport simplepie/simplepie#830 to resolve.' );
5858
}
5959

60+
/**
61+
* Ensure that fetch_feed uses the global cache on Multisite.
62+
*
63+
* @ticket 63719
64+
*
65+
* @group feed
66+
* @group ms-required
67+
*
68+
* @covers ::fetch_feed
69+
* @covers WP_Feed_Cache_Transient
70+
*/
71+
public function test_fetch_feed_uses_global_cache() {
72+
$second_blog_id = self::factory()->blog->create();
73+
74+
$filter = new MockAction();
75+
add_filter( 'pre_http_request', array( $filter, 'filter' ) );
76+
77+
fetch_feed( 'https://wordpress.org/news/feed/' );
78+
79+
switch_to_blog( $second_blog_id );
80+
81+
fetch_feed( 'https://wordpress.org/news/feed/' );
82+
$this->assertEquals( 1, $filter->get_call_count(), 'The feed cache should be global.' );
83+
}
84+
6085
/**
6186
* Mock response for `fetch_feed()`.
6287
*

0 commit comments

Comments
 (0)