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

Store block template object in object cache. #5467

Draft
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/wp-includes/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -519,11 +519,19 @@ function _remove_theme_attribute_from_template_part_block( &$block ) {
* @return WP_Block_Template Template.
*/
function _build_block_template_result_from_file( $template_file, $template_type ) {
$default_template_types = get_default_block_template_types();
$template_content = file_get_contents( $template_file['path'] );
$theme = get_stylesheet();
$template = new WP_Block_Template();
$cache_key = md5( serialize( $template_file ) );
Copy link
Member Author

Choose a reason for hiding this comment

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

$template_file is array, with contains theme, slug and path. Making it a good cache key.

$cache = wp_cache_get( $cache_key, 'block_template' );
if ( is_object( $cache ) ) {
foreach ( get_object_vars( $cache ) as $key => $value ) {
$template->$key = $value;
}

$template = new WP_Block_Template();
return $template;
}

$theme = ! isset( $template_file['theme'] ) ? $template_file['theme'] : get_stylesheet();
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
$template->content = file_get_contents( $template_file['path'] );
Copy link
Member Author

Choose a reason for hiding this comment

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

@felixarntz The massive performance benefits you saw in your PR are from this line. At the moment, EVERY time a block template is loaded, it is read into memory. In the case of TT4, this means loading, post meta template for every post on the page. In my test site, around 10 times. This is wasteful.

$template->id = $theme . '//' . $template_file['slug'];
$template->theme = $theme;
$template->slug = $template_file['slug'];
Expand All @@ -535,6 +543,7 @@ function _build_block_template_result_from_file( $template_file, $template_type
$template->is_custom = true;
$template->modified = null;

$default_template_types = get_default_block_template_types();
if ( 'wp_template' === $template_type && isset( $default_template_types[ $template_file['slug'] ] ) ) {
$template->description = $default_template_types[ $template_file['slug'] ]['description'];
$template->title = $default_template_types[ $template_file['slug'] ]['title'];
Expand All @@ -556,9 +565,11 @@ function _build_block_template_result_from_file( $template_file, $template_type
$before_block_visitor = make_before_block_visitor( $hooked_blocks, $template );
$after_block_visitor = make_after_block_visitor( $hooked_blocks, $template );
}
$blocks = parse_blocks( $template_content );
$blocks = parse_blocks( $template->content );
$template->content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );

wp_cache_add( $cache_key, $template, 'block_template' );

return $template;
}

Expand Down
2 changes: 1 addition & 1 deletion src/wp-includes/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@ function wp_start_object_cache() {
)
);

wp_cache_add_non_persistent_groups( array( 'counts', 'plugins', 'theme_json' ) );
wp_cache_add_non_persistent_groups( array( 'counts', 'plugins', 'theme_json', 'block_template' ) );
Copy link
Member Author

Choose a reason for hiding this comment

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

@felixarntz This is the most important part of this PR. This caches can never be a be a transient cache. Files can be edited local or by uploading via FTP. This will trigger no cache invalidation.

}

$first_init = false;
Expand Down
4 changes: 2 additions & 2 deletions src/wp-includes/ms-blogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ function switch_to_blog( $new_blog_id, $deprecated = null ) {
);
}

wp_cache_add_non_persistent_groups( array( 'counts', 'plugins', 'theme_json' ) );
wp_cache_add_non_persistent_groups( array( 'counts', 'plugins', 'theme_json', 'block_template' ) );
}
}

Expand Down Expand Up @@ -670,7 +670,7 @@ function restore_current_blog() {
);
}

wp_cache_add_non_persistent_groups( array( 'counts', 'plugins', 'theme_json' ) );
wp_cache_add_non_persistent_groups( array( 'counts', 'plugins', 'theme_json', 'block_template' ) );
}
}

Expand Down