lundi 11 janvier 2016

Timing issues in MySQL and PHP

We want to keep track of the number of pages that a page is loaded so I've got this method to do so:

function track_views($postID) {
    $count_key = 'post_views';
    $count = get_post_meta( $postID, $count_key, true );
    if($count == '') {
        delete_post_meta( $postID, $count_key );
        add_post_meta( $postID, $count_key, '1' );
    } else {
        $count++;
        update_post_meta( $postID, $count_key, $count );
    }
}

What I'm wondering about is could this potentially miss views? If two users load the page at the exact same time or if there is high traffic, will the little of time to get the field, update it and reinsert it make this count not 100% accurate?

Ex: User 1 loads the page - page count is 4 while updating that var to 5, user two loads and gets page count 4 as well. updating it to 5 and reinserting it.

^is that how it would work? Or would the timing not really make too big of a difference?

We don't really care a whole lot about the number of times the page loads so we aren't planning on having a table filled with records of each load (insert a new record every load) but we would like to know if this would have a race issue against other page loads...and/or if it would create a conflict.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire