splattne
splattne

Reputation: 104050

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

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?

Upvotes: 7

Views: 11456

Answers (7)

Shirish Patel
Shirish Patel

Reputation: 63

I totally disagree that web.config values are cached. It is continuously getting accessed by IIS.

I have about 30 AppSettings Keys in my web.config.

E.g.

<appSettings>
        <add key="ProductImageFileType" value=".jpg|.jpeg|.gif|.png" />
</appSettings>

In my ASP.Net C# Code I have

string ValidateType = ConfigurationManager.AppSettings["ProductImageFileType"].ToString();

See the below image in which the web.config Read Count is shown which is increasing every second.

enter image description here

My advice here is get the values from ConfigurationManager.AppSettings during Application_Start and store the Key Value pairs in a Global Dictionary.

Upvotes: 0

victor_c
victor_c

Reputation: 171

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.

Upvotes: 5

Ganesan
Ganesan

Reputation: 73

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.

Upvotes: -1

sanjay
sanjay

Reputation: 1

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.

Upvotes: -1

AnthonyWJones
AnthonyWJones

Reputation: 189505

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.

Upvotes: 8

Jon
Jon

Reputation: 2085

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.

Upvotes: 1

Michiel Overeem
Michiel Overeem

Reputation: 3992

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.

Upvotes: 1

Related Questions