Making WordPress.org

Changeset 12846


Ignore:
Timestamp:
08/17/2023 05:16:41 AM (11 months ago)
Author:
dd32
Message:

Plugin Directory: API: Increase the cache duration for plugin details, but add API cache invalidation.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/admin/class-status-transitions.php

    r12594 r12846  
    99use WordPressdotorg\Plugin_Directory\Email\Plugin_Rejected as Plugin_Rejected_Email;
    1010use WordPressdotorg\Plugin_Directory\Admin\Metabox\Reviewer as Reviewer_Metabox;
     11use WordPressdotorg\Plugin_Directory\Jobs\API_Update_Updater;
     12use WordPressdotorg\Plugin_Directory\Standalone\Plugins_Info_API;
    1113
    1214/**
     
    164166            update_post_meta( $post->ID, "_{$new_status}", strtotime( $post->post_modified_gmt ) );
    165167        }
     168
     169        // Clear any relevant caches.
     170        $this->flush_caches( $post );
    166171    }
    167172
     
    377382        Reviewer_Metabox::set_reviewer( $post, false, false );
    378383    }
     384
     385    /**
     386     * Flush the caches for the plugin.
     387     */
     388    protected function flush_caches( $post ) {
     389        // Update the API endpoints with the new data
     390        API_Update_Updater::update_single_plugin( $post->post_name );
     391        Plugins_Info_API::flush_plugin_information_cache( $post->post_name );
     392    }
    379393}
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/cli/class-import.php

    r12715 r12846  
    99use WordPressdotorg\Plugin_Directory\Email\Release_Confirmation as Release_Confirmation_Email;
    1010use WordPressdotorg\Plugin_Directory\Readme\Parser;
     11use WordPressdotorg\Plugin_Directory\Standalone\Plugins_Info_API;
    1112use WordPressdotorg\Plugin_Directory\Template;
    1213use WordPressdotorg\Plugin_Directory\Tools;
     
    370371        // Ensure that the API gets the updated data
    371372        API_Update_Updater::update_single_plugin( $plugin->post_name );
     373        Plugins_Info_API::flush_plugin_information_cache( $plugin->post_name );
    372374
    373375        // Import Tide data
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/jobs/class-meta-sync.php

    r10230 r12846  
    33
    44use WordPressdotorg\Plugin_Directory\Plugin_Directory;
     5use WordPressdotorg\Plugin_Directory\Standalone\Plugins_Info_API;
    56
    67/**
     
    182183            // Update the API endpoints with the new data
    183184            API_Update_Updater::update_single_plugin( $row->post_name );
     185            Plugins_Info_API::flush_plugin_information_cache( $row->post_name );
    184186
    185187            if ( $i % 100 === 0 ) {
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/standalone/class-plugins-info-api.php

    r12421 r12846  
    22namespace WordPressdotorg\Plugin_Directory\Standalone;
    33
    4 // The API caches here expire every 6-7 hours, avoids cache races when multiple change at the same time.
    5 define( 'API_CACHE_EXPIRY', 6 * 60 * 60 + rand( 0, 60 * 60 ) );
    64class Plugins_Info_API {
    75
    8     const CACHE_GROUP  = 'plugin_api_info';
    9     const CACHE_EXPIRY = API_CACHE_EXPIRY;
     6    const CACHE_GROUP       = 'plugin_api_info';
     7    const CACHE_EXPIRY      = 21600; // 6 hour cache, wporg_object_cache will spread this out.
     8    const LONG_CACHE_EXPIRY = 86400; // 24 hour cache, wporg_object_cache will spread this out.
    109
    1110    protected $format  = 'json';
     
    106105        }
    107106
    108         if ( false === ( $response = wp_cache_get( $cache_key = $this->plugin_information_cache_key( $request ), self::CACHE_GROUP ) ) ) {
     107        // Short circuit for invalid slugs.
     108        if ( ! $request->slug || ! preg_match( '/^[a-z0-9-]+$/', $request->slug ) ) {
     109            $response = [
     110                'error' => 'Invalid plugin slug.'
     111            ];
     112        } elseif ( false === ( $response = wp_cache_get( $cache_key = $this->plugin_information_cache_key( $request ), self::CACHE_GROUP ) ) ) {
    109113            $response = $this->internal_rest_api_call( 'plugins/v1/plugin/' . $request->slug, array( 'locale' => $request->locale ) );
    110114
     
    113117                    'error' => 'Plugin not found.'
    114118                ];
    115                 wp_cache_set( $cache_key, $response, self::CACHE_GROUP, 15 * 60 ); // shorter TTL for missing/erroring plugins.
     119                wp_cache_set( $cache_key, $response, self::CACHE_GROUP, self::LONG_CACHE_EXPIRY * 2 ); // Not found, twice as long as normal.
    116120            } else {
    117121                $response = $response->data;
    118                 wp_cache_set( $cache_key, $response, self::CACHE_GROUP, self::CACHE_EXPIRY );
     122                wp_cache_set( $cache_key, $response, self::CACHE_GROUP, self::LONG_CACHE_EXPIRY );
    119123            }
    120124        }
     
    145149        return 'plugin_information:'
    146150            . ( strlen( $request->slug ) > 200 ? 'md5:' . md5( $request->slug ) : $request->slug )
    147             . ':' . ( $request->locale ?: 'en_US' );
     151            . ':' . strtolower( $request->locale ?: 'en_US' );
     152    }
     153
     154    /**
     155     * Flush the cache for the plugin_information cache.
     156     *
     157     * @param string $slug The slug of the plugin to flush the cache for.
     158     */
     159    public static function flush_plugin_information_cache( $slug ) {
     160        foreach ( get_available_languages() as $locale ) {
     161            wp_cache_delete(
     162                self::plugin_information_cache_key(
     163                    (object) compact( 'slug', 'locale' )
     164                ),
     165                self::CACHE_GROUP
     166            );
     167        }
    148168    }
    149169
Note: See TracChangeset for help on using the changeset viewer.