PHP Class WP_Object_Cache, wp-redis

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.
Show file Open project: pantheon-systems/wp-redis Class Usage Examples

Public Properties

Property Type Description
$blog_prefix integer The blog prefix to prepend to keys in non-global groups.
$cache array Holds the cached objects
$cache_hits integer The amount of times the cache data was already stored in the cache.
$cache_misses integer Amount of times the cache did not have the request in cache
$do_redis_failback_flush boolean Whether or not the object cache thinks Redis needs a flush
$global_groups array List of global groups
$is_redis_connected boolean Whether or not Redis is connected
$last_triggered_error The last triggered error
$non_persistent_groups array List of non-persistent groups
$redis_calls integer The amount of times a request was made to Redis
$redis_hash_groups array List of groups which use Redis hashes.

Public Methods

Method Description
__construct ( ) : null | WP_Object_Cache Sets up object properties; PHP 5 style constructor
__destruct ( ) : boolean Will save the object cache before object is completely destroyed.
add ( integer | string $key, mixed $data, string $group = 'default', integer $expire ) : boolean Adds data to the cache if it doesn't already exist.
add_global_groups ( array $groups ) Sets the list of global groups.
add_non_persistent_groups ( array $groups ) Sets the list of non-persistent groups.
add_redis_hash_groups ( array $groups ) Sets the list of groups that use Redis hashes.
decr ( integer | string $key, integer $offset = 1, string $group = 'default' ) : false | integer Decrement numeric cache item's value
delete ( integer | string $key, string $group = 'default', boolean $force = false ) : boolean Remove the contents of the cache key in the group
delete_group ( string $group ) : boolean Remove the contents of all cache keys in the group.
flush ( boolean $redis = true ) : boolean Clears the object cache of all data.
get ( integer | string $key, string $group = 'default', string $force = false, boolean &$found = null ) : boolean | mixed Retrieves the cache contents, if it exists
incr ( integer | string $key, integer $offset = 1, string $group = 'default' ) : false | integer Increment numeric cache item's value
replace ( integer | string $key, mixed $data, string $group = 'default', integer $expire ) : boolean Replace the contents in the cache, if contents already exist
reset ( ) Reset keys
set ( integer | string $key, mixed $data, string $group = 'default', integer $expire ) : boolean Sets the data contents into the cache
stats ( ) Echoes the stats of the caching.
switch_to_blog ( integer $blog_id ) Switch the interal blog id.
wp_action_admin_notices_warn_missing_redis ( ) Admin UI to let the end user know something about the Redis connection isn't working.

Protected Methods

Method Description
_call_redis ( string $method ) : mixed Wrapper method for calls to Redis, which fails gracefully when Redis is unavailable
_connect_redis ( ) Wrapper method for connecting to Redis, which lets us retry the connection
_exists ( $key, $group ) Utility function to determine whether a key exists in the cache.
_get_internal ( string $key, string $group ) : mixed Get a value from the internal object cache
_isset_internal ( string $key, string $group ) : boolean Check whether there's a value in the internal object cache.
_key ( string $key = '', string $group = 'default' ) : string Utility function to generate the redis key for a given key and group.
_set_internal ( string $key, string $group, mixed $value ) Set a value to the internal object cache
_should_persist ( string $group ) : boolean Does this group use persistent storage?
_should_use_redis_hashes ( string $group ) : boolean Should this group use Redis hashes?
_unset_internal ( string $key, string $group ) Unset a value from the internal object cache

Private Methods

Method Description
is_redis_failback_flush_enabled ( ) : boolean Whether or not wakeup flush is enabled

Method Details

__construct() public method

Sets up object properties; PHP 5 style constructor
public __construct ( ) : null | WP_Object_Cache
return null | WP_Object_Cache If cache is disabled, returns null.

__destruct() public method

Called upon object destruction, which should be when PHP ends.
public __destruct ( ) : boolean
return boolean True value. Won't be used by PHP

_call_redis() protected method

Wrapper method for calls to Redis, which fails gracefully when Redis is unavailable
protected _call_redis ( string $method ) : mixed
$method string
return mixed

_connect_redis() protected method

Wrapper method for connecting to Redis, which lets us retry the connection
protected _connect_redis ( )

_exists() protected method

Utility function to determine whether a key exists in the cache.
protected _exists ( $key, $group )

_get_internal() protected method

Get a value from the internal object cache
protected _get_internal ( string $key, string $group ) : mixed
$key string
$group string
return mixed

_isset_internal() protected method

Check whether there's a value in the internal object cache.
protected _isset_internal ( string $key, string $group ) : boolean
$key string
$group string
return boolean

_key() protected method

Utility function to generate the redis key for a given key and group.
protected _key ( string $key = '', string $group = 'default' ) : string
$key string The cache key.
$group string The cache group.
return string A properly prefixed redis cache key.

_set_internal() protected method

Set a value to the internal object cache
protected _set_internal ( string $key, string $group, mixed $value )
$key string
$group string
$value mixed

_should_persist() protected method

Does this group use persistent storage?
protected _should_persist ( string $group ) : boolean
$group string Cache group.
return boolean true if the group is persistent, false if not.

_should_use_redis_hashes() protected method

Should this group use Redis hashes?
protected _should_use_redis_hashes ( string $group ) : boolean
$group string Cache group.
return boolean True if the group should use Redis hashes, false if not.

_unset_internal() protected method

Unset a value from the internal object cache
protected _unset_internal ( string $key, string $group )
$key string
$group string

add() public method

Adds data to the cache if it doesn't already exist.
public add ( integer | string $key, mixed $data, string $group = 'default', integer $expire ) : boolean
$key integer | string 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
return boolean False if cache key and group already exist, true on success

add_global_groups() public method

Sets the list of global groups.
public add_global_groups ( array $groups )
$groups array List of groups that are global.

add_non_persistent_groups() public method

Sets the list of non-persistent groups.
public add_non_persistent_groups ( array $groups )
$groups array List of groups that are non-persistent.

add_redis_hash_groups() public method

Sets the list of groups that use Redis hashes.
public add_redis_hash_groups ( array $groups )
$groups array List of groups that use Redis hashes.

decr() public method

Decrement numeric cache item's value
public decr ( integer | string $key, integer $offset = 1, string $group = 'default' ) : false | integer
$key integer | string The cache key to increment
$offset integer The amount by which to decrement the item's value. Default is 1.
$group string The group the key is in.
return false | integer False on failure, the item's new value on success.

delete() public method

If the cache key 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.
public delete ( integer | string $key, string $group = 'default', boolean $force = false ) : boolean
$key integer | string 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 key in the group
return boolean False if the contents weren't deleted and true on success

delete_group() public method

Remove the contents of all cache keys in the group.
public delete_group ( string $group ) : boolean
$group string Where the cache contents are grouped.
return boolean True on success, false on failure.

flush() public method

By default, this will flush the session cache as well as Redis, but we can leave the redis cache intact if we want. This is helpful when, for instance, you're running a batch process and want to clear the session store to reduce the memory footprint, but you don't want to have to re-fetch all the values from the database.
public flush ( boolean $redis = true ) : boolean
$redis boolean Should we flush redis as well as the session cache?
return boolean Always returns true

get() public method

The contents will be first attempted to be retrieved by searching by the key in the cache group. If the cache is hit (success) then the contents are returned. On failure, the number of cache misses will be incremented.
public get ( integer | string $key, string $group = 'default', string $force = false, boolean &$found = null ) : boolean | mixed
$key integer | string What the contents in the cache are called
$group string Where the cache contents are grouped
$force string Whether to force a refetch rather than relying on the local cache (default is false)
$found boolean Optional. Whether the key was found in the cache. Disambiguates a return of false, a storable value. Passed by reference. Default null.
return boolean | mixed False on failure to retrieve contents or the cache contents on success

incr() public method

Increment numeric cache item's value
public incr ( integer | string $key, integer $offset = 1, string $group = 'default' ) : false | integer
$key integer | string The cache key to increment
$offset integer The amount by which to increment the item's value. Default is 1.
$group string The group the key is in.
return false | integer False on failure, the item's new value on success.

replace() public method

Replace the contents in the cache, if contents already exist
See also: WP_Object_Cache::set()
public replace ( integer | string $key, mixed $data, string $group = 'default', integer $expire ) : boolean
$key integer | string 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
return boolean False if not exists, true if contents were replaced

reset() public method

Reset keys
Deprecation: 3.5.0
public reset ( )

set() public method

The cache contents is grouped by the $group parameter followed by the $key. 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.
public set ( integer | string $key, mixed $data, string $group = 'default', integer $expire ) : boolean
$key integer | string 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 TTL for the data, in seconds
return boolean Always returns true

stats() public method

Gives the cache hits, and cache misses. Also prints every cached group, key and the data.
public stats ( )

switch_to_blog() public method

This changes the blog id used to create keys in blog specific groups.
public switch_to_blog ( integer $blog_id )
$blog_id integer Blog ID

wp_action_admin_notices_warn_missing_redis() public method

Admin UI to let the end user know something about the Redis connection isn't working.

Property Details

$blog_prefix public property

The blog prefix to prepend to keys in non-global groups.
public int $blog_prefix
return integer

$cache public property

Holds the cached objects
public array $cache
return array

$cache_hits public property

The amount of times the cache data was already stored in the cache.
public int $cache_hits
return integer

$cache_misses public property

Amount of times the cache did not have the request in cache
public int $cache_misses
return integer

$do_redis_failback_flush public property

Whether or not the object cache thinks Redis needs a flush
public bool $do_redis_failback_flush
return boolean

$global_groups public property

List of global groups
public array $global_groups
return array

$is_redis_connected public property

Whether or not Redis is connected
public bool $is_redis_connected
return boolean

$last_triggered_error public property

The last triggered error
public $last_triggered_error

$non_persistent_groups public property

List of non-persistent groups
public array $non_persistent_groups
return array

$redis_calls public property

The amount of times a request was made to Redis
public int $redis_calls
return integer

$redis_hash_groups public property

List of groups which use Redis hashes.
public array $redis_hash_groups
return array