[wp-trac] [WordPress Trac] #29351: Add a function for stampedes transient caching
WordPress Trac
noreply at wordpress.org
Sun Aug 24 20:04:52 UTC 2014
#29351: Add a function for stampedes transient caching
----------------------------+-----------------------------
Reporter: hypertextranch | Owner:
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Cache API | Version: trunk
Severity: normal | Keywords:
Focuses: performance |
----------------------------+-----------------------------
A common caching pattern that's used by WordPress and developers
everywhere look something like this:
{{{
if ( false === ( $value = get_transient( $cache_key ) ) ) {
// A
// Bunch
// Of
// Code
// $value = ...
set_transient( $cache_key, $value, 300 );
}
}}}
The downside of this approach is when the cache is cold or when the cache
expires there's a stampede effect as multiple requests try and refresh the
cache at the same time. Instead if we had a function that allowed for soft
expirations we could do something like the following:
{{{
$some_slow_func = function() {
// A
// Bunch
// Of
// Code
// $value = ...
return $value;
}
$value = get_set_soft_transient(
$cache_key,
$some_slow_func,
300,
'Some fake initial stale value'
);
}}}
This approach abstracts out the cache refresh logic into another function
which can result in slightly cleaner code and can be used to prevent cache
stampedes.
Some other optimizations / changes that we could do with this approach
that's not yet implemented in the patch:
* Run all cache refresh functions async via wp_cron so even the unlucky
winner that gets to trigger the cache refresh will get stale data (fast).
* Better locking so that only one request will refresh the cache. (Right
now a couple requests could happen to refresh at the same time on a very
busy site.)
* Perhaps prevent caching of a `false` value or one that equates to the
default passed in?
* Use filters instead of PHP callbacks to make this more WordPress like.
* Also build out `get_set_soft_site_transient()`.
This is inspired by the stampedes cache @gibrown wrote for some of
WordPress.com and the @mboynes talk at WordCamp Boston.
/cc @betzster
--
Ticket URL: <https://core.trac.wordpress.org/ticket/29351>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list