Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add object caching for WP_Theme_JSON::compute_style_properties #5567

Open
wants to merge 8 commits into
base: trunk
Choose a base branch
from
10 changes: 10 additions & 0 deletions src/wp-includes/class-wp-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -1925,6 +1925,14 @@ protected static function flatten_tree( $tree, $prefix = '', $token = '--' ) {
* @return array Returns the modified $declarations.
*/
protected static function compute_style_properties( $styles, $settings = array(), $properties = null, $theme_json = null, $selector = null, $use_root_padding = null ) {
$args = func_get_args();
$cache_key = 'compute_style_properties_' . md5( wp_json_encode( $args ) );
$cache = wp_cache_get( $cache_key, 'wp-styles' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably need to add a check for development mode before making use of caches, similar to how WP_Theme::get_block_patterns does here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: If we introduce a development mode check, we might end up in a loop of discussions similar to what we had in #59719. Would it be advisable to prioritize and address that ticket first to determine the best way forward, making it easier to adopt the solution in other places? What are your thoughts on this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question—thanks for asking, @mukeshpanchal27. I don't think we need that discussion to block forward progress on this ticket since it's not proposing that we make these caches persistent via transients. In this case, adding a development mode check just ensures that anyone who is developing while using an object cache would still get fresh results.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mukeshpanchal27 @joemcgill, thanks for these inputs.

I just pushed an update to bail processing empty $styles and ensure we don't leverage cache in development mode.

I'm also adding an expiry time for the cache keys — a day is a reasonable value. This will help the system to clean up old data that it's not used often, in case there's a need for resources.


if ( $cache ) {
return $cache;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certainly, you can add the code after line 1944 to avoid creating a cache for empty( $styles ). Please insert the code in the appropriate location to achieve this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch, @mukeshpanchal27.


if ( null === $properties ) {
$properties = static::PROPERTIES_METADATA;
}
Expand Down Expand Up @@ -2000,6 +2008,8 @@ protected static function compute_style_properties( $styles, $settings = array()
}
}

wp_cache_set( $cache_key, $declarations, 'wp-styles' );

return $declarations;
}

Expand Down