如何“查看计数”最好实施? [英] How "View Count" is best implemented?

查看:124
本文介绍了如何“查看计数”最好实施?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在任何网站上,如StackOverflow,每个问题都有一个查看次数,用户阅读了一个问题,但以前读过它不会计算两次。我有一些关于它如何实现的想法,并使用什么表来实现。



你认为最好的方法是什么?

解决方案

您有几个选项,如我所见。



/ strong>



您可以在用户浏览器中为每个正在记录视图的页面存储一个cookie。检查此Cookie是否存在,如果Cookie已存在,则不记录视图。



不利的是,如果Cookie被禁用或有人试图游戏系统,它将无法正常工作。



在正面方面,您不必担心可能存储数百万/数十亿行的表格数据。



数据库



您拥有每个视图的记录。以某种方式将该记录关联给用户,例如会员ID,IP地址;应该是用户唯一的东西。 IP不是理想的,但是如果您不要求用户登录,则可以使用不够好。



所以你可以举个例子列出以下列:




  • ArticleID(Foreign Key)

  • UserID(Foreign Key)

  • 日期



由于几个原因,日期将会有用,




  • 报告。一旦你知道每个视图的记录时间,你可以建立更好的统计信息。

  • 查看超时。例如,您可能只希望每个用户每小时存储一个视图。在日期列中,您可以执行此操作。



如果您的应用程序在这种情况下受欢迎,那么您将需要处理存储影响我运行流行的Facebook应用程序,每天添加超过100,000个视图行。实际上,如果你的应用程序如此受欢迎,这成为一个问题,那么你会有更多的问题要处理。


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 about how it is implemented and using what tables to do it.

What do you think is the best way to implement this?

解决方案

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.

这篇关于如何“查看计数”最好实施?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆