简体   繁体   English

ASP.NET缓存内存保存代码

[英]ASP.NET cache memory saving code

I am trying to create a site view counter for my ASP.NET-4.0 MVC-4 website. 我正在尝试为我的ASP.NET-4.0 MVC-4网站创建一个站点视图计数器。 For this, what I am doing is: 为此,我正在做的是:

I am storing the User's IP address in asp.net cache . 我将用户的IP地址存储在asp.net cache And when the cache expired, I will do a bulk entry in my SQL Server Database. 当缓存过期时,我将在SQL Server数据库中进行批量输入。

Now for this I have to write a code which will insert the user's IP address in the cache memory. 现在,我必须编写一个代码,将用户的IP地址插入高速缓存中。 Initially I decided to write that code in my /Home/Index Action but then I thought if user visit my website's contact page directly and than leave my website without going to my website's home page then that user's IP address will not record in cache memory. 最初,我决定在/Home/Index Action编写该代码,但是后来我认为,如果用户直接访问我的网站的联系页面,而不是离开我的网站而不访问我的网站的主页,那么该用户的IP地址将不会记录在缓存中。

Now the obvious solution for this problem is that we can write a function and put a call to that function in My home and contact action and every other action. 现在,针对此问题的明显解决方案是,我们可以编写一个函数,然后在“我的家”和联系操作以及所有其他操作中对该函数进行调用。 But I want something by which we can avoid writing function call at every place. 但是我想要一些可以避免在每个地方编写函数调用的方法。

Can anybody please suggest me a good solution for this ? 有人可以为此建议我一个好的解决方案吗?

I think you mean "cache", not "catch"? 我认为您是说“缓存”,而不是“捕获”?

I also think you want one place that runs on every request, instead of having to put code in every controller. 我还认为您想要一个可以在每个请求上运行的地方,而不是必须在每个控制器中放置代码。

In that case, you should look at adding to your Global.asax an event for Application_BeginRequest. 在这种情况下,您应该查看将Application_BeginRequest事件添加到Global.asax中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM