{"id":5553,"date":"2026-02-12T20:44:46","date_gmt":"2026-02-13T01:44:46","guid":{"rendered":"https:\/\/chubes.net\/?documentation=wp_object_cache"},"modified":"2026-03-13T03:28:04","modified_gmt":"2026-03-13T07:28:04","slug":"wp_object_cache","status":"publish","type":"documentation","link":"https:\/\/chubes.net\/docs\/wordpress-core\/cache\/wp_object_cache\/","title":{"rendered":"WP_Object_Cache"},"content":{"rendered":"<p>Core class that implements an in-memory object cache.<\/p><p><strong>Since:<\/strong> 2.0.0<br \/>\n<strong>Source:<\/strong> <code>wp-includes\/class-wp-object-cache.php<\/code><\/p><h2 class=\"wp-block-heading\">Description<\/h2><p>The WordPress Object Cache stores data in memory to reduce database queries. It uses a key\/group system to namespace cache entries.<\/p><p>The default implementation stores data in a PHP array, meaning data persists only for the current request. For true persistence, a drop-in at <code>wp-content\/object-cache.php<\/code> can replace this class with Redis, Memcached, or other backends.<\/p><h2 class=\"wp-block-heading\">Class Synopsis<\/h2><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">#[AllowDynamicProperties]\nclass WP_Object_Cache {\n    \/\/ Properties\n    private array $cache = array();\n    public int $cache_hits = 0;\n    public int $cache_misses = 0;\n    protected array $global_groups = array();\n    private string $blog_prefix;\n    private bool $multisite;\n    \n    \/\/ Methods\n    public function __construct()\n    public function add( $key, $data, $group = &#039;default&#039;, $expire = 0 ): bool\n    public function add_multiple( array $data, $group = &#039;&#039;, $expire = 0 ): array\n    public function replace( $key, $data, $group = &#039;default&#039;, $expire = 0 ): bool\n    public function set( $key, $data, $group = &#039;default&#039;, $expire = 0 ): bool\n    public function set_multiple( array $data, $group = &#039;&#039;, $expire = 0 ): array\n    public function get( $key, $group = &#039;default&#039;, $force = false, &amp;$found = null ): mixed\n    public function get_multiple( $keys, $group = &#039;default&#039;, $force = false ): array\n    public function delete( $key, $group = &#039;default&#039;, $deprecated = false ): bool\n    public function delete_multiple( array $keys, $group = &#039;&#039; ): array\n    public function incr( $key, $offset = 1, $group = &#039;default&#039; ): int|false\n    public function decr( $key, $offset = 1, $group = &#039;default&#039; ): int|false\n    public function flush(): bool\n    public function flush_group( $group ): bool\n    public function add_global_groups( $groups ): void\n    public function switch_to_blog( $blog_id ): void\n    public function reset(): void  \/\/ Deprecated\n    public function stats(): void\n    protected function is_valid_key( $key ): bool\n    protected function _exists( $key, $group ): bool\n}<\/code><\/pre><\/div><hr class=\"wp-block-separator\"\/><h2 class=\"wp-block-heading\">Properties<\/h2><h3 class=\"wp-block-heading\">$cache<\/h3><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">private array $cache = array();<\/code><\/pre><\/div><p>Holds all cached objects. Structure: <code>$cache[ $group ][ $key ] = $value<\/code><\/p><p><strong>Since:<\/strong> 2.0.0<\/p><hr class=\"wp-block-separator\"\/><h3 class=\"wp-block-heading\">$cache_hits<\/h3><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">public int $cache_hits = 0;<\/code><\/pre><\/div><p>Number of times cache returned a stored value.<\/p><p><strong>Since:<\/strong> 2.5.0<\/p><hr class=\"wp-block-separator\"\/><h3 class=\"wp-block-heading\">$cache_misses<\/h3><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">public int $cache_misses = 0;<\/code><\/pre><\/div><p>Number of times a key was not found in cache.<\/p><p><strong>Since:<\/strong> 2.0.0<\/p><hr class=\"wp-block-separator\"\/><h3 class=\"wp-block-heading\">$global_groups<\/h3><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">protected array $global_groups = array();<\/code><\/pre><\/div><p>List of global cache groups (shared across multisite network).<\/p><p><strong>Since:<\/strong> 3.0.0<\/p><hr class=\"wp-block-separator\"\/><h3 class=\"wp-block-heading\">$blog_prefix<\/h3><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">private string $blog_prefix;<\/code><\/pre><\/div><p>Prefix prepended to keys in non-global groups. Format: <code>{blog_id}:<\/code> in multisite, empty string otherwise.<\/p><p><strong>Since:<\/strong> 3.5.0<\/p><hr class=\"wp-block-separator\"\/><h3 class=\"wp-block-heading\">$multisite<\/h3><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">private bool $multisite;<\/code><\/pre><\/div><p>Whether WordPress is running in multisite mode.<\/p><p><strong>Since:<\/strong> 3.5.0<\/p><hr class=\"wp-block-separator\"\/><h2 class=\"wp-block-heading\">Methods<\/h2><h3 class=\"wp-block-heading\">__construct()<\/h3><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">public function __construct()<\/code><\/pre><\/div><p>Sets up object properties, detecting multisite and setting blog prefix.<\/p><p><strong>Since:<\/strong> 2.0.8<\/p><hr class=\"wp-block-separator\"\/><h3 class=\"wp-block-heading\">add()<\/h3><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">public function add( \n    int|string $key, \n    mixed $data, \n    string $group = &#039;default&#039;, \n    int $expire = 0 \n): bool<\/code><\/pre><\/div><p>Adds data to the cache if it doesn&#8217;t already exist.<\/p><p><strong>Since:<\/strong> 2.0.0<\/p><p><strong>Parameters:<\/strong><\/p><figure class=\"wp-block-table\"><table><thead><tr><th>Name<\/th><th>Type<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>$key<\/code><\/td><td><code>int|string<\/code><\/td><td>Cache key<\/td><\/tr><tr><td><code>$data<\/code><\/td><td><code>mixed<\/code><\/td><td>Data to store<\/td><\/tr><tr><td><code>$group<\/code><\/td><td><code>string<\/code><\/td><td>Cache group. Default <code>'default'<\/code><\/td><\/tr><tr><td><code>$expire<\/code><\/td><td><code>int<\/code><\/td><td>Expiration (ignored in default implementation)<\/td><\/tr><\/tbody><\/table><\/figure><p><strong>Returns:<\/strong> <code>true<\/code> on success, <code>false<\/code> if key exists or cache addition is suspended.<\/p><p><strong>Behavior:<\/strong><\/p><ol class=\"wp-block-list\"><li>Checks <code>wp_suspend_cache_addition()<\/code> &#8211; returns <code>false<\/code> if suspended<\/li><li>Validates key with <code>is_valid_key()<\/code><\/li><li>Applies blog prefix for non-global groups in multisite<\/li><li>Checks if key exists with <code>_exists()<\/code><\/li><li>Calls <code>set()<\/code> if key doesn&#8217;t exist<\/li><\/ol><hr class=\"wp-block-separator\"\/><h3 class=\"wp-block-heading\">add_multiple()<\/h3><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">public function add_multiple( \n    array $data, \n    string $group = &#039;&#039;, \n    int $expire = 0 \n): bool[]<\/code><\/pre><\/div><p>Adds multiple values in one call.<\/p><p><strong>Since:<\/strong> 6.0.0<\/p><p><strong>Returns:<\/strong> Array of booleans keyed by cache key.<\/p><hr class=\"wp-block-separator\"\/><h3 class=\"wp-block-heading\">replace()<\/h3><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">public function replace( \n    int|string $key, \n    mixed $data, \n    string $group = &#039;default&#039;, \n    int $expire = 0 \n): bool<\/code><\/pre><\/div><p>Replaces cache contents only if key exists.<\/p><p><strong>Since:<\/strong> 2.0.0<\/p><p><strong>Returns:<\/strong> <code>true<\/code> if replaced, <code>false<\/code> if original doesn&#8217;t exist.<\/p><hr class=\"wp-block-separator\"\/><h3 class=\"wp-block-heading\">set()<\/h3><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">public function set( \n    int|string $key, \n    mixed $data, \n    string $group = &#039;default&#039;, \n    int $expire = 0 \n): bool<\/code><\/pre><\/div><p>Sets data in the cache. Always writes regardless of existence.<\/p><p><strong>Since:<\/strong> 2.0.0<br \/>\n<strong>Modified:<\/strong> 6.1.0 &#8211; Returns <code>false<\/code> for invalid keys<\/p><p><strong>Behavior:<\/strong><\/p><ol class=\"wp-block-list\"><li>Validates key<\/li><li>Defaults empty group to <code>'default'<\/code><\/li><li>Applies blog prefix for non-global groups<\/li><li>Clones objects to prevent reference issues<\/li><li>Stores in <code>$this-&gt;cache[ $group ][ $key ]<\/code><\/li><\/ol><p><strong>Note:<\/strong> The <code>$expire<\/code> parameter is not used in the default implementation since cache dies with request.<\/p><hr class=\"wp-block-separator\"\/><h3 class=\"wp-block-heading\">set_multiple()<\/h3><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">public function set_multiple( \n    array $data, \n    string $group = &#039;&#039;, \n    int $expire = 0 \n): bool[]<\/code><\/pre><\/div><p>Sets multiple values in one call.<\/p><p><strong>Since:<\/strong> 6.0.0<\/p><hr class=\"wp-block-separator\"\/><h3 class=\"wp-block-heading\">get()<\/h3><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">public function get( \n    int|string $key, \n    string $group = &#039;default&#039;, \n    bool $force = false, \n    bool &amp;$found = null \n): mixed|false<\/code><\/pre><\/div><p>Retrieves cache contents.<\/p><p><strong>Since:<\/strong> 2.0.0<\/p><p><strong>Parameters:<\/strong><\/p><figure class=\"wp-block-table\"><table><thead><tr><th>Name<\/th><th>Type<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>$key<\/code><\/td><td><code>int|string<\/code><\/td><td>Cache key<\/td><\/tr><tr><td><code>$group<\/code><\/td><td><code>string<\/code><\/td><td>Cache group. Default <code>'default'<\/code><\/td><\/tr><tr><td><code>$force<\/code><\/td><td><code>bool<\/code><\/td><td>Unused in default implementation<\/td><\/tr><tr><td><code>$found<\/code><\/td><td><code>bool<\/code><\/td><td>Set to <code>true<\/code> if found, <code>false<\/code> otherwise<\/td><\/tr><\/tbody><\/table><\/figure><p><strong>Returns:<\/strong> Cached value on success, <code>false<\/code> on failure.<\/p><p><strong>Behavior:<\/strong><\/p><ol class=\"wp-block-list\"><li>Validates key<\/li><li>Applies blog prefix for non-global groups<\/li><li>Checks existence with <code>_exists()<\/code><\/li><li>On hit: increments <code>cache_hits<\/code>, clones objects, returns value<\/li><li>On miss: increments <code>cache_misses<\/code>, returns <code>false<\/code><\/li><\/ol><hr class=\"wp-block-separator\"\/><h3 class=\"wp-block-heading\">get_multiple()<\/h3><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">public function get_multiple( \n    array $keys, \n    string $group = &#039;default&#039;, \n    bool $force = false \n): array<\/code><\/pre><\/div><p>Retrieves multiple values in one call.<\/p><p><strong>Since:<\/strong> 5.5.0<\/p><hr class=\"wp-block-separator\"\/><h3 class=\"wp-block-heading\">delete()<\/h3><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">public function delete( \n    int|string $key, \n    string $group = &#039;default&#039;, \n    bool $deprecated = false \n): bool<\/code><\/pre><\/div><p>Removes a cache entry.<\/p><p><strong>Since:<\/strong> 2.0.0<\/p><p><strong>Returns:<\/strong> <code>true<\/code> on success, <code>false<\/code> if key doesn&#8217;t exist.<\/p><hr class=\"wp-block-separator\"\/><h3 class=\"wp-block-heading\">delete_multiple()<\/h3><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">public function delete_multiple( \n    array $keys, \n    string $group = &#039;&#039; \n): bool[]<\/code><\/pre><\/div><p>Deletes multiple keys in one call.<\/p><p><strong>Since:<\/strong> 6.0.0<\/p><hr class=\"wp-block-separator\"\/><h3 class=\"wp-block-heading\">incr()<\/h3><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">public function incr( \n    int|string $key, \n    int $offset = 1, \n    string $group = &#039;default&#039; \n): int|false<\/code><\/pre><\/div><p>Increments a numeric cache value.<\/p><p><strong>Since:<\/strong> 3.3.0<\/p><p><strong>Behavior:<\/strong><\/p><ul class=\"wp-block-list\"><li>Non-numeric values are treated as <code>0<\/code> before incrementing<\/li><li>Result cannot go below <code>0<\/code><\/li><\/ul><p><strong>Returns:<\/strong> New value on success, <code>false<\/code> if key doesn&#8217;t exist.<\/p><hr class=\"wp-block-separator\"\/><h3 class=\"wp-block-heading\">decr()<\/h3><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">public function decr( \n    int|string $key, \n    int $offset = 1, \n    string $group = &#039;default&#039; \n): int|false<\/code><\/pre><\/div><p>Decrements a numeric cache value.<\/p><p><strong>Since:<\/strong> 3.3.0<\/p><p><strong>Behavior:<\/strong><\/p><ul class=\"wp-block-list\"><li>Non-numeric values are treated as <code>0<\/code><\/li><li>Result floors at <code>0<\/code> (never negative)<\/li><\/ul><hr class=\"wp-block-separator\"\/><h3 class=\"wp-block-heading\">flush()<\/h3><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">public function flush(): true<\/code><\/pre><\/div><p>Clears the entire cache.<\/p><p><strong>Since:<\/strong> 2.0.0<\/p><p><strong>Returns:<\/strong> Always <code>true<\/code>.<\/p><hr class=\"wp-block-separator\"\/><h3 class=\"wp-block-heading\">flush_group()<\/h3><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">public function flush_group( string $group ): true<\/code><\/pre><\/div><p>Removes all cache items in a group.<\/p><p><strong>Since:<\/strong> 6.1.0<\/p><p><strong>Returns:<\/strong> Always <code>true<\/code>.<\/p><hr class=\"wp-block-separator\"\/><h3 class=\"wp-block-heading\">add_global_groups()<\/h3><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">public function add_global_groups( string|string[] $groups ): void<\/code><\/pre><\/div><p>Registers groups as global (shared across multisite network).<\/p><p><strong>Since:<\/strong> 3.0.0<\/p><p>Keys in global groups are not prefixed with blog ID.<\/p><hr class=\"wp-block-separator\"\/><h3 class=\"wp-block-heading\">switch_to_blog()<\/h3><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">public function switch_to_blog( int $blog_id ): void<\/code><\/pre><\/div><p>Changes the blog ID used for key prefixing.<\/p><p><strong>Since:<\/strong> 3.5.0<\/p><hr class=\"wp-block-separator\"\/><h3 class=\"wp-block-heading\">reset()<\/h3><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">public function reset(): void<\/code><\/pre><\/div><p><strong>Deprecated:<\/strong> 3.5.0. Use <code>switch_to_blog()<\/code>.<\/p><p>Clears non-global caches when blog ID changes.<\/p><hr class=\"wp-block-separator\"\/><h3 class=\"wp-block-heading\">stats()<\/h3><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">public function stats(): void<\/code><\/pre><\/div><p>Outputs cache statistics (hits, misses, group sizes).<\/p><p><strong>Since:<\/strong> 2.0.0<\/p><p><strong>Example Output:<\/strong><\/p><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">html<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-html\"><code class=\"language-html\">&lt;p&gt;\n&lt;strong&gt;Cache Hits:&lt;\/strong&gt; 523&lt;br \/&gt;\n&lt;strong&gt;Cache Misses:&lt;\/strong&gt; 47&lt;br \/&gt;\n&lt;\/p&gt;\n&lt;ul&gt;\n&lt;li&gt;&lt;strong&gt;Group:&lt;\/strong&gt; posts - ( 125.43k )&lt;\/li&gt;\n&lt;li&gt;&lt;strong&gt;Group:&lt;\/strong&gt; options - ( 45.21k )&lt;\/li&gt;\n&lt;\/ul&gt;<\/code><\/pre><\/div><hr class=\"wp-block-separator\"\/><h3 class=\"wp-block-heading\">is_valid_key()<\/h3><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">protected function is_valid_key( int|string $key ): bool<\/code><\/pre><\/div><p>Validates a cache key.<\/p><p><strong>Since:<\/strong> 6.1.0<\/p><p><strong>Valid Keys:<\/strong><\/p><ul class=\"wp-block-list\"><li>Integers<\/li><li>Non-empty strings<\/li><\/ul><p><strong>Invalid Keys:<\/strong><\/p><ul class=\"wp-block-list\"><li>Empty strings<\/li><li>Non-string, non-integer types<\/li><\/ul><p>Triggers <code>_doing_it_wrong()<\/code> for invalid keys.<\/p><hr class=\"wp-block-separator\"\/><h3 class=\"wp-block-heading\">_exists()<\/h3><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">protected function _exists( int|string $key, string $group ): bool<\/code><\/pre><\/div><p>Checks if a key exists in the cache.<\/p><p><strong>Since:<\/strong> 3.4.0<\/p><p>Uses both <code>isset()<\/code> and <code>array_key_exists()<\/code> to handle <code>null<\/code> values.<\/p><hr class=\"wp-block-separator\"\/><h2 class=\"wp-block-heading\">Magic Methods<\/h2><p>The class uses <code>#[AllowDynamicProperties]<\/code> and implements magic methods for backward compatibility:<\/p><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">public function __get( $name )    \/\/ Read private properties\npublic function __set( $name, $value )  \/\/ Write private properties\npublic function __isset( $name )  \/\/ Check if property set\npublic function __unset( $name )  \/\/ Unset property<\/code><\/pre><\/div><p><strong>Since:<\/strong> 4.0.0<\/p><p>This allows external access to private properties like <code>$cache<\/code> for debugging:<\/p><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">global $wp_object_cache;\nvar_dump( $wp_object_cache-&gt;cache ); \/\/ Works via __get()<\/code><\/pre><\/div><hr class=\"wp-block-separator\"\/><h2 class=\"wp-block-heading\">Implementing a Drop-in<\/h2><p>To create a persistent cache, place <code>object-cache.php<\/code> in <code>wp-content\/<\/code>:<\/p><div class=\"code-block-wrapper\"><div class=\"code-block-header\"><span class=\"code-block-language\">php<\/span><button class=\"code-copy-btn\" aria-label=\"Copy code\"><svg><use href=\"https:\/\/chubes.net\/wp-content\/themes\/chubes\/assets\/icons\/chubes.svg#icon-copy\"><\/use><\/svg><\/button><\/div><pre data-chubes-enhanced class=\"wp-block-code language-php\"><code class=\"language-php\">&lt;?php\n\/**\n * Object Cache Drop-in\n *\/\n\nclass WP_Object_Cache {\n    private $local_cache = array();\n    private $redis;\n    \n    public function __construct() {\n        $this-&gt;redis = new Redis();\n        $this-&gt;redis-&gt;connect( &#039;127.0.0.1&#039;, 6379 );\n    }\n    \n    public function get( $key, $group = &#039;default&#039;, $force = false, &amp;$found = null ) {\n        $cache_key = $this-&gt;build_key( $key, $group );\n        \n        \/\/ Check local cache first\n        if ( ! $force &amp;&amp; isset( $this-&gt;local_cache[ $cache_key ] ) ) {\n            $found = true;\n            return $this-&gt;local_cache[ $cache_key ];\n        }\n        \n        \/\/ Check Redis\n        $value = $this-&gt;redis-&gt;get( $cache_key );\n        if ( false !== $value ) {\n            $found = true;\n            $this-&gt;local_cache[ $cache_key ] = unserialize( $value );\n            return $this-&gt;local_cache[ $cache_key ];\n        }\n        \n        $found = false;\n        return false;\n    }\n    \n    public function set( $key, $data, $group = &#039;default&#039;, $expire = 0 ) {\n        $cache_key = $this-&gt;build_key( $key, $group );\n        \n        $this-&gt;local_cache[ $cache_key ] = $data;\n        \n        if ( $expire &gt; 0 ) {\n            return $this-&gt;redis-&gt;setex( $cache_key, $expire, serialize( $data ) );\n        }\n        return $this-&gt;redis-&gt;set( $cache_key, serialize( $data ) );\n    }\n    \n    \/\/ ... implement remaining methods\n}\n\nfunction wp_cache_init() {\n    $GLOBALS[&#039;wp_object_cache&#039;] = new WP_Object_Cache();\n}\n\n\/\/ ... implement all wp_cache_* functions<\/code><\/pre><\/div><p>The drop-in must implement all the functions from <code>cache.php<\/code> as WordPress expects them to exist.<\/p>","protected":false},"excerpt":{"rendered":"<p>Core class that implements an in-memory object cache. Since: 2.0.0 Source: wp-includes\/class-wp-object-cache.php Description The WordPress Object Cache stores data in memory to reduce database queries. It uses a key\/group system&#8230;<\/p>\n","protected":false},"featured_media":0,"template":"","meta":{"footnotes":""},"tags":[],"project":[606],"project_type":[749],"class_list":["post-5553","documentation","type-documentation","status-publish","hentry","project-cache","project_type-wordpress-reference"],"project_info":{"id":589,"name":"WordPress Core","slug":"wordpress-core"},"project_type_info":{"id":749,"name":"WordPress Reference","slug":"wordpress-reference"},"_links":{"self":[{"href":"https:\/\/chubes.net\/wp-json\/wp\/v2\/documentation\/5553","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chubes.net\/wp-json\/wp\/v2\/documentation"}],"about":[{"href":"https:\/\/chubes.net\/wp-json\/wp\/v2\/types\/documentation"}],"version-history":[{"count":28,"href":"https:\/\/chubes.net\/wp-json\/wp\/v2\/documentation\/5553\/revisions"}],"predecessor-version":[{"id":10885,"href":"https:\/\/chubes.net\/wp-json\/wp\/v2\/documentation\/5553\/revisions\/10885"}],"wp:attachment":[{"href":"https:\/\/chubes.net\/wp-json\/wp\/v2\/media?parent=5553"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chubes.net\/wp-json\/wp\/v2\/tags?post=5553"},{"taxonomy":"project","embeddable":true,"href":"https:\/\/chubes.net\/wp-json\/wp\/v2\/project?post=5553"},{"taxonomy":"project_type","embeddable":true,"href":"https:\/\/chubes.net\/wp-json\/wp\/v2\/project_type?post=5553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}