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

Add cache preloading functionality #360

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Nginx Helper #
[![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)

**Contributors:** rtcamp, rahul286, saurabhshukla, manishsongirkar36, faishal, desaiuditd, darren-slatten, jk3us, daankortenbach, telofy, pjv, llonchj, jinnko, weskoop, bcole808, gungeekatx, rohanveer, chandrapatel, gagan0123, ravanh, michaelbeil, samedwards, niwreg, entr, nuvoPoint, iam404, rittesh.patel, vishalkakadiya, BhargavBhandari90, vincent-lu, murrayjbrown, bryant1410, 1gor, matt-h, pySilver, johan-chassaing, dotsam, sanketio, petenelson, nathanielks, rigagoogoo, dslatten, jinschoi, kelin1003, vaishuagola27, rahulsprajapati, Joel-James, utkarshpatel, gsayed786, shashwatmittal, sudhiryadav, thrijith, stayallive, jaredwsmith, abhijitrakas, umeshnevase, sid177, souptik, arafatkn, subscriptiongroup, akrocks
**Contributors:** rtcamp, rahul286, saurabhshukla, manishsongirkar36, faishal, desaiuditd, darren-slatten, jk3us, daankortenbach, telofy, pjv, llonchj, jinnko, weskoop, bcole808, gungeekatx, rohanveer, chandrapatel, gagan0123, ravanh, michaelbeil, samedwards, niwreg, entr, nuvoPoint, iam404, rittesh.patel, vishalkakadiya, BhargavBhandari90, vincent-lu, murrayjbrown, bryant1410, 1gor, matt-h, pySilver, johan-chassaing, dotsam, sanketio, petenelson, nathanielks, rigagoogoo, dslatten, jinschoi, kelin1003, vaishuagola27, rahulsprajapati, Joel-James, utkarshpatel, gsayed786, shashwatmittal, sudhiryadav, thrijith, stayallive, jaredwsmith, abhijitrakas, umeshnevase, sid177, souptik, arafatkn, subscriptiongroup, akrocks, Vedant-Gandhi

**Tags:** nginx, cache, purge, nginx map, nginx cache, maps, fastcgi, proxy, redis, redis-cache, rewrite, permalinks

Expand Down
97 changes: 97 additions & 0 deletions admin/class-nginx-helper-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ public function nginx_helper_default_settings() {
'redis_prefix' => 'nginx-cache:',
'purge_url' => '',
'redis_enabled_by_constant' => 0,
'preload_cache' => 0,
'is_cache_preloaded' => 0
);

}
Expand Down Expand Up @@ -775,5 +777,100 @@ public function purge_all() {
public function display_notices() {
echo '<div class="updated"><p>' . esc_html__( 'Purge initiated', 'nginx-helper' ) . '</p></div>';
}

/**
* Preloads the cache for the website.
*
* @return void
*/
public function preload_cache() {
$is_cache_preloaded = $this->options['is_cache_preloaded'];
$preload_cache_enabled = $this->options['preload_cache'];

if ( $preload_cache_enabled && false === boolval( $is_cache_preloaded ) ) {
$this->options['is_cache_preloaded'] = true;

update_site_option( 'rt_wp_nginx_helper_options', $this->options );
$this->preload_cache_from_sitemap();
}
}

/**
* This function preloads the cache from sitemap url.
*
* @return void
*/
private function preload_cache_from_sitemap() {

$sitemap_urls = $this->get_index_sitemap_urls();
$all_urls = array();

foreach ( $sitemap_urls as $sitemap_url ) {
$urls = $this->extract_sitemap_urls( $sitemap_url );
$all_urls = array_merge( $all_urls, $urls );
}

$args = array(
'timeout' => 1,
'blocking' => false,
'sslverify' => false,
);

foreach ( $all_urls as $url ) {
wp_remote_get( esc_url_raw( $url ), $args );
}

}

/**
* Fetches all the sitemap urls for the site.
*
* @return array
*/
private function get_index_sitemap_urls() {
$sitemaps = wp_sitemaps_get_server()->index->get_sitemap_list();
$urls = array();
foreach ( $sitemaps as $sitemap ) {
$urls[] = $sitemap['loc'];
}
return $urls;
}

/**
* Parse sitemap content and extract all URLs.
*
* @param string $sitemap_url The URL of the sitemap.
* @return array|WP_Error An array of URLs or WP_Error on failure.
*/
private function extract_sitemap_urls( $sitemap_url ) {
$response = wp_remote_get( $sitemap_url );

$urls = array();

if ( is_wp_error( $response ) ) {
return $urls;
}

$sitemap_content = wp_remote_retrieve_body( $response );

libxml_use_internal_errors( true );
$xml = simplexml_load_string( $sitemap_content );

if ( false === $xml ) {
return new WP_Error( 'sitemap_parse_error', esc_html__( 'Failed to parse the sitemap XML', 'nginx-helper' ) );
}

$urls = array();

if ( $xml === false ) {
return $urls;
}

foreach ( $xml->url as $url ) {
$urls[] = (string) $url->loc;
}

return $urls;
}

}
7 changes: 7 additions & 0 deletions admin/partials/nginx-helper-general-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
'purge_page_on_deleted_comment',
'purge_feeds',
'smart_http_expire_form_nonce',
'preload_cache',
);

$all_inputs = array();
Expand Down Expand Up @@ -126,6 +127,12 @@
<label for="enable_purge"><?php esc_html_e( 'Enable Purge', 'nginx-helper' ); ?></label>
</td>
</tr>
<tr valign="top">
<td>
<input type="checkbox" value="1" id="preload_cache" name="preload_cache" <?php checked( $nginx_helper_settings['preload_cache'], 1 ); ?> />
<label for="preload_cache"><?php esc_html_e( 'Preload Cache', 'nginx-helper' ); ?></label>
</td>
</tr>
</table>
</div> <!-- End of .inside -->
</div>
Expand Down
3 changes: 3 additions & 0 deletions includes/class-nginx-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ private function define_admin_hooks() {

// expose action to allow other plugins to purge the cache.
$this->loader->add_action( 'rt_nginx_helper_purge_all', $nginx_purger, 'purge_all' );

// add action to preload the cache
$this->loader->add_action( 'admin_init', $nginx_helper_admin, 'preload_cache' );
}

/**
Expand Down
Loading