How “View Count” is best implemented?

前端 未结 6 521
梦谈多话
梦谈多话 2021-01-30 18:07

On any website, such as on StackOverflow, each question has a view count, and user reading a question but has previous read it won\'t count twice.

I have some ideas abou

6条回答
  •  心在旅途
    2021-01-30 18:38

    You have a couple of options as I see it.

    Cookies

    You can store a cookie in the users browser for each page you are logging views on. Check for this cookies existence and don't log a view if the cookie already exists.

    Downside to this is that it won't work if cookies are disabled or someone is trying to game the system.

    On the plus side you don't have to worry about the storage of potentially millions/billions of rows of table data.

    Database

    You hold a record for each view. Relating that record to a user in some way e.g. MemberID, IP Address; something that should be unique to the user. IP is not ideal but good enough if you are not requiring users to login.

    So you would have for example a table with the following columns,

    • ArticleID (Foreign Key)
    • UserID (Foreign Key)
    • Date

    The date will be useful for a couple of reasons,

    • Reporting. You can build much better statistics once you know when each view was recorded.
    • View Timeouts. For example, you may only want to store one view per user per hour. With the date column held you can do this.

    If your application gets popular in this situation then you will need to deal with the storage implications. I run a popular Facebook app which results in over 100,000 view rows being added each day. Realistically though if your app gets so popular that it becomes a problem then you'll have much bigger issues to deal with.

提交回复
热议问题