Make WordPress Core

Opened 9 years ago

Last modified 38 hours ago

#37040 assigned enhancement

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

Reported by: danieliser's profile danieliser Owned by: pbearne's profile pbearne
Milestone: 6.8 Priority: normal
Severity: normal Version: 4.6
Component: Options, Meta APIs Keywords: has-patch needs-testing has-unit-tests needs-dev-note
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 8 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 (12)

#1 @MikeHansenMe
9 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
9 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
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?

@danieliser
8 years ago

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

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

#5 @pbearne
9 months ago

  • Owner set to pbearne
  • Status changed from new to assigned

This ticket was mentioned in PR #6650 on WordPress/wordpress-develop by @pbearne.


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

#7 @pbearne
3 weeks ago

  • Milestone set to 6.8

#8 @audrasjb
3 weeks ago

  • Keywords needs-dev-note added

#9 follow-up: @johnbillion
3 weeks ago

These functions won't work when a persistent object cache is in use. What can be done about that?

#10 in reply to: ↑ 9 @pbearne
2 weeks ago

Replying to johnbillion:

These functions won't work when a persistent object cache is in use. What can be done about that?

Can we make a cache call to see if we have value as part of the cache?

#11 @flixos90
38 hours ago

Per the feedback above, the PR https://github.com/WordPress/wordpress-develop/pull/6650 does not fully address the problem here as it doesn't work as expected when a persistent object cache is used.

I'm personally not sure about the value of having this function from a performance perspective, but I'm not opposed either. But the above concern on persistent object cache support needs to be addressed for this to land.

We're closing in on 6.8 Beta, so this will probably need an update soon or otherwise would need to be punted to the following release.

Note: See TracTickets for help on using tickets.