开发者

Modifying Application Variables in ASP.Net (MVC)

I store a large structure holding my appli开发者_如何学Pythoncation's reference data in a variable I access through HttpContext.Application. Every once in a while this data needs to change. When I update it in place, is there a danger that incoming requests will see the data in an inconsistent state? Is there a need (and a way) to lock some or all of this structure? Finally, are there other approaches to this problem other than querying the database every time you need this (mostly static) data?


There are also other solutions availiable, there are many caching providers that you can use.

First of all, there's the HttpRuntime.Cache (which is the same as the HttpContext cache). There's also the System.Runtime.Caching.MemoryCache in .NET 4.

You can set data expiry and other rules for the data in the cache.

http://wiki.asp.net/page.aspx/655/caching-in-aspnet/

http://msdn.microsoft.com/en-us/library/6hbbsfk6.aspx

http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.aspx

More advanced caching includes distributed caches.

Usually, they reside on another server but may also reside on a different process on the same server. Such providers are AppFabric (from Microsoft) and MemCached and others that I can't recall currently.

appfabric: http://msdn.microsoft.com/en-us/magazine/ff714581.aspx

memcached: http://memcached.org/


You will not see the application variable in inconsistent state.

The MSDN page for HttpApplicationState says (Under the Thread Safety section):

This type is thread safe.


You may be looking for HttpContext.Items instead to store data in the request scope instead of the application scope. Check out this article to get a great overview of the different context scopes in ASP.NET.

Your solution to avoid querying the database for "mostly static data" is to leverage ASP.NET's caching.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜