Friday, August 19, 2016

Caching

  • To Store frequently used data in memory.
  • 1st Time will get from DB and retain a copy in memory. Any request during duration will get from cache.
  • After duration, the next request will get from DB again and cache for another duration.
  • Improve the performance and scalability. 
  • VaryByParam="None" , a seperate response is cached for GET and POST request, that's why when you change drop down (POST), it will keep showing back same selection.
  • Disadvantage: cached pages that display results can be inconsistent, i.e. not reflecting the latest changes applied to a database.
1)Output Caching
  • Store a copy rendered HTML pages to the client.
<%@ OutputCache Duration="10"//Second 
VaryByParam="ddlProducts" //No use then None
Location="ServerAndClient" //Where the cache store %>

OR Code Behind :

Response.Cache.SetExpires(DateTime.Now.AddSeconds(30));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetValidUntilExpires(true);
Response.Cache.VaryByParams["ddlProducts"] = true;


2)Fragment Chaching
  • partial Caching use for specific section of page is static and take long time to load. Others is process like normal. E.g. past 10 year total sales.
  • Use in UserControl
  • "Shared =  true" attribute in OutputCache only for userControl, it allow many web forms share a single userControl.
Steps :
  1. Encapsulate specific sections of that page put into userControl.
  2. Use OutputCache directive on that userControl to specify the cache settings.
  3. Drag and drop the userControl on the webform.
VaryByParam
Description
none
only GET
*
n versions of page cached based on query string and/or POST body
-can be user control or QueryString
V1
n versions of page cached based on value of V1 variable in query string or POST body
V1;V2
n versions of page cached based on value of V1 and V2 variables in query string or POST body

  • VaryByHeader - maintain separate cache entry for header string changes (UserAgent, UserLanguage, etc.)
  • VaryByControl - for user controls, maintain separate cache entry for properties of a user control
  • VaryByCustom - can specify separate cache entries for browser types and version or provide a custom GetVaryByCustomString method in HttpApplication derived class.
3)Data Caching
  • Store data in cache. Just Query DB 1 time.
  • Cache will be remove when expired, full, dependency object change.
  • Cache can set expired, but Application Cannot.
  • AbsoluteExpiration : when should be remove the cache
  • SlidingExpiration : How long data remain in cache after the data was last accessed.
  • You will get run-time error if sepicy AbsoluteExpiration and SlidingExpiration .
  • CacheItemPirority : If server memory full, cache may removed. So this 1 is to specified the priority of cache. 
  • Add and Insert is same. Different : If Cache already have, add is not work, but insert will overwrite the Cache.
  • CacheItemRemovedCallback :  this delegate use when cached item is removed from cache, we can store the info to DB.
  • Cache Dependency
    • When the file changes, the cache will remove.
    • https://www.youtube.com/watch?v=cB9TlZB3pSw#t=61.538684 (DB)
Different between Cache and Session and Application
  • Cache and Application is per application(Shared all user), Session is per user.
  • Cache can be remove in expire, full or dependency changes, but Session will stay until session end. Application do not time out or file dependency.
  • Cache and Application is keep in process worker, Session can keep in external (state server or SQL server) and apply for Web Farm.

No comments:

Post a Comment