markjaquith / WP-TLC-Transients

GNU General Public License v2.0
341 stars 37 forks source link

updates_with() not working for callback method inside class #32

Closed MickeyKay closed 9 years ago

MickeyKay commented 9 years ago

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?

Rarst commented 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.

MickeyKay commented 9 years ago

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();
Rarst commented 9 years ago

Background only is opt-in, you should see result generated on first load by default.

MickeyKay commented 9 years ago

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.

Rarst commented 9 years ago

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.

MickeyKay commented 9 years ago

Got it. So the only difference is that background_only forces the async generate every time, including the first time?

Rarst commented 9 years ago

Yes, precisely. See Examples in readme. :)