ASP.NET: Where/how is web.config cached?

2019-01-12 06:40发布

问题:

I read somewhere in the Microsoft documentation that the content of the ASP.NET's web.config is cached. If that is true, where is it cached - in memory or on disk?

And a follow-up question: are there any performance considerations I have to make, if I have to access the web.config intensively?

回答1:

Its cached in memory, caching on disk doesn't make any sense, its already on disk.

First of all in ASP.NET you want to ensure you access configuration sections through the HttpContext object's GetSection method (this uses the cached copies managed by ASP.NET).

Performance of accessing config values is a function of the internal implementation of the Section object (the object returned by GetSection). A ConfigurationSection may simply act as wrapper for a DOM node which it may read on every request for a property. OTH it could internaly cache the value and watch for changes.

My advice would be keep your code simple and just access the values you need via GetSection rather than attempt to hold copies of them elsewhere but by all means maintain a reference to the object returned by GetSection for the duration of a request if you are going to fetch multiple values from it.



回答2:

In ASP.NET, the <appSettings> section is cached to memory after 1st access:

  • http://msdn.microsoft.com/en-us/library/aa478432.aspx
  • http://weblogs.asp.net/stevewellens/web-config-is-cached

ASP.NET restarts the application if there are updates to the web.config file.



回答3:

I think that the web.config is cached in memory (in object instances from System.Web.Configuration). Those are reloaded when the .config file is changed (and thus reloading your web app).

Hitting those objects is unlikely to give you a performance bottleneck. But if you have to do parsing etc, you might want to hold on the the parsed objects.

[Extra] I good practice (I think at least) is to create static properties in your global.asax.cs file for you appsettings. You can instantiate those properties in the application_start method and use them through out your web app. This prevents you from using hard-coded string (configuration keys) throughout your code.



回答4:

It's cached in memory. Caching on disk doesn't make a lot of sense for something that's accessed often and is already in a format where you can turn it into an easily stored data structure. My advice would be to freely access it as it will be as fast as any scheme you come up with for storing it and probably faster.



回答5:

My advice would be to use it just like anyother variable for the simple reason that data is cached. An if you create static variables in global.asax you are forcing yourself to write more code. No matter how planned you are, it is highly likely that you add variables in appconfig frequently during the development stage.



回答6:

There are 2 types of Caching in ASP .NET.

  1. Application Caching - internal object cache based on memorylimitation, time limits and other dependency

  2. Page Output Caching - rendered page cache at server. Both of them are memory based; not disk.