WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 14 months ago

#37040 new enhancement

Enhancement: new function to validate a transient exists, and isn't expired without extra query

Reported by: danieliser Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.6
Component: Options, Meta APIs Keywords: has-patch needs-testing
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)

valid_transients.patch (3.7 KB) - added by danieliser 4 years ago.
Adds 2 new functions valid_transient & valid_site_transient. Modifies get_transient & get_site_transient to use these new functions.

Download all attachments as: .zip

Change History (5)

#1 @MikeHansenMe
4 years ago

@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.

#2 @danieliser
4 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 @danieliser
4 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?

@danieliser
4 years ago

Adds 2 new functions valid_transient & valid_site_transient. Modifies get_transient & get_site_transient to use these new functions.

#4 @danieliser
4 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.

Note: See TracTickets for help on using tickets.