PHP Класс 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.
Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
$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.

Открытые методы

Метод Описание
__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.

Защищенные методы

Метод Описание
_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

Приватные методы

Метод Описание
is_redis_failback_flush_enabled ( ) : boolean Whether or not wakeup flush is enabled

Описание методов

__construct() публичный Метод

Sets up object properties; PHP 5 style constructor
public __construct ( ) : null | WP_Object_Cache
Результат null | WP_Object_Cache If cache is disabled, returns null.

__destruct() публичный Метод

Called upon object destruction, which should be when PHP ends.
public __destruct ( ) : boolean
Результат boolean True value. Won't be used by PHP

_call_redis() защищенный Метод

Wrapper method for calls to Redis, which fails gracefully when Redis is unavailable
protected _call_redis ( string $method ) : mixed
$method string
Результат mixed

_connect_redis() защищенный Метод

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

_exists() защищенный Метод

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

_get_internal() защищенный Метод

Get a value from the internal object cache
protected _get_internal ( string $key, string $group ) : mixed
$key string
$group string
Результат mixed

_isset_internal() защищенный Метод

Check whether there's a value in the internal object cache.
protected _isset_internal ( string $key, string $group ) : boolean
$key string
$group string
Результат boolean

_key() защищенный Метод

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.
Результат string A properly prefixed redis cache key.

_set_internal() защищенный Метод

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() защищенный Метод

Does this group use persistent storage?
protected _should_persist ( string $group ) : boolean
$group string Cache group.
Результат boolean true if the group is persistent, false if not.

_should_use_redis_hashes() защищенный Метод

Should this group use Redis hashes?
protected _should_use_redis_hashes ( string $group ) : boolean
$group string Cache group.
Результат boolean True if the group should use Redis hashes, false if not.

_unset_internal() защищенный Метод

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

add() публичный Метод

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
Результат boolean False if cache key and group already exist, true on success

add_global_groups() публичный Метод

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

add_non_persistent_groups() публичный Метод

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() публичный Метод

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() публичный Метод

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.
Результат false | integer False on failure, the item's new value on success.

delete() публичный Метод

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
Результат boolean False if the contents weren't deleted and true on success

delete_group() публичный Метод

Remove the contents of all cache keys in the group.
public delete_group ( string $group ) : boolean
$group string Where the cache contents are grouped.
Результат boolean True on success, false on failure.

flush() публичный Метод

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?
Результат boolean Always returns true

get() публичный Метод

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.
Результат boolean | mixed False on failure to retrieve contents or the cache contents on success

incr() публичный Метод

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.
Результат false | integer False on failure, the item's new value on success.

replace() публичный Метод

Replace the contents in the cache, if contents already exist
См. также: 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
Результат boolean False if not exists, true if contents were replaced

reset() публичный Метод

Reset keys
Устаревший: 3.5.0
public reset ( )

set() публичный Метод

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
Результат boolean Always returns true

stats() публичный Метод

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

switch_to_blog() публичный Метод

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() публичный Метод

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

Описание свойств

$blog_prefix публичное свойство

The blog prefix to prepend to keys in non-global groups.
public int $blog_prefix
Результат integer

$cache публичное свойство

Holds the cached objects
public array $cache
Результат array

$cache_hits публичное свойство

The amount of times the cache data was already stored in the cache.
public int $cache_hits
Результат integer

$cache_misses публичное свойство

Amount of times the cache did not have the request in cache
public int $cache_misses
Результат integer

$do_redis_failback_flush публичное свойство

Whether or not the object cache thinks Redis needs a flush
public bool $do_redis_failback_flush
Результат boolean

$global_groups публичное свойство

List of global groups
public array $global_groups
Результат array

$is_redis_connected публичное свойство

Whether or not Redis is connected
public bool $is_redis_connected
Результат boolean

$last_triggered_error публичное свойство

The last triggered error
public $last_triggered_error

$non_persistent_groups публичное свойство

List of non-persistent groups
public array $non_persistent_groups
Результат array

$redis_calls публичное свойство

The amount of times a request was made to Redis
public int $redis_calls
Результат integer

$redis_hash_groups публичное свойство

List of groups which use Redis hashes.
public array $redis_hash_groups
Результат array