wp cache

Adds, removes, fetches, and flushes the WP Object Cache object.

By default, the WP Object Cache exists in PHP memory for the length of the request (and is emptied at the end). Use a persistent object cache drop-in to persist the object cache between requests.

Read the codex article for more detail.

Subcommands

Command Description
wp cache add Adds a value to the object cache.
wp cache decr Decrements a value in the object cache.
wp cache delete Removes a value from the object cache.
wp cache flush Flushes the object cache.
wp cache flush-group Removes all cache items in a group, if the object cache implementation supports it.
wp cache get Gets a value from the object cache.
wp cache incr Increments a value in the object cache.
wp cache patch Update a nested value from the cache.
wp cache pluck Get a nested value from the cache.
wp cache replace Replaces a value in the object cache, if the value already exists.
wp cache set Sets a value to the object cache, regardless of whether it already exists.
wp cache supports Determines whether the object cache implementation supports a particular feature.
wp cache type Attempts to determine which object cache is being used.

Examples

# Set cache.
$ wp cache set my_key my_value my_group 300
Success: Set object 'my_key' in group 'my_group'.

# Get cache.
$ wp cache get my_key my_group
my_value

wp cache add

Adds a value to the object cache. Errors if a value already exists for the key, which means the value can’t be added.

Synopsis

wp cache add <key> <value> [<group>] [<expiration>]

Parameters

Parameter Description
<key> Cache key.
<value> Value to add to the key.
[<group>] Method for grouping data within the cache which allows the same key to be used across groups. Default: default
[<expiration>] Define how long to keep the value, in seconds. 0 means as long as possible. Default: 0

Examples

$ wp cache add my_key my_group my_value 300
Success: Added object 'my_key' in group 'my_value'.

wp cache decr

Decrements a value in the object cache. Errors if the value can’t be decremented.

Synopsis

wp cache decr <key> [<offset>] [<group>]

Parameters

Parameter Description
<key> Cache key.
[<offset>] The amount by which to decrement the item’s value. Default: 1
[<group>] Method for grouping data within the cache which allows the same key to be used across groups. Default: default

Examples

$ wp cache decr my_key 2 my_group
48

wp cache delete

Removes a value from the object cache. Errors if the value can’t be deleted.

Synopsis

wp cache delete <key> [<group>]

Parameters

Parameter Description
<key> Cache key.
[<group>] Method for grouping data within the cache which allows the same key to be used across groups. Default: default

Examples

$ wp cache delete my_key my_group
Success: Object deleted.

wp cache flush

Flushes the object cache.

For WordPress multisite instances using a persistent object cache, flushing the object cache will typically flush the cache for all sites. Beware of the performance impact when flushing the object cache in production.

Synopsis

wp cache flush

Examples

$ wp cache flush
Success: The cache was flushed.

wp cache flush-group

Removes all cache items in a group, if the object cache implementation supports it.

Synopsis

wp cache flush-group <group>

Parameters

Parameter Description
<group> Cache group key.

Examples

$ wp cache flush-group my_group
Success: Cache group 'my_group' was flushed.

wp cache get

Gets a value from the object cache. Errors if the value doesn’t exist.

Synopsis

wp cache get <key> [<group>]

Parameters

Parameter Description
<key> Cache key.
[<group>] Method for grouping data within the cache which allows the same key to be used across groups. Default: default

Examples

$ wp cache get my_key my_group
my_value

wp cache incr

Increments a value in the object cache. Errors if the value can’t be incremented.

Synopsis

wp cache incr <key> [<offset>] [<group>]

Parameters

Parameter Description
<key> Cache key.
[<offset>] The amount by which to increment the item’s value. Default: 1
[<group>] Method for grouping data within the cache which allows the same key to be used across groups. Default: default

Examples

$ wp cache incr my_key 2 my_group
50

wp cache patch

Update a nested value from the cache.

Synopsis

wp cache patch <action> <key> <key-path>... [<value>] [--group=<group>] [--format=<format>]

Parameters

Parameter Description
<action> Patch action to perform. Options: insert, update, delete
<key> Cache key.
<key-path>... The name(s) of the keys within the value to locate the value to patch.
[<value>] The new value. If omitted, the value is read from STDIN.
[--group=<group>] Method for grouping data within the cache. Default: default
[--format=<format>] The serialization format for the value. Default: plaintext. Options: plaintext, json

wp cache pluck

Get a nested value from the cache.

Synopsis

wp cache pluck <key> <key-path>... [--group=<group>] [--format=<format>]

Parameters

Parameter Description
<key> Cache key.
<key-path>... The name(s) of the keys within the value to locate the value to pluck.
[--group=<group>] Method for grouping data within the cache. Default: default
[--format=<format>] The output format of the value. Default: plaintext. Options: plaintext, json, yaml

wp cache replace

Replaces a value in the object cache, if the value already exists. Errors if the value can’t be replaced.

Synopsis

wp cache replace <key> <value> [<group>] [<expiration>]

Parameters

Parameter Description
<key> Cache key.
<value> Value to replace.
[<group>] Method for grouping data within the cache. Default: default
[<expiration>] Define how long to keep the value, in seconds. 0 means as long as possible. Default: 0

Examples

$ wp cache replace my_key new_value my_group
Success: Replaced object 'my_key' in group 'my_group'.

wp cache set

Sets a value to the object cache, regardless of whether it already exists. Errors if the value can’t be set.

Synopsis

wp cache set <key> <value> [<group>] [<expiration>]

Parameters

Parameter Description
<key> Cache key.
<value> Value to set on the key.
[<group>] Method for grouping data within the cache. Default: default
[<expiration>] Define how long to keep the value, in seconds. 0 means as long as possible. Default: 0

Examples

$ wp cache set my_key my_value my_group 300
Success: Set object 'my_key' in group 'my_group'.

wp cache supports

Determines whether the object cache implementation supports a particular feature.

Synopsis

wp cache supports <feature>

Parameters

Parameter Description
<feature> Name of the feature to check for.

Examples

# Check whether add_multiple is supported.
$ wp cache supports add_multiple
$ echo $?
0

# Bash script for checking support:
if ! wp cache supports non_existing; then
    echo 'non_existing is not supported'
fi

wp cache type

Attempts to determine which object cache is being used.

Note that the guesses made by this function are based on the WP_Object_Cache classes that define the 3rd party object cache extension. Changes to those classes could render problems with this function’s ability to determine which object cache is being used.

Synopsis

wp cache type

Examples

$ wp cache type
Default