Make WordPress Core

Opened 5 months ago

Closed 4 months ago

#61484 closed enhancement (fixed)

Prime notoptions within `delete_option()`, `delete_network_option()`.

Reported by: peterwilsoncc's profile peterwilsoncc Owned by: pbearne's profile pbearne
Milestone: 6.7 Priority: normal
Severity: normal Version:
Component: Options, Meta APIs Keywords: has-patch has-unit-tests
Focuses: performance Cc:

Description

When an option is deleted from the database, it becomes known that the option does not exist. However, the notoptions cache is not primed to include this information.

The result is that a subsequent call to get the option will query the database unnecessarily. Priming the notoptions cache in this instance will prevent these queries.

$ wp shell 
wp> update_option( 'delete_option_testing', 'yes' );
=> bool(true)
wp> get_num_queries();
=> int(42)
wp> get_option( 'delete_option_testing' ); 
=> string(3) "yes"
wp> get_num_queries();
=> int(42)
wp> delete_option( 'delete_option_testing' );
=> bool(true)
wp> get_num_queries();
=> int(45)
wp> get_option( 'delete_option_testing' );
=> bool(false)
wp> get_num_queries();
=> int(46)

Change History (14)

This ticket was mentioned in Slack in #core-performance by mukeshpanchal27. View the logs.


5 months ago

#2 @pbearne
5 months ago

  • Owner set to pbearne
  • Status changed from new to assigned

#3 @mukesh27
5 months ago

  • Keywords needs-patch added
  • Milestone changed from Awaiting Review to 6.7

This ticket was mentioned in PR #6964 on WordPress/wordpress-develop by @pbearne.


5 months ago
#4

  • Keywords has-patch has-unit-tests added; needs-patch removed

Update delete_option to modify notoptions cache

This commit introduces updates to the delete_option and delete_network_option functions so that they modify the notoptions cache. Corresponding unit tests have been added to ensure the behavior is as expected. This ensures the query count doesn't increase when calling get_option or get_network_option after an option has been deleted.

@peterwilsoncc commented on PR #6964:


5 months ago
#5

@pbearne Are you able to make the same changes to the network options code?

@pbearne commented on PR #6964:


5 months ago
#6

@pbearne Are you able to make the same changes to the network options code?

added to delete_network_option to we need it elsewhere?

@peterwilsoncc commented on PR #6964:


5 months ago
#7

I mean the changes I suggested in the original review. I made them against the options functions and tests but they're also needed for the network options.

@pbearne commented on PR #6964:


4 months ago
#8

@peterwilsoncc made the changes

#9 @peterwilsoncc
4 months ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 58782:

Options, Meta APIs: Prime notoptions cache when deleting options.

Prime the notoptions cache within delete_option and delete_network_option to avoid the need for a database query if get_option or get_network_option is subsequently called.

Adds some associated tests to ensure that an option is cleared from the notoptions cache when an option is added either via add_option, update_option or their network option equivalent.

Props pbearne, mukesh27.
Fixes #61484.

#12 @peterwilsoncc
4 months ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

Reopening this as @bjorsch picked an issue up while working on #61730.

In delete_option() the notoptions cache is set regardless of the result of the database query.

If wpdb::delete() returns false (database connection error) then it's not known if the option exists or not so the cache should probably not be set so there ought to be a check for that before setting the cache.

If wpdb::delete() returns 0 the cache can be set as it's known not to be an option as there were no rows to delete.

#13 @peterwilsoncc
4 months ago

In 58811:

Options, Meta APIs: Prevent Single Site installs using network notoptions cache.

Modifies the caching of notoptions in delete_network_option() to ensure that the network cache is bypassed on single site installs.

On single site installs the incorrect caching was causing the notoptions cache to remain populated once a deleted option was subsequently added or updated.

Follow up to [58782].

Props bjorsch, pbearne.
Fixes #61730.
See #61484.

#14 @peterwilsoncc
4 months ago

  • Resolution set to fixed
  • Status changed from reopened to closed

I noticed the caching issues following database errors appears in multiple functions so created #61762 to handle it.

Reclosing this.

Note: See TracTickets for help on using tickets.