[wp-trac] [WordPress Trac] #58919: Add a hook to set_cached_mo_files() to allow flexible caching strategies for globbing *.mo files

WordPress Trac noreply at wordpress.org
Wed Jul 26 22:11:42 UTC 2023


#58919: Add a hook to set_cached_mo_files() to allow flexible caching strategies
for globbing *.mo files
-------------------------+-----------------------------
 Reporter:  mreishus     |      Owner:  (none)
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  I18N         |    Version:
 Severity:  normal       |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 WordPress uses MO files to manage translations in plugins, themes, and
 core itself. One of the methods in the core class
 `WP_Textdomain_Registry`, `set_cached_mo_files()`, uses the `glob()`
 function to retrieve all MO files in a specific directory. On sites with a
 large number of number of language files, the `glob()` operation can
 become expensive if run on a directory with thousands or tens of thousands
 of files.

 Currently, the `set_cached_mo_files()` method doesn't allow for a
 persistent caching strategy. The solution proposed here adds a hook in
 this method that would allow hosting providers to come up with their own
 caching strategy for the `glob( $path . '/*.mo' )` call. This would allow
 for improving overall performance in scenarios where there are a large
 number of language files.

 This change is about providing flexibility for different setups and does
 not suggest a specific caching implementation. We've used
 wp_cache_set/wp_cache_get, but it doesn't yet feel viable for core because
 of its lack of invalidation mechanism.  Other hosting providers could
 leverage different strategies that best suit their environments.

 Here's a potential approach:

 {{{#!php
         private function set_cached_mo_files( $path ) {
                 $this->cached_mo_files[ $path ] = array();

                 /**
                  * Filters the .mo files retrieved from a specified path.
                  *
                  * This filter allows you to change the way .mo files are
 fetched from a given path.
                  * This can be useful in situations where the directory
 contains a large number of files
                  * and the default glob() function becomes expensive in
 terms of performance.
                  *
                  * @since TBD
                  *
                  * @param null|array $mo_files Null by default, allowing
 the usual glob() function to run. If a non-null value
                  *                             is returned by the filter,
 this value is used instead of running glob().
                  *                             Should be an array of .mo
 files if not null.
                  * @param string $path The path from which .mo files are
 being fetched.
                 **/
                 $mo_files = apply_filters( 'get_mo_files_from_path', null,
 $path );
                 if ( null === $mo_files ) {
                         $mo_files = glob( $path . '/*.mo' );
                 }

                 if ( $mo_files ) {
                         $this->cached_mo_files[ $path ] = $mo_files;
                 }
         }

 }}}

 With this implementation, a filter `get_mo_files_from_path` is added,
 allowing a return of null to fall back to the current behavior, or an
 array of MO files from an alternative cache or storage system.

 In our testing, with a directory containing around 12,000 files, we've
 found this modification to provide a noticeable improvement in response
 times. The glob takes 5-10ms, even in a production environment. Fetching
 the same data from from APCu can take ~0.25ms. While the exact performance
 benefit may vary based on the specifics of the caching mechanism and the
 number of files involved, the added flexibility allows WordPress to scale
 better in larger setups.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/58919>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list