View Counts

Robust

Well-known member
So I'm working on something non-XF related. It's based on Laravel (PHP framework).

I have news articles and like to have view counts on them. What's the best way to go about it?

I was thinking, for logged in users each literal view is also a view onto the thing. For guests (non logged in users), a session is a view.

I'm not sure if I want to be calling the database to update this var on every page load. I was thinking perhaps save it into Laravel's cache, then every 30 minutes start a Laravel scheduler task to update the values from the cache into the database, then remove the key from the cache.

Pretty generically speaking, how do you advise going about view counts and saving them to the database? What does XF do?
 
I asked a few people and apparently a query per page load of a logged in member and a query of a guest session isn't a bad idea.
 
Bunch of different ways you can do it, but if you adopt the method you mentioned, you only need to cache for a few seconds, maybe 30. Any caching for a sort period will stop the issue of multiple concurrent writes to the database and if you keep it short will make the update query much smaller and less likely to have a proolonged lock on the database.

A more 'enterprise' system would write the data to something like redis, or even elasticsearch.
 
Bunch of different ways you can do it, but if you adopt the method you mentioned, you only need to cache for a few seconds, maybe 30. Any caching for a sort period will stop the issue of multiple concurrent writes to the database and if you keep it short will make the update query much smaller and less likely to have a proolonged lock on the database.

A more 'enterprise' system would write the data to something like redis, or even elasticsearch.
Laravel does support Redis out of the box, and Redis was a god when it came to Java, so that's always an option
http://laravel.com/docs/5.1/redis
 
Back
Top Bottom