[wp-trac] [WordPress Trac] #45471: Allow caching of parse_blocks results

WordPress Trac noreply at wordpress.org
Mon Dec 3 15:51:15 UTC 2018


#45471: Allow caching of parse_blocks results
------------------------------------+------------------------------
 Reporter:  joostdevalk             |       Owner:  (none)
     Type:  enhancement             |      Status:  new
 Priority:  normal                  |   Milestone:  Awaiting Review
Component:  Cache API               |     Version:
 Severity:  normal                  |  Resolution:
 Keywords:  has-patch dev-feedback  |     Focuses:  performance
------------------------------------+------------------------------

Comment (by dmsnell):

 This is a good idea, definitely valid for re-parses.

 The cache key however appears to be based purely on the content of the
 document though and not on the parser, which is filterable. I think that
 in almost all cases this will be fine as the parser won't likely change
 within a cache session, but perhaps we could rewrite the patch to account
 for that.

 For instance, we could probably just add the name from the parser class
 given in the filter to the cache key. On this point, why pass in the cache
 key and default to `false` at all? I'm probably missing something but what
 key would we use other than one we could autogenerate based on content and
 parser?

 {{{#!php
 function parse_blocks( $content ) {
         $parser_class = apply_filters( 'block_parser_class',
 'WP_Block_Parser' );
         $cache_key    = sprintf( 'block_%s_%s', md5( $content ),
 $parser_class );

         $cached = wp_cache_get( $cache_key, 'parsed_blocks' );
         if ( $cached ) {
                 return $cached;
         }

         $parser = new $parser_class();
         $parsed = $parser->parse( $content );

         wp_cache_add( $cache_key, $parsed, 'parsed_blocks' );

         return $parsed;
 }
 }}}

 Are we trying to only turn on caching when we think or know we will need
 it?

 Also, at this point is there a reason not to create a PR in the Github
 repo?

 Thanks! This is great having more help on this area.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/45471#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list