Why does WordPress create two transients with the same name when I specify timeout value?

Working with transients with a timeout, I seem to be getting two transients created, and I don’t quite understand why. Looking at the source code on: https://developer.wordpress.org/reference/functions/set_transient/, it would appear this only occurs when wp_using_ext_object_cache() returns false.

My calls are:

set_transient( 'mytransientprefix_key', 'value', 3600);

and then I update the same transient with:

set_transient( 'mytransientprefix_key', 'new_value', 3600);

In the wp_options table, I’m then left with:

One _transient_timeout_mytransientprefix_key (“value”) and one _transient_mytransientprefix_key (“new_value”).

What?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Why WordPress creates two transients: Because the first transient with timeout in the name, is used for storing the expiration you set for your transient (but the stored value is a UNIX timestamp, not simply the value you passed like 3600) so that WordPress knows when to delete your transient. As for the second one, it stores the actual transient value that you passed to set_transient(), i.e. the second parameter.

$key = 'mytransientprefix_key';

set_transient( $key, 'value', 3600 );
// That will create two options:
// _transient_timeout_mytransientprefix_key and
// _transient_mytransientprefix_key

var_dump(
    get_option( '_transient_timeout_' . $key ),
    get_option( '_transient_' . $key )
);
// Sample output: int(1611143902) string(5) "value"

So the value of the “timeout” transient shouldn’t be value — try the above code and see how it goes?


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x