Download the PHP package 10up/async-transients without Composer
On this page you can find all versions of the php package 10up/async-transients. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download 10up/async-transients
More information about 10up/async-transients
Files in 10up/async-transients
Package async-transients
Short Description Transients, that regenerate asynchronously once content is expired, and serve stale content in the mean time
License MIT
Informations about the package async-transients
Async Transients
Transients that serve stale data while regenerating the new transients in the background.
Background & Purpose
Transients are great for storing data that is expensive to regenerate, but we still run in to the problem of needing to regenerate that data synchronously once the transient expires. This library solves that problem by serving stale data once the transient is expired, and processing the regenerate callback after the request has finished, so that end users never see the impact of regenerating transients.
Requirements
Requires support for fastcgi_finish_request
, or else transients will regenerate expired data immediately.
Installation
This library is meant to be included with composer. To install, run composer require 10up/async-transients
. The
library is set up to use composer's autoloader, so make certain you are loading your vendor/autoload.php
file.
Usage
Usage is similar to standard WordPress transient functions, in that you provide a transient key and an expiration time, but its different in that you must also provide a callback function, as well as any (optional) parameters to pass to the callback function, that should be called to regenerate the transient data if it is expired.
Example Usage:
How does all of this work?
First, when calling get_async_transient
, you now have to pass a callback function, and optionally, any parameters to
pass to the callback function. The transient is then retrieved, much like how WordPress core would retrieve it, but
with a key difference. Instead of returning nothing if the transient is expired, we return the last known value, and
add the callback function and params to a queue, to process later. By the end of the request, we have a queue of that
contains callback functions for all transients that were accessed, that had expired data.
Next, we hook into the WordPress shutdown
action. The shutdown
action runs just before PHP shuts down execution. The
Transient class hooks into that action, and calls the fastcgi_finish_request
function, if it is available.
That function flushes all response data to the client, and as far as the browser is concerned, the request is done,
however, php is allowed to keep running in the background.
At this point, we iterate over all the callback functions in the queue, which then regenerate any transient data that was accessed, but was expired.
Issues
If you identify any errors or have an idea for improving the plugin, please open an issue. We're excited to see what the community thinks of this project, and we would love your input!
Support Level
Stable: 10up is not planning to develop any new features for this, but will still respond to bug reports and security concerns. We welcome PRs, but any that include new features should be small and easy to integrate and should not include breaking changes. We otherwise intend to keep this tested up to the most recent version of WordPress.