Opened 8 years ago
Last modified 6 months ago
#37040 assigned enhancement
Enhancement: new function to validate a transient exists, and isn't expired without extra query
Reported by: | danieliser | Owned by: | pbearne |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 4.6 |
Component: | Options, Meta APIs | Keywords: | has-patch needs-testing has-unit-tests |
Focuses: | performance | Cc: |
Description
Currently calling get_transient makes 2 queries, the first to validate if the transient exists & is not expired, the second to get the transient value.
I propose creating a new function that does the job of the first query only. It should validate that the transient exists and hasn't expired returning a boolean.
The get_transient should then be modified to call that function rather than get_option.
The purpose here is to allow plugin / theme developers to minimize queries even further.
Example: If all I need to do is check for a valid transient that only requires 1 query, I don't always need the value, just need to know whether it should be refreshed.
In that case I could call valid_transient( 'transient' ).
Since that value can be cached if I later need the full value then only one additional query would be needed.
Attachments (1)
Change History (7)
#2
@
8 years ago
@MikeHansenMe: My pleasure. Will see what I can come up with though my ideal / proposed solution would be to simply move the initial check for a time value would simply be moved to an external function, then called in place.
Ideally no real change to the existing methods already there, just a separation into a reusable method.
In the future though some core calls could use the new method to reduce unneeded queries. Though I think its use becomes more evident with a large production site with a large mix of plugins & theme functionalities.
Personally I only tend to use transients to store a timer, then store the actual data as an option so that autoload handles it unless not needed, though I have always wondered personally why the transients are not autoloaded in general as they tend to be checked quite often.
#3
@
8 years ago
@MikeHansenMe: Just getting around to this, nearly have a patch complete, but realized there is still more room for improvement.
Why is WP not querying for both transient timeout & value in one query?
@
8 years ago
Adds 2 new functions valid_transient & valid_site_transient. Modifies get_transient & get_site_transient to use these new functions.
#4
@
8 years ago
- Keywords has-patch needs-testing added
Patch added. Tested and this allows the reduction of unnecessary queries like so.
This is how it can be used.
<?php if ( ! valid_transient( 'testing' ) ) { $value = 'value'; set_transient( 'testing', $value, 60 ); } else { $value = get_transient( 'testing' ); }
This becomes more useful when useful when using transients for timers & such.
This ticket was mentioned in PR #6650 on WordPress/wordpress-develop by @pbearne.
6 months ago
#6
- Keywords has-unit-tests added
A new function named 'valid_transient' has been introduced to verify if a transient and its value are valid, also by checking if they have not expired. Sites with transient data can use the 'valid_site_transient' function to perform similar checks. Along with these functions, PHPUnit test cases have been added to ensure these functions perform as expected.
@danieliser can you provide a patch and show what kind of performance improvements might be gained. This will also need to pass unit tests if the way get_transient function is modified.