在 ASP.NET MVC 中查看计数器

发布于 2024-09-16 16:37:57 字数 396 浏览 10 评论 0原文

我将为文章创建一个查看计数器。我有一些问题:

  1. 我应该忽略文章的作者吗 当他打开这篇文章时?

  2. 我不想每次都更新数据库 时间。我可以存储在 字典 (articleId, viewCount)多少次 每篇文章都被浏览过。 100后 点击我可以更新数据库。

  3. 我应该只计算一次点击 每个用户和文章的小时数。 (如果 用户多次打开一篇文章 一小时内观看次数 应该只增加一次)。

对于每个问题,我想知道您对如何正确解决问题的建议。

我特别感兴趣如何做#3。我应该将用户打开文章的时间存储在 cookie 中吗?这是否意味着我应该为每个页面创建一个新的 cookie?

I'm going to create a view counter for articles. I have some questions:

  1. Should I ignore article's author
    when he opens the article?

  2. I don't want to update database each
    time. I can store in a
    Dictionary<int, int> (articleId, viewCount) how many times
    each article was viewed. After 100
    hits I can update the database.

  3. I should only count the hit once per
    hour for each user and article. (if
    the user opens one article many
    times during one hour the view count
    should be incremented only once).

For each question I want to know your suggestions how to do it right.

I'm especially interested how to do #3. Should I store the time when the user opened the article in a cookie? Does it mean that I should create a new cookie for each page?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

各自安好 2024-09-23 16:37:57

我想我知道答案 - 他们正在按照 Ope 的建议分析 IIS 日志。

需要将隐藏图像src设置为

http://stackoverflow.com/posts/3590653/ivc/[Random code]

[随机代码],因为许多人可能共享相同的IP(例如在网络中),并且代码用于区分用户。

I think I know the answer - they are analyzing the IIS log as Ope suggested.

Hidden image src is set to

http://stackoverflow.com/posts/3590653/ivc/[Random code]

[Random code] is needed because many people may share the same IP (in a network, for example) and the code is used to distinguish users.

甲如呢乙后呢 2024-09-23 16:37:57
  1. 当然 - 我认为这是一个好主意

  2. 和 3. 相关:问题是你实际上在哪里存储这个字典和逻辑。

ASP.NET 应用程序或会话范围当然是最简单的选择,但您确实需要了解应用程序池的逻辑。 ASP.NET 应用程序会不时地被回收:当站点在一段时间内没有任何操作或在特殊情况下 - 例如,如果进程开始占用太多内存,则应用程序将关闭并启动一个新的应用程序下一个请求。有会话和应用程序关闭的事件,但至少在几年前它们并不真正可靠:在许多特殊情况下它们并不总是触发。也许他们现在好多了,但测试是痛苦的。 1 小时确实是一个很长的时间:通常会话仅在上次请求后 20 分钟才保持活动状态。

一种可靠的方法是拥有一个单独的 Windows 服务(需要大量的编程工作)或始终通过双视图分析存储到数据库(对于如此小的功能来说,开销相当大)。

您有权访问 IIS 日志吗?如何使用某种计时器进程分析 IIS 日志(例如每 30 分钟一次)并从中进行计数?或者然后将所有点击与用户信息一起存储到数据库中,并使用类似的定时过程计算唯一点击。

最后一个问题:您真的确定互联网上数以千计的计数器应用程序/服务中没有一个不能满足您的要求吗?

祝你好运!

  1. Sure - I think that is a good idea

  2. and 3. are related: The issue is where would you actually store this dictionary and logic.

An ASP.NET application or session scope are of course the easiest choice, but there you really need to understand the logic of application pools. ASP.NET applications are recycled from time to time: when there is no action on the site for a certain period or in special situations - e.g. if the process starts to take too much memory the application is shut down and a new one is started in the next request. There are events for session and application shut-down, but at least some years ago they were not really reliable: In many special cases they did not always fire. Perhaps they are better now, but it is painful to test. And 1 hour is really a long time: Usually sessions are kept alive only like 20 minutes after last request.

A reliable way would be to have a separate Windows service (a lot of work to program) or always storing to database with double-view analyses (quite a lot of overhead for such a small feature).

Do you have access to IIS logs? How about analyzing IIS logs e.g. every 30 minutes with some kind of timer process and taking the count from there? Or then just store all the hits to the database with user information and calculate the unique hits with a similar timed process.

One final question: Are you really sure none of the thousands of counter applications/services in the Internet wouldn't do the job close enough to your requirements?

Good luck!

不顾 2024-09-23 16:37:57

这是 Firebug 中该页面的屏幕截图。您可以看到有一个请求返回 204 状态代码(无内容)。

这是 stackoverflow 的视图计数器。他们使用指向控制器操作的隐藏图像。

我有很多文章。如何跟踪用户已经访问过哪些文章?

PS BTW,为什么这个请求提出了两次?

替代文字

This is the screenshot of this page in Firebug. You can see that there is a request which returns 204 status code (No Content).

This is stackoverflow's view counter. They are using a hidden image which point to a controller's action.

I have many articles. How to track which articles the user visited already?

P.S. BTW, why is this request made two times?

alt text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文