ActionScheduler_OptionLock::set()publicWC 1.0

Set a lock using options for a given amount of time (60 seconds by default).

Using an autoloaded option avoids running database queries or other resource intensive tasks on frequently triggered hooks, like 'init' or 'shutdown'.

For example, ActionScheduler_QueueRunner->maybe_dispatch_async_request() uses a lock to avoid calling ActionScheduler_QueueRunner->has_maximum_concurrent_batches() every time the 'shutdown', hook is triggered, because that method calls ActionScheduler_QueueRunner->store->get_claim_count() to find the current number of claims in the database.

Method of the class: ActionScheduler_OptionLock{}

No Hooks.

Return

null. Nothing (null).

Usage

$ActionScheduler_OptionLock = new ActionScheduler_OptionLock();
$ActionScheduler_OptionLock->set( $lock_type );
$lock_type(string) (required)
A string to identify different lock types.

ActionScheduler_OptionLock::set() code WC 8.7.0

public function set( $lock_type ) {
	global $wpdb;

	$lock_key            = $this->get_key( $lock_type );
	$existing_lock_value = $this->get_existing_lock( $lock_type );
	$new_lock_value      = $this->new_lock_value( $lock_type );

	// The lock may not exist yet, or may have been deleted.
	if ( empty( $existing_lock_value ) ) {
		return (bool) $wpdb->insert(
			$wpdb->options,
			array(
				'option_name'  => $lock_key,
				'option_value' => $new_lock_value,
				'autoload'     => 'no',
			)
		);
	}

	if ( $this->get_expiration_from( $existing_lock_value ) >= time() ) {
		return false;
	}

	// Otherwise, try to obtain the lock.
	return (bool) $wpdb->update(
		$wpdb->options,
		array( 'option_value' => $new_lock_value ),
		array(
			'option_name'  => $lock_key,
			'option_value' => $existing_lock_value,
		)
	);
}