Making WordPress.org


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

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.