0

I've created an AJAX function that updates the views count of each post load.

public function updateViewCount()
{
    // ...

    $postID = $_REQUEST['post_id'];
    $key = 'post_views_count';
    $count = get_post_meta($postID, $key, true);

    if ($count == '') {
        $count = 0;
        delete_post_meta($postID, $key);
        add_post_meta($postID, $key, '1');
    } else {
        $count++;
        update_post_meta($postID, $key, $count);
    }

    // ...
}

This works fine for sites without lots of users. However, I need to deploy this on a large site with over 50.000 sessions per day. I fear that this function will slow down the site to a crawl.

I've thought about gathering these views count updates in a transient and then run a cron job a few times a day to save the counts to their respective post. Now, according to the official transients docs, "Transients should also never be assumed to be in the database, since they may not be stored there at all." Let's say they are stored in the database with add_option()/update_option(), how perfomant is this relative to just keeping my current function the way it is?

Your Answer

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

Browse other questions tagged or ask your own question.