Automattic / VIP-Coding-Standards

PHP_CodeSniffer ruleset to enforce WordPress VIP coding standards.
https://wpvip.com/documentation/how-to-install-php-code-sniffer-for-wordpress-com-vip/
Other
236 stars 40 forks source link

Enhance logic of LowCacheTime to minimize noise. #408

Open mdbitz opened 5 years ago

mdbitz commented 5 years ago

❗️ Warning: Low cache expiry time of "wp_rand( 5 * MINUTE_IN_SECONDS, 10 * MINUTE_IN_SECONDS )", it is recommended to have 300 seconds or more (WordPressVIPMinimum.Cache.LowExpiryCacheTime.LowCacheTime).

wp_cache_set( $latest_blocks_cache_key, $latest_blocks, null, wp_rand( 5 * MINUTE_IN_SECONDS, 10 * MINUTE_IN_SECONDS ) );

What problem would the enhancement address for VIP?

It is very common for clients to use constants HOUR_IN_SECONDS, DAY_IN_SECONDS, MINUTE_IN_SECONDS and to also vary the duration to not have cache stampedes.

Describe the solution you'd like

Can we enhance the logic of the ruleset to not flag for common usages?

GaryJones commented 5 years ago

We already handle time constants.

What we don't handle yet are calls to wp_rand() / rand() / random_int() / mt_rand(), - but we can check that the first arg meets the 300-second minimum (actually, it would have to be either arg for wp_rand(), as wp_rand() can accept args in either order).