{"slug":"transients-object-cache","title":"WordPress Transients and Object Cache","tags":["transients","object-cache","redis","performance","caching"],"agent_summary":"WordPress transients API for temporary DB-cached data, object cache API for request-scoped memory, Redis persistent object cache setup, and WP-CLI transient cleanup commands.","trigger_phrases":["wordpress transients","set_transient","get_transient","wordpress object cache","redis wordpress","wp_cache_set","transient expiry","persistent object cache"],"runnable":true,"markdown":"\nTransients cache temporary data in the database (or object cache). Object cache stores data in memory for the current request.\n\n## Transients API\n\n```php\n// Store for 12 hours\nset_transient('my_api_response', $data, 12 * HOUR_IN_SECONDS);\n\n// Get — returns false on cache miss\n$data = get_transient('my_api_response');\nif ($data === false) {\n    $data = fetch_from_api();\n    set_transient('my_api_response', $data, 12 * HOUR_IN_SECONDS);\n}\n\n// Delete immediately\ndelete_transient('my_api_response');\n\n// Multisite-aware (stored in wp_sitemeta, not wp_options)\nset_site_transient('global_data', $data, DAY_IN_SECONDS);\n$data = get_site_transient('global_data');\n```\n\n### Time Constants\n\n| Constant | Seconds |\n|----------|---------|\n| `MINUTE_IN_SECONDS` | 60 |\n| `HOUR_IN_SECONDS` | 3600 |\n| `DAY_IN_SECONDS` | 86400 |\n| `WEEK_IN_SECONDS` | 604800 |\n| `MONTH_IN_SECONDS` | 2592000 |\n| `YEAR_IN_SECONDS` | 31536000 |\n\n## Object Cache API\n\n```php\n// Store in memory (current request only — no DB persistence without backend)\nwp_cache_set('my_key', $value, 'my_group', 300);\n\n// Retrieve\n$cached = wp_cache_get('my_key', 'my_group');\n\n// Delete\nwp_cache_delete('my_key', 'my_group');\n\n// Flush a group\nwp_cache_flush_group('my_group');\n```\n\n## Persistent Object Cache — Redis\n\nFor high-traffic sites, make object cache persist across requests:\n\n```bash\n# 1. Install Redis on server\nsudo apt install redis-server\nsudo systemctl enable redis-server\n\n# 2. Install Redis Object Cache plugin (by Till Krüss)\nwp plugin install redis-cache --activate\n\n# 3. Enable via WP-CLI\nwp redis enable\n\n# 4. Check status\nwp redis status\n```\n\nwp-config.php settings:\n\n```php\ndefine('WP_REDIS_HOST', '127.0.0.1');\ndefine('WP_REDIS_PORT', 6379);\ndefine('WP_REDIS_DATABASE', 0);\ndefine('WP_CACHE_KEY_SALT', 'mysite_unique_'); // unique per site on shared Redis\n```\n\n## WP-CLI Transient Cleanup\n\n```bash\n# Delete only expired transients\nwp transient delete --all --expired\n\n# Delete ALL transients (nuclear — flushes entire transient cache)\nwp transient delete --all\n\n# Check a specific transient\nwp transient get my_api_response\n```\n","html":"<p>Transients cache temporary data in the database (or object cache). Object cache stores data in memory for the current request.</p>\n<h2>Transients API</h2>\n<pre><code class=\"language-php\">// Store for 12 hours\nset_transient('my_api_response', $data, 12 * HOUR_IN_SECONDS);\n\n// Get — returns false on cache miss\n$data = get_transient('my_api_response');\nif ($data === false) {\n    $data = fetch_from_api();\n    set_transient('my_api_response', $data, 12 * HOUR_IN_SECONDS);\n}\n\n// Delete immediately\ndelete_transient('my_api_response');\n\n// Multisite-aware (stored in wp_sitemeta, not wp_options)\nset_site_transient('global_data', $data, DAY_IN_SECONDS);\n$data = get_site_transient('global_data');\n</code></pre>\n<h3>Time Constants</h3>\n<p>| Constant | Seconds |\n|----------|---------|\n| <code>MINUTE_IN_SECONDS</code> | 60 |\n| <code>HOUR_IN_SECONDS</code> | 3600 |\n| <code>DAY_IN_SECONDS</code> | 86400 |\n| <code>WEEK_IN_SECONDS</code> | 604800 |\n| <code>MONTH_IN_SECONDS</code> | 2592000 |\n| <code>YEAR_IN_SECONDS</code> | 31536000 |</p>\n<h2>Object Cache API</h2>\n<pre><code class=\"language-php\">// Store in memory (current request only — no DB persistence without backend)\nwp_cache_set('my_key', $value, 'my_group', 300);\n\n// Retrieve\n$cached = wp_cache_get('my_key', 'my_group');\n\n// Delete\nwp_cache_delete('my_key', 'my_group');\n\n// Flush a group\nwp_cache_flush_group('my_group');\n</code></pre>\n<h2>Persistent Object Cache — Redis</h2>\n<p>For high-traffic sites, make object cache persist across requests:</p>\n<pre><code class=\"language-bash\"># 1. Install Redis on server\nsudo apt install redis-server\nsudo systemctl enable redis-server\n\n# 2. Install Redis Object Cache plugin (by Till Krüss)\nwp plugin install redis-cache --activate\n\n# 3. Enable via WP-CLI\nwp redis enable\n\n# 4. Check status\nwp redis status\n</code></pre>\n<p>wp-config.php settings:</p>\n<pre><code class=\"language-php\">define('WP_REDIS_HOST', '127.0.0.1');\ndefine('WP_REDIS_PORT', 6379);\ndefine('WP_REDIS_DATABASE', 0);\ndefine('WP_CACHE_KEY_SALT', 'mysite_unique_'); // unique per site on shared Redis\n</code></pre>\n<h2>WP-CLI Transient Cleanup</h2>\n<pre><code class=\"language-bash\"># Delete only expired transients\nwp transient delete --all --expired\n\n# Delete ALL transients (nuclear — flushes entire transient cache)\nwp transient delete --all\n\n# Check a specific transient\nwp transient get my_api_response\n</code></pre>\n"}