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 more about the object cache in the description object cache functions.

Commands 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 get Gets a value from the object cache.
wp cache incr Increments a value in the object 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 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

Source code of the commands


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.

Usage

wp cache add {key} {value} [{group}] [{expiration}]

You can specify global options and the following:

{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

# Add cache.
$ 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.

Usage

wp cache decr {key} [{offset}] [{group}]

You can specify global options and the following:

{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

# Decrease cache value.
$ 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.

Usage

wp cache delete {key} [{group}]

You can specify global options and the following:

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

Examples

# Delete cache.
$ 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.

Errors if the object cache can't be flushed.

Usage

wp cache flush 

Examples

# Flush cache.
$ wp cache flush
Success: The cache was flushed.

wp cache get

Gets a value from the object cache.

Errors if the value doesn't exist.

Usage

wp cache get {key} [{group}]

You can specify global options and the following:

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

Examples

# Get cache.
$ 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.

Usage

wp cache incr {key} [{offset}] [{group}]

You can specify global options and the following:

{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

# Increase cache value.
$ wp cache incr my_key 2 my_group
50

wp cache replace

Replaces a value in the object cache, if the value already exists.

Errors if the value can't be replaced.

Usage

wp cache replace {key} {value} [{group}] [{expiration}]

You can specify global options and the following:

{key}
Cache key.
{value}
Value to replace.
[{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

# Replace cache.
$ 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.

Usage

wp cache set {key} {value} [{group}] [{expiration}]

You can specify global options and the following:

{key}
Cache key.
{value}
Value to set on 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

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

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.

Usage

wp cache type 

Examples

# Check cache type.
$ wp cache type
Default