admin管理员组文章数量:1335840
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 badges3 Answers
Reset to default 7The 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.
本文标签: phphow to get the value of time left for a transient
版权声明:本文标题:php - how to get the value of time left for a transient 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742386843a2465204.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论