Make WordPress Core

Changeset 37286


Ignore:
Timestamp:
04/21/2016 08:17:32 PM (9 years ago)
Author:
ocean90
Message:

Media: Don't cache the results of wp_mkdir_p() in a persistent cache.

To improve the performance of wp_upload_dir() the result of wp_mkdir_p() was stored in a persistent cache, introduced in [36565]. But this becomes an issue when WordPress is scaled horizontally. You may end up caching a value for a server where the directory doesn't exist which will prevent further uploads on other servers because of the persistent cache.
The fix is to use a non-persistent cache.

Merge of [37285] to the 4.5 branch.

Props azaozz, ocean90.
See #34359.
Fixes #36621.

Location:
branches/4.5
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.5

  • branches/4.5/src/wp-includes/functions.php

    r36925 r37286  
    18551855 */
    18561856function wp_upload_dir( $time = null, $create_dir = true, $refresh_cache = false ) {
    1857     static $cache = array();
     1857    static $cache = array(), $tested_paths = array();
    18581858
    18591859    $key = sprintf( '%d-%s', get_current_blog_id(), (string) $time );
     
    18751875    if ( $create_dir ) {
    18761876        $path = $uploads['path'];
    1877         $tested_paths = wp_cache_get( 'upload_dir_tested_paths' );
    1878 
    1879         if ( ! is_array( $tested_paths ) ) {
    1880             $tested_paths = array();
    1881         }
    1882 
    1883         if ( ! in_array( $path, $tested_paths, true ) ) {
     1877
     1878        if ( array_key_exists( $path, $tested_paths ) ) {
     1879            $uploads['error'] = $tested_paths[ $path ];
     1880        } else {
    18841881            if ( ! wp_mkdir_p( $path ) ) {
    18851882                if ( 0 === strpos( $uploads['basedir'], ABSPATH ) ) {
     
    18901887
    18911888                $uploads['error'] = sprintf( __( 'Unable to create directory %s. Is its parent directory writable by the server?' ), esc_html( $error_path ) );
    1892             } else {
    1893                 $tested_paths[] = $path;
    1894                 wp_cache_set( 'upload_dir_tested_paths', $tested_paths );
    18951889            }
     1890
     1891            $tested_paths[ $path ] = $uploads['error'];
    18961892        }
    18971893    }
Note: See TracChangeset for help on using the changeset viewer.