Verified Commit aea3f4e7 authored by Erick Hitter's avatar Erick Hitter
Browse files

Introduce caching in the options rebuilding, rather than relying on what Core might provide

Since we often query directly, it was foolish of me to omit this both because Core's caching is often bypassed, and because it won't be reliable.
parent e6a3276e
......@@ -16,6 +16,8 @@ class Cron_Options_CPT extends Singleton {
const POST_STATUS_PENDING = 'inherit';
const POST_STATUS_COMPLETED = 'trash';
const CACHE_KEY = 'a8c_cron_ctrl_option';
private $posts_to_clean = array();
private $option_before_unscheduling = null;
......@@ -64,6 +66,14 @@ class Cron_Options_CPT extends Singleton {
* Override cron option requests with data from CPT
*/
public function get_option() {
// Use cached value for reads, except when we're unscheduling and state is important
$cached_option = wp_cache_get( self::CACHE_KEY, null, true );
if ( ! $this->is_unscheduling() && false !== $cached_option ) {
return $cached_option;
}
// Start building a new cron option
$cron_array = array(
'version' => 2, // Core versions the cron array; without this, events will continually requeue
);
......@@ -133,6 +143,9 @@ class Cron_Options_CPT extends Singleton {
$this->option_before_unscheduling = null;
}
// Cache the results, bearing in mind that they won't be used during unscheduling events
wp_cache_set( self::CACHE_KEY, $cron_array, null, 1 * \HOUR_IN_SECONDS );
return $cron_array;
}
......@@ -287,6 +300,9 @@ class Cron_Options_CPT extends Singleton {
}
}
// Delete internal cache
wp_cache_delete( self::CACHE_KEY );
// Allow more events to be created
Lock::free_lock( self::LOCK );
}
......@@ -329,6 +345,9 @@ class Cron_Options_CPT extends Singleton {
$this->posts_to_clean[] = $job_post_id;
}
// Delete internal cache
wp_cache_delete( self::CACHE_KEY );
return true;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment