Skip to content

WP_CLI_CACHE_EXPIRY=0 and WP_CLI_CACHE_MAX_SIZE=0 fall back to the defaults #6452

Open

Description

@ekamran

Bug summary

WP_CLI::get_cache() reads the two cache limits like this on main (source):

$ttl      = (int) Utils\get_env_or_config( 'WP_CLI_CACHE_EXPIRY' ) ? : 15552000;
$max_size = (int) Utils\get_env_or_config( 'WP_CLI_CACHE_MAX_SIZE' ) ? : 314572800;

0 is falsy, so WP_CLI_CACHE_EXPIRY=0 and WP_CLI_CACHE_MAX_SIZE=0 become the defaults of 180 days and 300 MB. The same happens when the values are set under env: in wp-cli.yml. WP-CLI 2.12.0 reads getenv( 'WP_CLI_CACHE_EXPIRY' ) ? : 15552000 (no cast), and the string '0' is just as falsy there.

FileCache itself treats 0 as no limit: clean() only expires files when $ttl > 0 (L237) and only trims the cache when $max_size > 0 (L255). A negative value already reaches FileCache and turns the limit off, so 0 is the one value that cannot.

Steps to reproduce

mkdir cache
export WP_CLI_CACHE_DIR="$PWD/cache"
touch -t 202501010000 cache/old.bin
printf '<?php WP_CLI::get_cache()->clean();\n' > clean.php
WP_CLI_CACHE_EXPIRY=0 wp --require=clean.php cli version
ls cache

old.bin is deleted, because it is older than the default 180 days. With WP_CLI_CACHE_EXPIRY=-1 it is kept.

Expected

0 reaches FileCache as 0, so the cache expiry or the size limit is turned off.

Environment

  • WP-CLI 2.12.0 (the steps above) and main at aafc2a8 (the same steps, run from a checkout), PHP 8.5, macOS

Possible solution

Fall back to the default only when the value cast to an integer is 0 and the value is not a number. A number that casts to 0, such as 0 or 0.5, then turns the limit off. Empty and non-numeric values keep the defaults, and values that cast to a non-zero number are read as before. I have a fix with tests and will open a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions