WP_Object_Cache
- Since
- 2.0.0
- Source
wp-includes/class-wp-object-cache.php:24
Core class that implements an object cache.
Description
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.
Compatibility
- WordPress
- since 2.0.0
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0).
Properties · 6
$cachearrayprivate- Holds the cached objects.
$cache_hitsintpublic- The amount of times the cache data was already stored in the cache.
$cache_missesintpublic- Amount of times the cache did not have the request in cache.
$global_groupsstring[]protected- List of global cache groups.
$blog_prefixstringprivate- The blog prefix to prepend to keys in non-global groups.
$multisiteboolprivate- Holds the value of is_multisite().
Methods · 24
- __construct()Sets up object properties.
- __get()Makes private properties readable for backward compatibility.
- __set()Makes private properties settable for backward compatibility.
- __isset()Makes private properties checkable for backward compatibility.
- __unset()Makes private properties un-settable for backward compatibility.
- is_valid_key()Serves as a utility function to determine whether a key is valid.
- _exists()Serves as a utility function to determine whether a key exists in the cache.
- add()Adds data to the cache if it doesn't already exist.
- add_multiple()Adds multiple values to the cache in one call.
- replace()Replaces the contents in the cache, if contents already exist.
- set()Sets the data contents into the cache.
- set_multiple()Sets multiple values to the cache in one call.
- get()Retrieves the cache contents, if it exists.
- get_multiple()Retrieves multiple values from the cache in one call.
- delete()Removes the contents of the cache key in the group.
- delete_multiple()Deletes multiple values from the cache in one call.
- incr()Increments numeric cache item's value.
- decr()Decrements numeric cache item's value.
- flush()Clears the object cache of all data.
- flush_group()Removes all cache items in a group.
- add_global_groups()Sets the list of global cache groups.
- switch_to_blog()Switches the internal blog ID.
- reset()Resets cache keys.
- stats()Echoes the stats of the caching.
Source code
#[AllowDynamicProperties]class WP_Object_Cache { /** * Holds the cached objects. * * @since 2.0.0 * @var array */ private $cache = array(); /** * The amount of times the cache data was already stored in the cache. * * @since 2.5.0 * @var int */ public $cache_hits = 0; /** * Amount of times the cache did not have the request in cache. * * @since 2.0.0 * @var int */ public $cache_misses = 0; /** * List of global cache groups. * * @since 3.0.0 * @var string[] */ protected $global_groups = array(); /** * The blog prefix to prepend to keys in non-global groups. * * @since 3.5.0 * @var string */ private $blog_prefix; /** * Holds the value of is_multisite(). * * @since 3.5.0 * @var bool */ private $multisite; /** * Sets up object properties. * * @since 2.0.8 */ public function __construct() { $this->multisite = is_multisite(); $this->blog_prefix = $this->multisite ? get_current_blog_id() . ':' : ''; } /** * Makes private properties readable for backward compatibility. * * @since 4.0.0 * * @param string $name Property to get. * @return mixed Property. */ public function __get( $name ) { return $this->$name; } /** * Makes private properties settable for backward compatibility. * * @since 4.0.0 * * @param string $name Property to set. * @param mixed $value Property value.Changelog
Introduced in 2.0.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 tag, from
src/wp-includes/class-wp-object-cache.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.