-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
base: trunk
Are you sure you want to change the base?
Changes from 3 commits
85e9170
7450d26
c9b36ea
dd8c83e
7d97433
bd6f08c
2377348
d707a79
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' ); | ||
|
||
if ( $cache ) { | ||
return $cache; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great catch, @mukeshpanchal27. |
||
|
||
if ( null === $properties ) { | ||
$properties = static::PROPERTIES_METADATA; | ||
} | ||
|
@@ -2000,6 +2008,8 @@ protected static function compute_style_properties( $styles, $settings = array() | |
} | ||
} | ||
|
||
wp_cache_set( $cache_key, $declarations, 'wp-styles' ); | ||
|
||
return $declarations; | ||
} | ||
|
||
|
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.