Commit 64a1da2f authored by Erick Hitter's avatar Erick Hitter Committed by GitHub
Browse files

Merge pull request #10 from Automattic/add/native-caching

Add native caching
parents c4642708 aea3f4e7
......@@ -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;
}
......@@ -201,26 +214,21 @@ class Cron_Options_CPT extends Singleton {
/**
* Retrieve list of jobs, respecting whether or not the CPT is registered
*
* `WP_Query` also can't be used before `init` due to capabilities checks
* Uses a direct query to avoid stale caches that result in duplicate events
*/
private function get_jobs( $args ) {
// If called before `init`, we need to query directly because post types aren't registered earlier
if ( did_action( 'init' ) ) {
return get_posts( $args );
} else {
global $wpdb;
$orderby = 'date' === $args['orderby'] ? 'post_date' : $args['orderby'];
global $wpdb;
if ( isset( $args['paged'] ) ) {
$paged = max( 0, $args['paged'] - 1 );
$offset = $paged * $args['posts_per_page'];
} else {
$offset = 0;
}
$orderby = 'date' === $args['orderby'] ? 'post_date' : $args['orderby'];
return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->posts} WHERE post_type = %s AND post_status = %s ORDER BY %s %s LIMIT %d,%d;", $args['post_type'], $args['post_status'], $orderby, $args['order'], $offset, $args['posts_per_page'] ), 'OBJECT' );
if ( isset( $args['paged'] ) ) {
$paged = max( 0, $args['paged'] - 1 );
$offset = $paged * $args['posts_per_page'];
} else {
$offset = 0;
}
return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->posts} WHERE post_type = %s AND post_status = %s ORDER BY %s %s LIMIT %d,%d;", $args['post_type'], $args['post_status'], $orderby, $args['order'], $offset, $args['posts_per_page'] ), 'OBJECT' );
}
/**
......@@ -292,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 );
}
......@@ -334,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