WordPress Object Cache

The WordPress Object Cache is used to save on trips to the database. The Object Cache stores all of the cache data to memory and makes the cache contents available by using a key, which is used to name and later retrieve the cache contents.

The Object Cache can be replaced by other caching mechanisms by placing files in the wp-content folder which is looked at in wp-settings. If that file exists, then this file will not be included.

package WordPress
subpackage Cache
since 2.0

 Methods

PHP4 constructor; Calls PHP 5 style constructor

WP_Object_Cache() : \WP_Object_Cache
since 2.0.0

Returns

\WP_Object_Cache

Sets up object properties; PHP 5 style constructor

__construct() : null | \WP_Object_Cache
since 2.0.8

Returns

null\WP_Object_CacheIf cache is disabled, returns null.

Will save the object cache before object is completely destroyed.

__destruct() : boolean

Called upon object destruction, which should be when PHP ends.

since 2.0.8

Returns

booleanTrue value. Won't be used by PHP

Adds data to the cache if it doesn't already exist.

add(integer|string $id, mixed $data, string $group = 'default', integer $expire = '') : boolean
uses Checks to see if the cache already has data.
uses Sets the data after the checking the cache contents existance.
since 2.0.0

Parameters

$id

integerstring

What to call the contents in the cache

$data

mixed

The contents to store in the cache

$group

string

Where to group the cache contents

$expire

integer

When to expire the cache contents

Returns

booleanFalse if cache ID and group already exists, true on success

add_global_groups()

add_global_groups($groups) 

Parameters

$groups

add_non_persistent_groups()

add_non_persistent_groups($groups) 

Parameters

$groups

Remove the contents of the cache ID in the group

delete(integer|string $id, string $group = 'default', boolean $force = false) : boolean

If the cache ID does not exist in the group and $force parameter is set to false, then nothing will happen. The $force parameter is set to false by default.

On success the group and the id will be added to the $non_existant_objects property in the class.

since 2.0.0

Parameters

$id

integerstring

What the contents in the cache are called

$group

string

Where the cache contents are grouped

$force

boolean

Optional. Whether to force the unsetting of the cache ID in the group

Returns

booleanFalse if the contents weren't deleted and true on success

Clears the object cache of all data

flush($group = null) : boolean
since 2.0.0

Parameters

$group

Returns

booleanAlways returns true

Retrieves the cache contents, if it exists

get(integer|string $id, string $group = 'default') : boolean | mixed

The contents will be first attempted to be retrieved by searching by the ID in the cache group. If the cache is hit (success) then the contents are returned.

On failure, the $non_existant_objects property is checked and if the cache group and ID exist in there the cache misses will not be incremented. If not in the nonexistant objects property, then the cache misses will be incremented and the cache group and ID will be added to the nonexistant objects.

since 2.0.0

Parameters

$id

integerstring

What the contents in the cache are called

$group

string

Where the cache contents are grouped

Returns

booleanmixedFalse on failure to retrieve contents or the cache contents on success

Replace the contents in the cache, if contents already exist

replace(integer|string $id, mixed $data, string $group = 'default', integer $expire = '') : boolean
since 2.0.0
see

Parameters

$id

integerstring

What to call the contents in the cache

$data

mixed

The contents to store in the cache

$group

string

Where to group the cache contents

$expire

integer

When to expire the cache contents

Returns

booleanFalse if not exists, true if contents were replaced

Sets the data contents into the cache

set(integer|string $id, mixed $data, string $group = 'default', integer $expire = '') : boolean

The cache contents is grouped by the $group parameter followed by the $id. This allows for duplicate ids in unique groups. Therefore, naming of the group should be used with care and should follow normal function naming guidelines outside of core WordPress usage.

The $expire parameter is not used, because the cache will automatically expire for each time a page is accessed and PHP finishes. The method is more for cache plugins which use files.

since 2.0.0

Parameters

$id

integerstring

What to call the contents in the cache

$data

mixed

The contents to store in the cache

$group

string

Where to group the cache contents

$expire

integer

Not Used

Returns

booleanAlways returns true

Echos the stats of the caching.

stats() 

Gives the cache hits, and cache misses. Also prints every cached group, key and the data.

since 2.0.0

 Properties

 

Holds the cached objects

$cache : array

Default

array()
access private
since 2.0.0
 

The amount of times the cache data was already stored in the cache.

$cache_hits : integer

Default

0
since 2.5.0
access private
 

Amount of times the cache did not have the request in cache

$cache_misses : integer

Default

0
access public
since 2.0.0
 

Cache objects that do not exist in the cache

$non_existant_objects : array

Default

array()
access private
since 2.0.0

package BuddyPress

 Methods

WP_Object_Cache()

WP_Object_Cache() 

__construct()

__construct() 

add()

add($id, $data, $group = 'default', $expire = 0

Parameters

$id

$data

$group

$expire

add_global_groups()

add_global_groups($groups) 

Parameters

$groups

add_key_to_group_keys_cache()

add_key_to_group_keys_cache($key, $group) 

Parameters

$key

$group

add_non_persistent_groups()

add_non_persistent_groups($groups) 

Parameters

$groups

close()

close() 

colorize_debug_line()

colorize_debug_line($line) 

Parameters

$line

decr()

decr($id, $n, $group) 

Parameters

$id

$n

$group

delete()

delete($id, $group = 'default'

Parameters

$id

$group

delete_all_keys_in_group_key_cache()

delete_all_keys_in_group_key_cache($group) 

Parameters

$group

failure_callback()

failure_callback($host, $port) 

Parameters

$host

$port

flush()

flush($group = null

Parameters

$group

get()

get($id, $group = 'default'

Parameters

$id

$group

get_mc()

get_mc($group) 

Parameters

$group

get_multi()

get_multi($groups) 

Parameters

$groups

incr()

incr($id, $n, $group) 

Parameters

$id

$n

$group

key()

key($key, $group) 

Parameters

$key

$group

remove_key_from_group_keys_cache()

remove_key_from_group_keys_cache($key, $group) 

Parameters

$key

$group

replace()

replace($id, $data, $group = 'default', $expire = 0

Parameters

$id

$data

$group

$expire

set()

set($id, $data, $group = 'default', $expire = 0

Parameters

$id

$data

$group

$expire

stats()

stats() 

 Properties

 

$cache

$cache 

Default

array()
 

$default_expiration

$default_expiration 

Default

0
 

$global_groups

$global_groups 

Default

array('_cache_keys')
 

$group_ops

$group_ops 

Default

array()
 

$mc

$mc 

Default

array()
 

$no_mc_groups

$no_mc_groups 

Default

array()
 

$stats

$stats 

Default

array()