Skip to content

Commit cc43e64

Browse files
committed
improve cache clearing storage
1 parent 02c52d7 commit cc43e64

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

inc/admin.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function __construct() {
5757
add_action( 'admin_notices', [ $this, 'add_notice_conflicts' ] );
5858
add_action( 'optml_daily_sync', [ $this, 'daily_sync' ] );
5959
add_action( 'admin_init', [ $this, 'redirect_old_dashboard' ] );
60-
60+
add_action( 'optml_purge_image_cache', [ $this, 'purge_image_cache' ] );
6161
if ( $this->settings->is_connected() ) {
6262
add_action( 'init', [ $this, 'check_domain_change' ] );
6363
add_action( 'optml_pull_image_data_init', [ $this, 'pull_image_data_init' ] );
@@ -100,6 +100,18 @@ public function __construct() {
100100
add_filter( 'themeisle-sdk/survey/' . OPTML_PRODUCT_SLUG, [ $this, 'get_survey_metadata' ], 10, 2 );
101101
}
102102

103+
/**
104+
* Function that purges the image cache for a specific file.
105+
*
106+
* @param string $file Path or url of the file to clear.
107+
*
108+
* @return void
109+
*/
110+
public function purge_image_cache( $file ) {
111+
$basename = wp_basename( $file );
112+
$settings = new Optml_Settings();
113+
$settings->clear_cache( $basename );
114+
}
103115
/**
104116
* Listen when the file is updated and clear the cache for the file.
105117
*
@@ -109,9 +121,8 @@ public function __construct() {
109121
* @return string The file path.
110122
*/
111123
public function listen_update_file( $file, $post_id ) {
112-
$basename = wp_basename( $file );
113-
$settings = new Optml_Settings();
114-
$settings->clear_cache( $basename );
124+
// Purge the image cache.
125+
do_action( 'optml_purge_image_cache', $file );
115126
return $file;
116127
}
117128
/**

inc/settings.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Optml_Settings {
1616
const FILTER_TYPE_LAZYLOAD = 'lazyload';
1717
const FILTER_TYPE_OPTIMIZE = 'optimize';
1818
const OPTML_USER_EMAIL = 'optml_user_email';
19+
const INDIVIDUAL_CACHE_TOKENS_KEY = '_optml_cache_tokens_individual';
1920
/**
2021
* Holds an array of possible settings to alter via wp cli or wp-config constants.
2122
*
@@ -744,15 +745,16 @@ public function clear_cache( $type = '' ) {
744745
$token = $this->get( 'cache_buster' );
745746
$token_images = $this->get( 'cache_buster_images' );
746747

748+
// here is an individual cache tokens
749+
$individual = get_transient( self::INDIVIDUAL_CACHE_TOKENS_KEY ) ?: [];
747750
if ( ( empty( $type ) || $type === 'images' ) ) {
748751
if ( ! empty( $token_images ) ) {
749752
$token = $token_images;
750753
}
751754
} elseif ( $type === 'assets' ) {
752755
$token = $this->get( 'cache_buster_assets' );
753756
} else {
754-
// here is an individual clear cache based on filename.
755-
$token = get_transient( '_file_' . crc32( $type ) ) ?: '';
757+
$token = $individual[ crc32( $type ) ] ?? $token_images ?: $token;
756758
}
757759

758760
$request = new Optml_Api();
@@ -776,11 +778,14 @@ public function clear_cache( $type = '' ) {
776778
if ( empty( $type ) || $type === 'images' ) {
777779
set_transient( 'optml_cache_lock', 'yes', 5 * MINUTE_IN_SECONDS );
778780
$this->update( 'cache_buster_images', $data['token'] );
781+
// we delete individual cache tokens since this is a global cache clear.
782+
delete_transient( self::INDIVIDUAL_CACHE_TOKENS_KEY );
779783
} elseif ( $type === 'assets' ) {
780784
set_transient( 'optml_cache_lock_assets', 'yes', 5 * MINUTE_IN_SECONDS );
781785
$this->update( 'cache_buster_assets', $data['token'] );
782786
} else {
783-
set_transient( '_file_' . crc32( $type ), $data['token'], 6 * HOUR_IN_SECONDS );
787+
$individual[ crc32( $type ) ] = $data['token'];
788+
set_transient( self::INDIVIDUAL_CACHE_TOKENS_KEY, $individual, 6 * HOUR_IN_SECONDS );
784789
}
785790

786791
return $data['token'];

inc/url_replacer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ private function normalize_image( $url, $original_url, $args, $is_uploaded = fal
284284
* @return string
285285
*/
286286
public static function get_active_cache_booster( $url, $main_cache_buster ) {
287-
return get_transient( '_file_' . crc32( wp_basename( $url ) ) ) ?: $main_cache_buster;
287+
return ( get_transient( Optml_Settings::INDIVIDUAL_CACHE_TOKENS_KEY ) ?: [] )[ crc32( wp_basename( $url ) ) ] ?? $main_cache_buster;
288288
}
289289
/**
290290
* Throw error on object clone

0 commit comments

Comments
 (0)