wp-cli/cache-command
WP-CLI Cache Command manages WordPress object and transient caches from the command line. Add, get, set, delete, increment/decrement, and flush cached values across groups with optional expiration—ideal for debugging and cache maintenance.
Installation:
composer require wp-cli/cache-command
Ensure WP-CLI is installed globally or locally in your project.
Verify Installation:
wp cache type
This confirms the package is functional and detects the active cache backend (e.g., Default, Redis, Memcached).
First Use Case: Cache a simple value and retrieve it:
wp cache set user_prefs '{"theme":"dark"}' user 3600
wp cache get user_prefs user
Output:
{"theme":"dark"}
CRUD Operations:
wp cache set or wp cache replace for conditional updates.
wp cache set api_rate_limit 100 api 86400 # 24-hour expiry
wp cache add to avoid overwriting existing keys.
wp cache add user_session "abc123" sessions 300
wp cache delete user_session sessions
wp cache flush-group sessions # Clears all keys in the 'sessions' group
Transient Management:
--network for multisite transients.
wp transient set network_theme "light" 86400 --network
wp transient list --human-readable --format=json
wp transient delete --expired --network
Nested Data:
wp cache patch update user_prefs theme.dark_mode true --format=json
wp cache pluck user_prefs theme.dark_mode --format=json
Automation:
wp cache flush && wp transient delete --all --network
booted or terminating events to cache/fetch data dynamically.Laravel-Specific:
// app/Providers/WPCacheServiceProvider.php
public function register()
{
$this->app->singleton('wp-cache', function () {
return new WPCLICacheCommand();
});
}
// app/Console/Commands/FlushWPCache.php
public function handle()
{
Artisan::call('wp cache flush');
}
Cache Backend Agnosticism:
wp cache supports to check feature availability (e.g., add_multiple):
if wp cache supports add_multiple; then
wp cache set --multiple user_* "value_*" 3600
fi
Environment-Specific Config:
.env:
WP_CACHE_GROUP=laravel_sessions
WP_CACHE_EXPIRY=3600
wp cache set ${WP_CACHE_GROUP}_${USER_ID} "$USER_DATA" ${WP_CACHE_EXPIRY}
Persistent Cache Dependencies:
wp cache flush may fail if no persistent cache (e.g., Redis) is configured.wp cache type and install a drop-in (e.g., wp package install wp-cli/redis-command).Transient Expiry Quirks:
--expired flag may miss transients if the database is not synced with the cache backend.wp transient delete --all followed by --expired for thorough cleanup.Multisite Scope:
--network flag affects all sites in a multisite. Test in staging first.wp site list to target specific sites:
wp site list --field=url | xargs -I {} wp --url={} transient delete --all
Nested Data Serialization:
wp cache patch/pluck assumes JSON/YAML. Plaintext values may break.--format=json for structured data.Silent Failures:
$?) after commands. Non-zero indicates failure.wp cache get non_existent_key || echo "Key missing"
Cache Backend Logs:
wp config set REDIS_LOGGING true
Command Output Parsing:
--format=json for programmatic parsing:
wp transient list --format=json | jq '.[] | select(.name | test("user_"))'
Custom Commands:
wp-cli/cache-command:
// phpcs:ignore
class MyCacheCommand extends \WP_CLI_Command {
public function __invoke($args, $assoc_args) {
\WP_CLI::runcommand('cache set ' . $args['key'] . ' ' . $args['value']);
}
}
Event Listeners:
// app/Providers/EventServiceProvider.php
protected $listen = [
'Illuminate\Auth\Events\Attempting' => [
'App\Listeners\CacheUserSession',
],
];
Testing:
$this->expectOutputRegex('/Success: Set object/');
\WP_CLI::runcommand('cache set test_key test_value');
Default Group:
<group> to use the default group (default). Explicitly specify for clarity:
wp cache set key value # Uses 'default' group
wp cache set key value my_group # Explicit group
Expiration Handling:
0 = no expiry. Use false or omit for default (WordPress-specific).wp transient set session_data '{"user_id":1}' 0 # Persists indefinitely
Multibyte Characters:
wp cache set "user name" "John Doe" users
How can I help you explore Laravel packages today?