WordPress自定义警报过期后LiteSpeed指定页面缓存清除异常
问题:LiteSpeed缓存全站被清除而非指定页面
我在WordPress中实现了自定义功能:借助自定义字段alert_expiration_date,当警报横幅(custom_alert自定义文章类型)过期时自动将其设为“草稿”状态。原本希望仅清除与该警报关联的页面(由alert_pages自定义字段定义)的LiteSpeed缓存,而非全站缓存,但当前执行时全站缓存都被清掉了。
原代码如下:
add_filter('cron_schedules', 'add_custom_cron_schedule'); function add_custom_cron_schedule($schedules) { if (!isset($schedules["every_two_minutes"])) { $schedules['every_two_minutes'] = array( 'interval' => 120, // 2 minutes en secondes 'display' => esc_html__('Every Two Minutes') ); } return $schedules; } if (!wp_next_scheduled('expire_alerts_check')) { wp_schedule_event(time(), 'every_two_minutes', 'expire_alerts_check'); } add_action('expire_alerts_check', 'expire_and_purge_function'); function expire_and_purge_function() { $today = date('Y-m-d H:i:s', current_time('timestamp', 0)); $args = [ 'post_type' => 'custom_alert', 'posts_per_page' => -1, 'post_status' => 'publish', 'meta_query' => [ [ 'key' => 'alert_expiration_date', 'value' => $today, 'compare' => '<=', 'type' => 'DATETIME' ] ] ]; $posts = get_posts($args); foreach ($posts as $post) { $expiration_date = get_field('alert_expiration_date', $post->ID); if ($expiration_date && strtotime($expiration_date) <= strtotime($today)) { wp_update_post([ 'ID' => $post->ID, 'post_status' => 'draft' ]); $pages_to_update = get_field('alert_pages', $post->ID); if (!empty($pages_to_update)) { purge_pages_cache($pages_to_update); } } } } function purge_pages_cache($pages_to_update) { foreach ($pages_to_update as $page_id) { do_action('litespeed_purge_post', $page_id); log_purge_action($page_id); } }
问题原因
LiteSpeed默认会在自定义文章类型的状态发生变更时,自动触发全站缓存清除。当调用wp_update_post将custom_alert文章设为草稿时,LiteSpeed的内置钩子会被触发,导致全站缓存被清空,覆盖了我们仅清除指定页面的逻辑。
修复方案
在更新custom_alert文章状态前,临时禁用LiteSpeed的自动缓存清除钩子,更新完成后再恢复。确保只有指定页面的缓存被清除。
修复后的完整代码:
add_filter('cron_schedules', 'add_custom_cron_schedule'); function add_custom_cron_schedule($schedules) { if (!isset($schedules["every_two_minutes"])) { $schedules['every_two_minutes'] = array( 'interval' => 120, // 2分钟(秒) 'display' => esc_html__('每两分钟') ); } return $schedules; } if (!wp_next_scheduled('expire_alerts_check')) { wp_schedule_event(time(), 'every_two_minutes', 'expire_alerts_check'); } add_action('expire_alerts_check', 'expire_and_purge_function'); function expire_and_purge_function() { $today = date('Y-m-d H:i:s', current_time('timestamp', 0)); $args = [ 'post_type' => 'custom_alert', 'posts_per_page' => -1, 'post_status' => 'publish', 'meta_query' => [ [ 'key' => 'alert_expiration_date', 'value' => $today, 'compare' => '<=', 'type' => 'DATETIME' ] ] ]; $posts = get_posts($args); foreach ($posts as $post) { $expiration_date = get_field('alert_expiration_date', $post->ID); if ($expiration_date && strtotime($expiration_date) <= strtotime($today)) { // 临时禁用LiteSpeed自动缓存清除钩子 add_filter('litespeed_disable_all_hooks', '__return_true'); // 更新文章状态为草稿 wp_update_post([ 'ID' => $post->ID, 'post_status' => 'draft' ]); // 恢复LiteSpeed钩子 remove_filter('litespeed_disable_all_hooks', '__return_true'); $pages_to_update = get_field('alert_pages', $post->ID); if (!empty($pages_to_update)) { purge_pages_cache($pages_to_update); } } } } function purge_pages_cache($pages_to_update) { foreach ($pages_to_update as $page_id) { do_action('litespeed_purge_post', $page_id); log_purge_action($page_id); } }
关键修改说明
- 在调用
wp_update_post前后,通过litespeed_disable_all_hooks过滤器临时禁用LiteSpeed的自动缓存处理,避免因自定义文章类型状态变更触发全站缓存清除 - 保持原有的
purge_pages_cache函数逻辑,确保仅清除alert_pages字段指定的页面缓存
内容的提问来源于stack exchange,提问作者user17092013
相关产品推荐
相关产品推荐

