When creating an app in ASP.Net MVC and using IIS 7 to host it, there are some easy tricks that can be used to improve its performance.
1. Enable caching static files
Doing this tells browsers that they can cache specified files and don’t need to download them from the server again when needed. To do this add following in web.config file
<system.webServer>
<caching>
<profiles>
<add extension=".gif" policy="CacheUntilChange" kernelCachePolicy="DontCache" location="Client" />
<add extension=".jpg" policy="CacheUntilChange" kernelCachePolicy="DontCache" location="Client" />
<add extension=".png" policy="CacheUntilChange" kernelCachePolicy="DontCache" location="Client" />
<add extension=".css" policy="CacheUntilChange" kernelCachePolicy="DontCache" location="Client" />
<add extension=".js" policy="CacheUntilChange" kernelCachePolicy="DontCache" location="Client" />
</profiles>
</caching>
<staticContent>
<clientCache cacheControlCustom="public" cacheControlMaxAge="30.00:00:00" cacheControlMode="UseMaxAge" />
</staticContent>
</system.webServer>
2. Enable compression
GZip compression is supported by all modern browser. Enabling GZip compression reduces transfer size and thus reducing the latency. This can be done simply by adding following in web.config file -
<system.webServer>
<urlCompression doDynamicCompression="true" doStaticCompression="true" />
<httpCompression dynamicCompressionEnableCpuUsage="80" />
<staticContent>
<remove fileExtension=".js" />
<mimeMap fileExtension=".js" mimeType="text/javascript" />
</staticContent>
</system.webServer>
3. Enable keep alive
This keeps the http connection open for future requests from same session and thus saving round trip time (RTT) and time spent in opening and closing connections. It can be done by adding following code in web.config file -
<system.webServer></system.webServer>
<httpProtocol allowKeepAlive="true" />
4. Output caching
This might not be suitable to enterprise apps where displaying latest transactional data is more important then displaying few min old data very fast. To do this define a profile by adding following xml snippet in web.config file and associate that profile with any action as follows -
<system.web>
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<clear />
<add name="MyCacheProfile" duration="300" />
</outputCacheProfiles>
</outputCacheSettings>
</caching>
<system.web>
[OutputCache(CacheProfile = “MyCacheProfile")]
public ActionResult Index()
{
return View();
}
5. Refreshing cache in background thread
This can only be applied where there is finite and know set of items that need to be cached. For example in live post there is a caching layer between database and app server and there is fixed number of news categories and subcategories. In the app I run a thread in the background that refreshes the in memory cache with data for these news categories. To avoid using threading code I have used CacheItemRemovedCallback provided by HttpRuntime.Cache to get a call back after specified interval and refresh the cache on receiving callback. It all starts on Applicaiton_Start() method in Global.asax file that is called when the application starts in the web server.
protected void Application_Start()
{
RegisterCallback();
}
private void RegisterCallback()
{
HttpRuntime.Cache.Add("expiryKey", "any value", null, DateTime.MaxValue, TimeSpan.FromMilliseconds(60000), CacheItemPriority.Normal, new CacheItemRemovedCallback(MyCallback));
}
private void MyCallback(string key, object value, CacheItemRemovedReason reason)
{
RefreshCache();
RegisterCallback();
}
0 nhận xét:
Đăng nhận xét