Changeset 12846 for sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/standalone/class-plugins-info-api.php
- Timestamp:
- 08/17/2023 05:16:41 AM (17 months ago)
- 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 2 2 namespace WordPressdotorg\Plugin_Directory\Standalone; 3 3 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 ) );6 4 class Plugins_Info_API { 7 5 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. 10 9 11 10 protected $format = 'json'; … … 106 105 } 107 106 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 ) ) ) { 109 113 $response = $this->internal_rest_api_call( 'plugins/v1/plugin/' . $request->slug, array( 'locale' => $request->locale ) ); 110 114 … … 113 117 'error' => 'Plugin not found.' 114 118 ]; 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. 116 120 } else { 117 121 $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 ); 119 123 } 120 124 } … … 145 149 return 'plugin_information:' 146 150 . ( 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 } 148 168 } 149 169
Note: See TracChangeset
for help on using the changeset viewer.