W
WordPress SOPKnowledge Base
Search
← All topics

WordPress Transients and Object Cache

runnable

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.

transientsobject-cacheredisperformancecaching
Agent trigger phrases: wordpress transients · set_transient · get_transient · wordpress object cache · redis wordpress · wp_cache_set · transient expiry · persistent object cache

Transients cache temporary data in the database (or object cache). Object cache stores data in memory for the current request.

Transients API

// Store for 12 hours
set_transient('my_api_response', $data, 12 * HOUR_IN_SECONDS);

// Get — returns false on cache miss
$data = get_transient('my_api_response');
if ($data === false) {
    $data = fetch_from_api();
    set_transient('my_api_response', $data, 12 * HOUR_IN_SECONDS);
}

// Delete immediately
delete_transient('my_api_response');

// Multisite-aware (stored in wp_sitemeta, not wp_options)
set_site_transient('global_data', $data, DAY_IN_SECONDS);
$data = get_site_transient('global_data');

Time Constants

| Constant | Seconds | |----------|---------| | MINUTE_IN_SECONDS | 60 | | HOUR_IN_SECONDS | 3600 | | DAY_IN_SECONDS | 86400 | | WEEK_IN_SECONDS | 604800 | | MONTH_IN_SECONDS | 2592000 | | YEAR_IN_SECONDS | 31536000 |

Object Cache API

// Store in memory (current request only — no DB persistence without backend)
wp_cache_set('my_key', $value, 'my_group', 300);

// Retrieve
$cached = wp_cache_get('my_key', 'my_group');

// Delete
wp_cache_delete('my_key', 'my_group');

// Flush a group
wp_cache_flush_group('my_group');

Persistent Object Cache — Redis

For high-traffic sites, make object cache persist across requests:

# 1. Install Redis on server
sudo apt install redis-server
sudo systemctl enable redis-server

# 2. Install Redis Object Cache plugin (by Till Krüss)
wp plugin install redis-cache --activate

# 3. Enable via WP-CLI
wp redis enable

# 4. Check status
wp redis status

wp-config.php settings:

define('WP_REDIS_HOST', '127.0.0.1');
define('WP_REDIS_PORT', 6379);
define('WP_REDIS_DATABASE', 0);
define('WP_CACHE_KEY_SALT', 'mysite_unique_'); // unique per site on shared Redis

WP-CLI Transient Cleanup

# Delete only expired transients
wp transient delete --all --expired

# Delete ALL transients (nuclear — flushes entire transient cache)
wp transient delete --all

# Check a specific transient
wp transient get my_api_response