Making WordPress.org

Changeset 9616


Ignore:
Timestamp:
03/22/2020 12:05:56 PM (5 years ago)
Author:
ocean90
Message:

Translate: Remove cron schedules for cache updates, update data on the fly.

Prevents missing data before the next schedule is run.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-routes/inc
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-routes/inc/class-plugin.php

    r9355 r9616  
    99class Plugin {
    1010
     11    public const CACHE_GROUP = 'wporg-translate';
     12
    1113    /**
    1214     * @var Plugin The singleton instance.
     
    4446        add_filter( 'gp_locale_glossary_path_prefix', [ $this, 'set_locale_glossary_path_prefix' ] );
    4547
    46         add_filter( 'cron_schedules', [ $this, 'register_cron_schedules' ] );
    47         add_action( 'init', [ $this, 'register_cron_events' ] );
    4848        add_action( 'init', [ $this, 'respect_robots_txt' ], 9 );
    49         add_action( 'wporg_translate_update_existing_locales_cache', [ $this, 'update_existing_locales_cache' ] );
    50         add_action( 'wporg_translate_update_translation_status_cache', [ $this, 'update_translation_status_cache' ] );
    51         add_action( 'wporg_translate_update_contributors_count_cache', [ $this, 'update_contributors_count_cache' ] );
    5249    }
    5350
     
    180177
    181178    /**
    182      * Filters the non-default cron schedules.
    183      *
    184      * @param array $schedules An array of non-default cron schedules.
    185      * @return array  An array of non-default cron schedules.
    186      */
    187     public function register_cron_schedules( $schedules ) {
    188         $schedules['15_minutes'] = array(
    189             'interval' => 15 * MINUTE_IN_SECONDS,
    190             'display'  => 'Every 15 minutes',
    191         );
    192 
    193         return $schedules;
    194     }
    195 
    196     /**
    197      * Registers CLI commands if WP-CLI is loaded.
    198      */
    199     public function register_cron_events() {
    200         if ( ! wp_next_scheduled( 'wporg_translate_update_existing_locales_cache' ) ) {
    201             wp_schedule_event( time(), '15_minutes', 'wporg_translate_update_existing_locales_cache' );
    202         }
    203 
    204         if ( ! wp_next_scheduled( 'wporg_translate_update_translation_status_cache' ) ) {
    205             wp_schedule_event( time() + 3 * MINUTE_IN_SECONDS, '15_minutes', 'wporg_translate_update_translation_status_cache' );
    206         }
    207 
    208         if ( ! wp_next_scheduled( 'wporg_translate_update_contributors_count_cache' ) ) {
    209             wp_schedule_event( time() + 6 * MINUTE_IN_SECONDS, '15_minutes', 'wporg_translate_update_contributors_count_cache' );
    210         }
    211     }
    212 
    213     /**
    214179     * Calculates the translation status of the WordPress project per locale.
    215180     */
    216     public function update_translation_status_cache() {
     181    public static function get_translation_status() {
    217182        global $wpdb;
    218183
    219184        if ( ! isset( $wpdb->project_translation_status ) ) {
    220185            return;
     186        }
     187
     188        $cached = wp_cache_get( 'translation-status', self::CACHE_GROUP );
     189        if ( false !== $cached ) {
     190            return $cached;
    221191        }
    222192
     
    229199        ), OBJECT_K );
    230200
    231         if ( ! $translation_status ) {
    232             return;
    233         }
    234 
    235         wp_cache_set( 'translation-status', $translation_status, 'wporg-translate' );
     201        wp_cache_set( 'translation-status', $translation_status, 'wporg-translate', 15 * MINUTE_IN_SECONDS );
     202
     203        return $translation_status;
    236204    }
    237205
     
    239207     * Updates contributors count per locale.
    240208     */
    241     public function update_contributors_count_cache() {
     209    public static function get_contributors_count() {
    242210        global $wpdb;
    243211
     212        $cached = wp_cache_get( 'contributors-count', self::CACHE_GROUP );
     213        if ( false !== $cached ) {
     214            return $cached;
     215        }
     216
    244217        if ( ! isset( $wpdb->user_translations_count ) ) {
    245             return;
    246         }
    247 
    248         $locales   = $this->get_existing_locales();
     218            return [];
     219        }
     220
     221        $locales   = self::get_existing_locales();
    249222        $db_counts = $wpdb->get_results(
    250223            "SELECT `locale`, COUNT( DISTINCT user_id ) as `count` FROM {$wpdb->user_translations_count} WHERE `accepted` > 0 GROUP BY `locale`",
    251224            OBJECT_K
    252225        );
    253 
    254         if ( ! $db_counts || ! $locales ) {
    255             return;
    256         }
    257226
    258227        $counts = array();
     
    265234        }
    266235
    267         wp_cache_set( 'contributors-count', $counts, 'wporg-translate' );
     236        wp_cache_set( 'contributors-count', $counts, self::CACHE_GROUP, 15 * MINUTE_IN_SECONDS );
     237
     238        return $counts;
    268239    }
    269240
     
    276247     * @return array List of GlotPress locales.
    277248     */
    278     private function get_existing_locales() {
     249    public static function get_existing_locales() {
    279250        global $wpdb;
    280251
    281         return $wpdb->get_col(
     252        $cached = wp_cache_get( 'existing-locales', self::CACHE_GROUP );
     253        if ( false !== $cached ) {
     254            return $cached;
     255        }
     256
     257        $existing_locales = $wpdb->get_col(
    282258            $wpdb->prepare(
    283259                "SELECT locale FROM {$wpdb->gp_translation_sets} WHERE `project_id` = %d and slug = %s",
     
    286262            )
    287263        );
    288     }
    289 
    290     /**
    291      * Updates cache for existing locales.
    292      */
    293     public function update_existing_locales_cache() {
    294         $existing_locales = $this->get_existing_locales();
    295         if ( ! $existing_locales ) {
    296             return;
    297         }
    298 
    299         wp_cache_set( 'existing-locales', $existing_locales, 'wporg-translate' );
     264
     265        wp_cache_set( 'existing-locales', $existing_locales, self::CACHE_GROUP, HOUR_IN_SECONDS );
     266
     267        return $existing_locales;
    300268    }
    301269}
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-routes/inc/routes/class-index.php

    r3002 r9616  
    55use GP_Locales;
    66use GP_Route;
     7use WordPressdotorg\GlotPress\Routes\Plugin;
    78
    89/**
     
    1314class Index extends GP_Route {
    1415
    15     private $cache_group = 'wporg-translate';
    16 
    1716    /**
    1817     * Prints all exisiting locales as cards.
    19      *
    20      * Note: Cache gets refreshed via `WPorg_GP_CLI_Update_Caches`.
    2118     */
    2219    public function get_locales() {
    23         $existing_locales = wp_cache_get( 'existing-locales', $this->cache_group );
    24         if ( false === $existing_locales ) {
    25             $existing_locales = array();
    26         }
     20        $existing_locales = Plugin::get_existing_locales();
    2721
    2822        $locales = array();
     
    3327        unset( $existing_locales );
    3428
    35         $contributors_count = wp_cache_get( 'contributors-count', $this->cache_group );
    36         if ( false === $contributors_count ) {
    37             $contributors_count = array();
    38         }
    39 
    40         $translation_status = wp_cache_get( 'translation-status', $this->cache_group );
    41         if ( false === $translation_status ) {
    42             $translation_status = array();
    43         }
     29        $contributors_count = Plugin::get_contributors_count();
     30        $translation_status = Plugin::get_translation_status();
    4431
    4532        $this->tmpl( 'index-locales', get_defined_vars() );
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-routes/inc/routes/class-locale.php

    r9299 r9616  
    88use stdClass;
    99use WordPressdotorg\GlotPress\Rosetta_Roles\Plugin as Rosetta_Roles;
     10use WordPressdotorg\GlotPress\Routes\Plugin;
    1011
    1112/**
     
    129130        );
    130131
    131         $contributors_count = wp_cache_get( 'contributors-count', 'wporg-translate' );
    132         if ( false === $contributors_count ) {
    133             $contributors_count = array();
    134         }
     132        $contributors_count = Plugin::get_contributors_count();
    135133
    136134        $variants = $this->get_locale_variants( $locale_slug );
     
    183181        $project_icon = $this->get_project_icon( $project, $sub_project, 64 );
    184182
    185         $contributors_count = wp_cache_get( 'contributors-count', 'wporg-translate' );
    186         if ( false === $contributors_count ) {
    187             $contributors_count = array();
    188         }
     183        $contributors_count = Plugin::get_contributors_count();
    189184
    190185        $sub_project_statuses = array();
Note: See TracChangeset for help on using the changeset viewer.