How to reset an application variable daily

Deadly 提交于 2019-12-01 21:32:01

I would store the calls to a database and do a select which groups by the current day to get the total calls, etc. for display.

That way it will automatically reset for you when a new day starts, and you don't need to worry about IIS Resets destroying your in memory data.

If you don't want the performance hit of querying too often, there are a number of caching options available.

You could configure the Periodic Restart Settings for Application Pool Recycling in IIS:

The element contains configuration settings that allow you to control when an application pool is recycled. You can specify that Internet Information Services (IIS) 7 recycle the application pool after a time interval (in minutes) or at a specific time each day. You can also configure IIS to base the recycle on the amount of virtual memory or physical memory that the worker process in the application pool is using or configure IIS to recycle the application pool after the worker process has processed a specific number of requests.

But this has a side-effect of putting the application offline during the time the pool is restarting, so if you have any user connected at that time it will lose its session. This can be minimized by restarting the application at a time you have no users connected, like at dawn.

The following config snippet set the application pool to daily recycle at 3:00 A.M.:

<add name="Example">
   <recycling logEventOnRecycle="Schedule">
     <periodicRestart>
       <schedule>
          <clear />
          <add value="03:00:00" />
        </schedule>
     </periodicRestart>
   </recycling>
 <processModel identityType="NetworkService" shutdownTimeLimit="00:00:30" startupTimeLimit="00:00:30" />
</add>

I'd have a date variable with the last time the counter was reset, and check the date is "today" on every access to the counter.

Unless you have critical performance problems, I guess that'd be the way to go.

Sample easy-lazy code to call whenever you are updating the counter:

lock(myCounter)
{ 
  if(DateTime.Now.Date != lastDateCounterWasReset)
  {
     lastDateCounterWasReset = DateTime.Now.Date;
     myCounter = 0;
  }
  myCounter++;
}

Now we'd need to know more about how you'd like to be storing those variables (myCounter and lastDateCounterWasReset), but could be basically anywhere (database, filesystem, etc.)

I suppose you could use the Application_BeginRequest method. Use a boolean to see if it's already run that day.

Another option is a scheduler with a hidden URL to reset.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!