Sunday, March 12, 2017

Caching best practices and difference between OutputCache Vs Data Cache in Asp.Net


1. Page Level Output Caching

This is simple form and very easy to implement.basically Output Cache save a copy of html and when again client request for same html it will send in response from cache untill or unless that cache is not expired by using this we can have a very good performance rate and response time.

for implementation of output cache on page, simply we need to add OutputCache directive to the page.


<%@ OutputCache Duration = "60" VaryByParam= "*" %>


This directive will be shown at the top of the page same like to other directives of the page:   OutputCache supports 5 parameters in which two are required "Duration" and "VaryByParam".


Duration: (mandatory) keeps the time in seconds,for how much time we want to store cache.

VaryByParam: (mandatory) Here we can mention the names of the variables, which will be result in separate for each entry. it have a "none" parameter which can be used for no variation. mostly used parameter  is "*",is used to create a new entry in cache for different variable.but we have to separate the variables with ";" for more than one variation.

Location: (optional) In location we mention that where our cache will be cached it support different parameter like Any, Client, Downstream, None, Server or ServerAndClient.

VaryByHeader: (optional) Cache entries can be changed by using this parameter it check the variation in the header and accordingly.

VaryByCustom: (optional) it allow the custom variations which can be specified in the global.asax (e.g. "Browser").

2.Fragment Cache or User Control Cache

As its name shown fragment Caching,Most of the time we have such requirements thats we dont want to cache the whole page because that are customized to user data so in such cases where we are required to cache only specific part of page and other should not b part of cache then we will use Fragment Cache. To have different data in a page mostly we prefer to use user control which are best in performance and can be customized easily.

Examples

<%@ OutputCache Duration="60" VaryByParam="*" %>

This will cache the control for 60 seconds only and it will have separate entry for each variation in url query string and every page on which this control is added.


<%@ OutputCache Duration="60" VaryByParam="none" VaryByControl="CategoryDropDownList" %>

This will also cache the control for 60 seconds but for each value of DropDownList and on each page.


e.g. <%@ OutputCache Duration="60" VaryByParam="none" VaryByCustom="browser" shared="true %>


Shared= true make sure that in all pages it will have only one entry and all page which have same control with same id will share the cache and there will be single entry for a single browser.

3. API Caching/Data Caching Using Cache object:

Other than cache a page or control we can also cache an object it may contain xml or database return object, Page Cache and Control cache is very easy and can be done in short time whereas API cache or Data cache reuires little programming. for this Asp.net provides the cache object like session and cookies. Cache ["keyname"] = "value"; it will store value in cache without any dependencies and it will remain cached untill or unless we remove it from cache. Asp.net provides Add() and Insert() method to implement other dependencies.

To cache the whole xml file we will have below format:

Cache.Insert("key", XMLFileDataCache, new System.Web.Caching.CacheDependency(Server.MapPath("users.xml")));

so again and again no need to read data from the file once its cached then on each request we can retrive data from cache and it will also increase performance.cache will be expired whenever there is any change in xml file.

Cache.Insert("dependentkey", myDependentData, new System.Web.Caching.CacheDependency(new string[] {}, new string[]

  {"key"}));

To store an array of data into cache we can use above format.it depends upon the value key, it will expire the cache if key does not exists or updated.


Cache.Insert("key", myTimeSensitiveData, null, DateTime.Now.AddMinutes(1), TimeSpan.Zero);

Here we are using absolute expiration that means we will cache the data for a specific time or we will store the data which depends upon time interval after that time cache will expire automatically.

Cache.Insert("key", myFrequentlyAccessedData,null,  System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(1));

This is sliding expiration that means if cache is not used for a specific time then it will expire. in above example if there is no access to cache for 1 minute it will be expired.absolute and soliding expiration is not possible to use same time.

No comments:

Post a Comment