| @@ -35,8 +35,18 @@ | ||
| 35 | 35 | */ |
| 36 | 36 | protected $force = false; |
| 37 | 37 | |
| 38 | 38 | /** |
| 39 | + * The most recent decode of each cache key's payload. | |
| 40 | + * | |
| 41 | + * Each entry is array( 'raw' => string, 'decoded' => mixed ). The raw JSON is kept so a reuse | |
| 42 | + * can be checked against what is stored right now, rather than assumed to still be current. | |
| 43 | + * | |
| 44 | + * @var array<string,array> | |
| 45 | + */ | |
| 46 | + private static $decoded_cache = array(); | |
| 47 | + | |
| 48 | + /** | |
| 39 | 49 | * @since 3.06 |
| 40 | 50 | * |
| 41 | 51 | * @param string|null $license The license key. |
| 42 | 52 | * |
| @@ -243,9 +253,9 @@ | ||
| 243 | 253 | * |
| 244 | 254 | * @return string |
| 245 | 255 | */ |
| 246 | 256 | protected function transient_key() { |
| 247 | - return strtolower( self::class ) . '_request_lock'; | |
| 257 | + return strtolower( static::class ) . '_request_lock'; | |
| 248 | 258 | } |
| 249 | 259 | |
| 250 | 260 | /** |
| 251 | 261 | * @since 3.06 |
| @@ -338,9 +348,41 @@ | ||
| 338 | 348 | if ( $is_expired && ! $this->is_running() ) { |
| 339 | 349 | return false; |
| 340 | 350 | } |
| 341 | 351 | |
| 342 | - return json_decode( $cache['value'], true ); | |
| 352 | + return $this->decode_cached( $cache['value'] ); | |
| 353 | + } | |
| 354 | + | |
| 355 | + /** | |
| 356 | + * Decode a cached API payload, reusing the previous decode of an unchanged payload. | |
| 357 | + * | |
| 358 | + * The payload runs to tens of kilobytes of JSON, and decoding it costs far more than reading | |
| 359 | + * the option it came from. That matters because a single form builder page decodes it once per | |
| 360 | + * upsell it renders. The reuse is keyed on the raw JSON rather than on the cache key, so | |
| 361 | + * anything that changes what is stored -- a new license, a cleared cache, a fresh API response, | |
| 362 | + * a test replacing the option -- is picked up on the very next read instead of being masked. | |
| 363 | + * | |
| 364 | + * @since 6.35 | |
| 365 | + * | |
| 366 | + * @param string $value The cached JSON. | |
| 367 | + * | |
| 368 | + * @return mixed | |
| 369 | + */ | |
| 370 | + private function decode_cached( $value ) { | |
| 371 | + $previous = self::$decoded_cache[ $this->cache_key ] ?? false; | |
| 372 | + | |
| 373 | + if ( $previous && $previous['raw'] === $value ) { | |
| 374 | + return $previous['decoded']; | |
| 375 | + } | |
| 376 | + | |
| 377 | + $decoded = json_decode( $value, true ); | |
| 378 | + | |
| 379 | + self::$decoded_cache[ $this->cache_key ] = array( | |
| 380 | + 'raw' => $value, | |
| 381 | + 'decoded' => $decoded, | |
| 382 | + ); | |
| 383 | + | |
| 384 | + return $decoded; | |
| 343 | 385 | } |
| 344 | 386 | |
| 345 | 387 | /** |
| 346 | 388 | * Get the cache for the network if multisite. |