Skip to content

Commit

Permalink
Stats: Use option value instead of transient for cache buster (#39887)
Browse files Browse the repository at this point in the history
  • Loading branch information
kangzj authored and matticbot committed Oct 24, 2024
1 parent c0bcee7 commit c63cfb7
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 98 deletions.
60 changes: 30 additions & 30 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/automattic/jetpack-mu-wpcom/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"automattic/jetpack-connection": "^5.1.4",
"automattic/jetpack-masterbar": "^0.9.6",
"automattic/jetpack-redirect": "^2.0.4",
"automattic/jetpack-stats-admin": "^0.22.3",
"automattic/jetpack-stats-admin": "^0.22.4-alpha",
"automattic/jetpack-status": "^4.0.2",
"automattic/scheduled-updates": "^0.13.4",
"automattic/jetpack-compat": "^3.0.2",
Expand Down
7 changes: 7 additions & 0 deletions vendor/automattic/jetpack-stats-admin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.22.4-alpha - unreleased

This is an alpha version! The changes listed here are not final.

### Changed
- Odyssey Stats cache busting: use optioin instead of transient

## 0.22.3 - 2024-10-21
### Changed
- JITM: Expose function to render message. [#39714]
Expand Down
2 changes: 1 addition & 1 deletion vendor/automattic/jetpack-stats-admin/src/class-main.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Main {
/**
* Stats version.
*/
const VERSION = '0.22.3';
const VERSION = '0.22.4-alpha';

/**
* Singleton Main instance.
Expand Down
40 changes: 31 additions & 9 deletions vendor/automattic/jetpack-stats-admin/src/class-odyssey-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,36 +75,58 @@ public function load_admin_scripts( $asset_handle, $asset_name, $options = array
/**
* Returns cache buster string for assets.
* Development mode doesn't need this, as it's handled by `Assets` class.
*
* @return string
*/
protected function get_cdn_asset_cache_buster() {
$now_in_ms = floor( microtime( true ) * 1000 );
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['force_refresh'] ) ) {
set_transient( self::ODYSSEY_STATS_CACHE_BUSTER_CACHE_KEY, floor( microtime( true ) * 1000 ), 15 * MINUTE_IN_SECONDS );
update_option( self::ODYSSEY_STATS_CACHE_BUSTER_CACHE_KEY, $this->get_cache_buster_option_value( $now_in_ms ), false );
}

// Use cached cache buster in production.
$remote_asset_version = get_transient( self::ODYSSEY_STATS_CACHE_BUSTER_CACHE_KEY );
$remote_asset_version = get_option( self::ODYSSEY_STATS_CACHE_BUSTER_CACHE_KEY );

if ( ! empty( $remote_asset_version ) ) {
return $remote_asset_version;
$remote_asset_version = json_decode( $remote_asset_version, true );
// If cache buster is cached and not expired (valid in 15 min), return it.
if ( ! empty( $remote_asset_version['cache_buster'] ) && $remote_asset_version['cached_at'] > $now_in_ms - MINUTE_IN_SECONDS * 1000 * 15 ) {
return $remote_asset_version['cache_buster'];
}
}

// If no cached cache buster, we fetch it from CDN and set to transient.
$response = wp_remote_get( sprintf( self::ODYSSEY_CDN_URL, self::ODYSSEY_STATS_VERSION, 'build_meta.json?t=' . time() ), array( 'timeout' => 5 ) );
$response = wp_remote_get( sprintf( self::ODYSSEY_CDN_URL, self::ODYSSEY_STATS_VERSION, 'build_meta.json?t=' . $now_in_ms ), array( 'timeout' => 5 ) );

if ( is_wp_error( $response ) ) {
// fallback to the package version.
return Main::VERSION;
// fallback to current timestamp.
return (string) $now_in_ms;
}

$build_meta = json_decode( wp_remote_retrieve_body( $response ), true );
if ( ! empty( $build_meta['cache_buster'] ) ) {
// Cache the cache buster for 15 mins.
set_transient( self::ODYSSEY_STATS_CACHE_BUSTER_CACHE_KEY, $build_meta['cache_buster'], 15 * MINUTE_IN_SECONDS );
update_option( self::ODYSSEY_STATS_CACHE_BUSTER_CACHE_KEY, $this->get_cache_buster_option_value( $build_meta['cache_buster'] ), false );
return $build_meta['cache_buster'];
}

// fallback to the package version.
return Main::VERSION;
// fallback to current timestamp.
return (string) $now_in_ms;
}

/**
* Get the cache buster option value.
*
* @param string|int|float $cache_buster The cache buster.
* @return string|false
*/
protected function get_cache_buster_option_value( $cache_buster ) {
return wp_json_encode(
array(
'cache_buster' => (string) $cache_buster,
'cached_at' => floor( microtime( true ) * 1000 ), // milliseconds.
)
);
}
}
Loading

0 comments on commit c63cfb7

Please sign in to comment.