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

CommandDescription
wp cache addAdds a value to the object cache.
wp cache decrDecrements a value in the object cache.
wp cache deleteRemoves a value from the object cache.
wp cache flushFlushes the object cache.
wp cache flush-groupRemoves all cache items in a group, if the object cache implementation supports it.
wp cache getGets a value from the object cache.
wp cache incrIncrements a value in the object cache.
wp cache patchUpdate a nested value from the cache.
wp cache pluckGet a nested value from the cache.
wp cache replaceReplaces a value in the object cache, if the value already exists.
wp cache setSets a value to the object cache, regardless of whether it already exists.
wp cache supportsDetermines whether the object cache implementation supports a particular feature.
wp cache typeAttempts to determine which object cache is being used.

Examples

bash
# 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

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

Parameters

ParameterDescription
<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

bash
$ 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

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

Parameters

ParameterDescription
<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

bash
$ 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

bash
wp cache delete <key> [<group>]

Parameters

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

Examples

bash
$ 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

bash
wp cache flush

Examples

bash
$ 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

bash
wp cache flush-group <group>

Parameters

ParameterDescription
<group>Cache group key.

Examples

bash
$ 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

bash
wp cache get <key> [<group>]

Parameters

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

Examples

bash
$ 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

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

Parameters

ParameterDescription
<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

bash
$ wp cache incr my_key 2 my_group
50

wp cache patch

Update a nested value from the cache.

Synopsis

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

Parameters

ParameterDescription
<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

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

Parameters

ParameterDescription
<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

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

Parameters

ParameterDescription
<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

bash
$ 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

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

Parameters

ParameterDescription
<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

bash
$ 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

bash
wp cache supports <feature>

Parameters

ParameterDescription
<feature>Name of the feature to check for.

Examples

bash
# 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

bash
wp cache type

Examples

bash
$ wp cache type
Default