Closed MickeyKay closed 9 years ago
Note that updates are async (which is kind of the point). Your object will be serialized and stored, then unserialized to be run in separate environment. That very well might break it and require special wakeup handling.
Thanks @Rarst . A few more questions to follow up. . .
How soon should the background callback fire? When I load the page the first time, it makes sense that nothing shows, right? However it seems like when I load it for the second time, a minute or so later, I my transient should've been set by then, right? By chance does this not work on localhost?
Secondly, what do you recommend for forcing the transient to run synchronously the first time (e.g. if it doesn't already exist)? Would something like this work:
if ( $transient->get() ) {
$transient->background_only();
}
return $transient->get();
Background only is opt-in, you should see result generated on first load by default.
Right, but my question is, how would you running it in the background if and only if the transient is already set? That way it won't run the first time, but will run every time after that - so the only slow load will be the first one.
I am not sure I follow.
Default behavior:
background_only()
behavior:
There is no reason to make background_only()
conditional since just using it flips the first run logic one way or another.
Got it. So the only difference is that background_only forces the async generate every time, including the first time?
I'm trying to pass a class method to the
updates_with
method like so:$transient->updates_with( array( $this, 'fetch_api_data' ), $url );
But it doesn't seem to be working. Is this not set up to work inside a class, or am I missing something perhaps?