php - how to get the value of time left for a transient

I am setting up a transient with one hour expiry time.Now i want to know how much time left for the transient to expire

I am setting up a transient with one hour expiry time. Now i want to know how much time left for the transient to expire.

I am getting the transient timeout value with get_option function.

Can anyone help me out.

Thank You.

I am setting up a transient with one hour expiry time. Now i want to know how much time left for the transient to expire.

I am getting the transient timeout value with get_option function.

Can anyone help me out.

Thank You.

Share Improve this question asked Nov 21, 2016 at 7:00 chaitanyachaitanya 1111 silver badge2 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 7

The problem here is that if the site is actually using something like Redis, or any other supported "external cache" then nothing is going to actually be set in the options table (which can be checked with wp_using_ext_object_cache)

Storage in the options table is just how WordPress handles transients outside of any external caching (or local object caching).

With that said, to calculate the time remaining, you would do something like this:

$expires = (int) get_option( '_transient_timeout_MY_TRANSIENT_NAME', 0 );
$time_left = $expires - time();

The value for expiration is calculated by adding time() to the value passed when setting the transient, so to get time left, just subtract time() from the value set for expiration

There is no built-in WordPress function to get the transient timeout. But you can use the following function to get the transient timeout.

function get_transient_timeout( $transient ) {
    global $wpdb;
    $transient_timeout = $wpdb->get_col( "
      SELECT option_value
      FROM $wpdb->options
      WHERE option_name
      LIKE '%_transient_timeout_$transient%'
    " );
    return $transient_timeout[0];
}

Transients by definition can expire at any moment, no matter what interval you have requested, therefor the "time till expiry" can not be reliably determined. you can hack something by inspecting the "raw" option, but it is a bad idea to relay on it.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742386844a4434230.html

相关推荐

  • php - how to get the value of time left for a transient

    I am setting up a transient with one hour expiry time.Now i want to know how much time left for the transient to expire

    17小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信