Site icon Hip-Hop Website Design and Development

Gathering and logging knowledge from a plugin: find out how to do it with out race circumstances?

I’ve a must seize knowledge in a WordPress plugin and append it into some type of log.

The info I need occurs to be sluggish SQL queries from $wpdb->queries. I am hoping to current helpful "dirty dozen" SQL queries to my customers to assist them with optimizations. It is a companion plugin, or possibly a function set, for this plugin. (I am conscious of the efficiency points round this sort of system instrumentation. Seize classes might be restricted in period. And so forth.)

In every related pageview I’ll seize some knowledge and log it. Then later my plugin will retrieve, course of, and delete the log. How can I do that knowledge seize in a WordPress-friendly approach?

I am considering of utilizing a transient to carry the log. I am utilizing expiring transients in my hope to keep away from unprocessed logs from accumulating. My knowledge movement would look one thing like this (pseudocode).

$logdata = get_transient( $tName );
$logdata .= $newdata;
set_transient( $tName, $logdata, 86400 );

However, on a busy web site a number of php cases will race one another. One occasion will do its get_transient(), then one other occasion will do its get earlier than the primary occasion does its set. So one of many cases’ knowledge might be misplaced.

If I have been to do that with plain outdated MyISAM-compatible SQL I might do one thing like this:

UPDATE wp_options WHERE option_name='tname' SET option_value = option_value + 'newvalue'

That avoids the doable race situation. It will be nice if there have been an append_transient($identify, $val, $timeout) perform to do that operation.

I might additionally lock the desk, however that may be actually impolite.

If I knew my plugin was on a web site with InnoDB in its database I might use an SQL transaction. However I do not know that.

Is there a greater approach?