0

I have a need to capture data in a WordPress plugin and append it into some sort of log.

The data I want happens to be slow SQL queries from $wpdb->queries. I'm hoping to present useful "dirty dozen" SQL queries to my users to help them with optimizations. It's a companion plugin, or maybe a feature set, for this plugin. (I'm aware of the performance issues around this kind of system instrumentation. Capture sessions will be limited in duration. Etc.)

In each relevant pageview I will capture some data and log it. Then later my plugin will retrieve, process, and delete the log. How can I do this data capture in a WordPress-friendly way?

I'm thinking of using a transient to hold the log. I'm using expiring transients in my hope to avoid unprocessed logs from accumulating. My data flow would look something like this (pseudocode).

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

But, on a busy site multiple php instances will race each other. One instance will do its get_transient(), then another instance will do its get before the first instance does its set. So one of the instances' data will be lost.

If I were to do this with plain old MyISAM-compatible SQL I'd do something like this:

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

That avoids the possible race condition. It would be great if there were an append_transient($name, $val, $timeout) function to do this operation.

I could also lock the table, but that would be really rude.

If I knew my plugin was on a site with InnoDB in its database I could use an SQL transaction. But I don't know that.

Is there a better way?

2
  • 1
    If you're considering a transient, does that mean the log should expire? Why not just use a custom table? What are you logging, and how frequently do you expect to log it? Sep 16 at 12:38
  • Please see my edit. I hope to avoid a custom table if at all feasible.
    – O. Jones
    Sep 16 at 13:03
1

You can create a lock by using flock on one of your existing files (or create a special one fo this. Get the lock before you start the canculation and release it after the result was stored.

1

I finally solved my problem with this function. It appends to a transient in the MySQL database in a race-condition-safe way.

/** Upsert and append data to a WordPress transient.
 * This function deletes the transient from the WordPress options cache to avoid stale data.
 * @param string $transient Transient name.
 * @param string $value Data to append.
 * @param int $expiration Transient expiration (default 120 sec). 0 means the transient does not expire.
 * @param string $separator Separator between appended values (default '|||').
 * @param int $maxlength Stop appending when value reaches this length to avoid bloat (default 512 KiB).
 */
function append_to_transient( $transient, $value, $expiration = 120, $separator = '|||', $maxlength = 524288 ) {
    if ( ! $transient || strlen( $transient ) === 0 ) return;
    if ( ! $value ) return;
    try {
        global $wpdb;
        if ( $expiration ) {
            $name = '_transient_timeout_' . $transient;
            wp_cache_delete( $name, 'options' );
            $query = "INSERT IGNORE INTO $wpdb->options (option_name, option_value, autoload) VALUES (%s, %d, 'no')";
            $query = $wpdb->prepare( $query, $name, $expiration + time() );
            $wpdb->get_results( $query );
        }
        $name = '_transient_' . $transient;
        wp_cache_delete( $name, 'options' );
        $wpdb->get_results( $wpdb->prepare( "SET @upload = %s", $value ) );
        $query = "INSERT INTO $wpdb->options (option_name, option_value, autoload) VALUES (%s, @upload, 'no')"
                 . "ON DUPLICATE KEY UPDATE option_value ="
                 . "IF(%d > 0 AND LENGTH(option_value) <= %d - LENGTH(@upload),"
                 . "CONCAT(option_value, %s, @upload), option_value)";
        $query = $wpdb->prepare( $query, $name, $maxlength, $maxlength, $separator );
        $wpdb->get_results( $query );
    } catch ( Exception $e ) {
        /* empty, intentionally, don't crash when logging fails */
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.