1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

View Counts

Discussion in 'General PHP and MySQL Discussions' started by Robust, Oct 25, 2015.

  1. Robust

    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?
  2. Robust

    Robust Well-Known Member

    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.
  3. Jim Boy

    Jim Boy Well-Known Member

    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.
  4. Robust

    Robust Well-Known Member

    Laravel does support Redis out of the box, and Redis was a god when it came to Java, so that's always an option

Share This Page