Anyone recommends an alternative to set_transient that persists the data for the whole duration of the expiration even when object cache is enabled?


  • List item
  • Set a long-duration transient set_transient('foo', 'bar', WEEK_IN_SECONDS);
  • Site is using object cache, so this is saved in memory
  • I don't know internals of Redis, but memory cache is cleared for some reason:
    • Redis rotates cache based on the eviction policy
    • Server reboots
    • O.S-level memory management frees up memory
    • etc?

Now that transient expired before 1 week.

In a scenario such as this, how can I create a value that is able to persist for a longer period?

1 Answer 1


I eventually used a combination of update_option and WP Cron:

// Create the option.
update_option('foo', 'bar', false);

* Schedule it to be deleted one week from now.
* @param int Expiration timestamp
* @param string Hook that will be invoked
* @param string Params to send to the hook callback
wp_schedule_single_event( time() + WEEK_IN_SECONDS, 'expire_option', [ 'foo' ] );

// Register the hook.
add_action( 'expire_option', 'expire_option_callback', 10, 1 );

// This will be called by WP Cron when we want to "expire" the option, one week after its creation.
function expire_option_callback( $option_name ) {
  delete_option( $option_name );

The only drawback is that the option won't be expired if WP Cron is not working in the site. If this is unnaceptable, you'll need a more robust solution mimicking how transients are saved and expired in the DB.

  • +1, transients by definition are not reliable storage. As for cron, you can trust it working otherwise some other things will not work as well. The other alternative is to store a time stamp as part of the option, like it is actually done for transients. Commented Feb 23, 2023 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.