XF 2.0 View counts

Lukas W.

Well-known member
I'm trying to replicate the way view counts for threads are done for my own projects entities. However, I haven't been able to fully figure out, why XF2 actually does, what it does.

There is this snippet in the \XF\Repository\Thread:
PHP:
public function logThreadView(\XF\Entity\Thread $thread)
    {
        $this->db()->query("
            INSERT INTO xf_thread_view
                (thread_id, total)
            VALUES
                (? , 1)
            ON DUPLICATE KEY UPDATE
                total = total + 1
        ", $thread->thread_id);
    }

public function batchUpdateThreadViews()
    {
        $db = $this->db();
        $db->query("
            UPDATE xf_thread AS t
            INNER JOIN xf_thread_view AS tv ON (t.thread_id = tv.thread_id)
            SET t.view_count = t.view_count + tv.total
        ");
        $db->emptyTable('xf_thread_view');
    }

But I fail to see through the actual point of having such a table instead of directly updating the thread table. What is the benefit of this approach?
 
The thread view table is a memory table, so it's not hitting disk on every thread view. Conversely, xf_thread is an InnoDB table and depending on InnoDB config (noting that many of our customers don't have particularly good InnoDB configs), you'd be writing to disk and calling an fsync on each thread view which is likely less scalable.
 
Thanks a lot for the insight. I never paid any particular attention to this setting in my phpmyadmin. Is there a way to create such memory-tables myself with the existing methods in XF2 upon addon installation?
 
Have a look in the XF\Install\Data\MySql class. All of our tables are created with the schema manager so you can see what's necessary for each.
 
Back
Top Bottom