I'm building a WordPress plugin that uses server-sent events to update a page when data changes.
Here's a snippet from that code:
while ( true ) {
// Send the updates to the client.
$this->send_sse_event( 'updates_push', get_transient( 'name_of_the_transient' );
sleep( 5 );
if ( connection_aborted() ) {
break;
}
}
The problem is that the events sent from that script hold stale data... meaning when another request to some PHP script changes the value of the transient, the server-sent event still sends the old value until I refresh the page.
Any ideas on how to solve this?
The problem seems to be default object cache of the managed hosting provider. After a call to wp_cache_flush()
the transient returns the correct data. Therefore, the solution is to exclude the transient from caching/revalidate the cache for that transient before you use its value.