Making WordPress.org

Changeset 10638


Ignore:
Timestamp:
02/03/2021 05:26:36 AM (4 years ago)
Author:
dd32
Message:

Support Forums: cache the result of count_users() to speed up the pageload time of wp-admin/users.php.

Although this page is rarely needed or used, when it is, it's slooow. This avoids the need for a 20-60s query on each users.php pageload.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-performance-optimizations.php

    r10604 r10638  
    6060        add_filter( 'bbp_edit_reply_pre_set_terms',    array( $this, 'limit_topic_reply_tag_creation' ) );
    6161
     62        // Add some caching on count_users().
     63        add_filter( 'pre_count_users', array( $this, 'cache_count_users' ), 10, 3 );
    6264    }
    6365
     
    556558    }
    557559
     560    /**
     561     * Cache the result of `count_users()` as the Support Forums site has a lot of users.
     562     *
     563     * This slows wp-admin/users.php down so much that it's hard to use when required.
     564     * As these numbers don't change often, it's cached for 6 hours, which avoids a 20-60s query on each users.php pageload.
     565     */
     566    public function cache_count_users( $result, $strategy, $site_id ) {
     567        static $running = false;
     568
     569        if ( $result || ! is_multisite() || $running ) {
     570            return $result;
     571        }
     572
     573        switch_to_blog( $site_id );
     574
     575        $result = get_site_transient( 'count_users' );
     576        if ( ! $result ) {
     577            $running = true;
     578            $result  = count_users( $strategy, $site_id );
     579            $running = false;
     580
     581            set_site_transient( 'count_users', $result, 6 * HOUR_IN_SECONDS );
     582        }
     583
     584        restore_current_blog();
     585
     586        return $result;
     587    }
    558588}
Note: See TracChangeset for help on using the changeset viewer.