ASP.NET应用程序状态

编程入门 行业动态 更新时间:2024-10-13 20:18:12
ASP.NET应用程序状态 - 创建“每分钟请求数”计数器(ASP.NET Application state - Creating a “requests per minute” counter)

这一定很容易,但是我的大脑拒绝解决这个问题。

在ASP.net中,可以直接使用应用程序状态创建全局“页面请求计数器”。 只需增加一个应用变量:

Application("Hitcount") = Application("HitCount") + 1

管他呢。 但是,我如何只测量过去一分钟内收到的点击数? 我需要在过去的60秒内持续计算应用程序命中数,以便我能够对传入的请求进行一些流量管理。 例如,如果当前负载超过每分钟1,000次点击,则重定向到某处。

This must be easy but my brain refuses to work it out.

In ASP.net it's straight-forward to create a global "page request counter" using the application state. Just increment an application variable:

Application("Hitcount") = Application("HitCount") + 1

or whatever. But how would I measure only those hits received in the past minute? I require an ongoing count of the application hits over the previous 60 seconds, to enable me to do some traffic management of the incoming requests. E.g. if the current load is over 1,000 hits per minute then redirect somewhere.

最满意答案

您可以使用内存缓存来执行此操作。 这将计算超过10分钟的滚动请求计数。 不过,我还没有测试过它的性能。

public BaseController() : base() { CacheItemPolicy policy = new CacheItemPolicy(); policy.AbsoluteExpiration = DateTime.UtcNow.AddMinutes(10); MemoryCache.Default.Add(Guid.NewGuid(), "RequestCount", policy); } public int RequestCountPerMinute { get { return MemoryCache.Default .Where(kv => kv.Value.ToString() == "RequestCount").Count() / 10; } }

You might be able to do this using memory cache. This would calculate a rolling request count over 10 minutes. I haven't tested the performance of this, though.

public BaseController() : base() { CacheItemPolicy policy = new CacheItemPolicy(); policy.AbsoluteExpiration = DateTime.UtcNow.AddMinutes(10); MemoryCache.Default.Add(Guid.NewGuid(), "RequestCount", policy); } public int RequestCountPerMinute { get { return MemoryCache.Default .Where(kv => kv.Value.ToString() == "RequestCount").Count() / 10; } }

更多推荐

本文发布于:2023-08-01 13:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1359251.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   状态   ASP   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!