Skip to content

Spike: Can we add caching to as_next_scheduled() and as_has_scheduled_action()? #1240

@prettyboymp

Description

@prettyboymp

Issue

There is a common pattern amongst WooCommerce core and extensions to guarantee that actions are scheduled by running code similar to:

if ( false === as_next_scheduled_action( 'my_custom_action' ) ) {
	as_schedule_single_action( time(), 'my_custom_action' );
}

With multiple extensions using ActionScheduler, this can quickly add 20 or more extra queries to every wp-admin page load. Not only that, the performance of the as_schedule_single_action() query can get pretty slow on stores with heavy action scheduler usage, e.g. those running subscription renewals.

Ideally all these extensions would only ever attempt to schedule these actions during updates or periodically to make sure they are working correctly, but that isn't happening in reality and expecting all the extensions that use ActionScheduler to change their pattern isn't likely going to happen.

Proposal

Add caching around the underlying queries within as_next_scheduled() and as_has_scheduled_action().

Because the query vars for those methods can vary quite a bit, we likely won't be able to cache every combination. However, we should be able to cache the result in the most common scenarios, e.g. when the $args are empty or the default values and when only a $hook or a $hook and $group parameter are used.

When an action is modified, we should be able to purge the cache specific to that action's $hook, and $hook/$group combination.

Compatibility Concerns

The simplest implementation of caching would be at the top level as_.. functions. However, ensuring cache is properly purged when a change occurs is more difficult at this layer because the underlying implementation may bypass cache clearing triggers. So the safest implementation will probably be to put the caching within the datastores themselves. This still holds a slight risk if a certain implementation runs actions in the background and bypasses the datastore, but the positive community impact should be evaluated in the decision.

Metadata

Metadata

Assignees

Labels

priority: highThe issue/PR is high priority—it affects lots of customers substantially, but not critically.type: enhancementThe issue is a request for an enhancement.

Projects

Status

Merged

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions