Saturday, November 24, 2018

Sitecore Caching


Hi All, In this post we will see about caching mechanism in Sitecore.

Here you go,

  • Sitecore uses various caches to store data, rendered presentation logic and other information in memory in order to improve performance and response time 
  • A cache is a dictionary that keeps track of when each entry is accessed. The data that is stored for each entry depends on the cache.  

Types of cache


                                1. Prefetch cache
                                2. Data cache
                                3. Item cache
                                4. Html or web cache.

Prefetch Cache 


  • There is a prefetch cache for each database.
  • Items are pulled out from database when application starts.
  • Items which should be loaded at startup are specified in App_Config/prefetch folder.
  • Each prefetch entry represents item in a database,this entry includes all field values for all versions including parent-child relationships.
  • Excessive use of prefetch caches can affect the time required for application initialization.

Data Cache 

  • Purpose of this cache is to minimize the amount of requests to the database.
  • There is also data cache for each database.
  • You can specify the data cache in web config

    < setting name = "Caching.DefaultDataCacheSize" value="10MB" />

or you can set as per Database

    < cacheSizes hint = "setting">
        < data > 20MB</data>
        < items > 10MB</items>
        < paths > 500KB</paths>
        < standardValues > 500KB</standardValues>
    </ cacheSizes >

Item Cache 

  •   Like prefetch and data cache,there is an item cache for each database.
  •   This cache has objects of Sitecore.Data.Items.Item

HTML Cache or Web Cache 

  •   Caches the actual HTML generated from renderings and layouts.
  •   You can set HTML Cache per presentation control.
  •   You can set html size and weather it should be enabled for your sites-it all done in the site section of web config

< site name = "website" virtualFolder="/" physicalFolder="/"rootPath="/sitecore/content " startItem="/Home" database="web"domain="extranet" cacheHtml="true" htmlCacheSize="10MB"enablePreview="true" enableWebEdit="true"enableDebugger="true" disableClientData="false" />


    
  Types: 

      1.Vary by Data

  • Sitecore caches the output based on the item accessed.
  • When the same item  is accessed for the second time, the HTML will be loaded from the cache  Typically used for headers and footers. 

  2.Vary by Device 

  • Sitecore caches copies of the output for each Device being used

3.Vary by Param

  • Sitecore caches the output based on rendering parameters passed to the presentation component.

4.Vary by QueryString

  • The VaryByQueryString property controls whether or not output caching varies based on query string parameters passed in the URL.
  • The VaryByParm property causes output caching to vary based on rendering parameter values passed by the developer. The VaryByQueryString property causes output caching to vary based on parameters passed in the URL query string.

5.Vary by User

  • The VaryByUser property controls whether or not output caching varies by the domain and username of the context user. Used with personalization enabled on websites.

6.Vary By Login

  • Controls controls whether or not output caching varies by authenticated user.

To Monitor your caches

              http://< your domain >/Sitecore/admin/cache.aspx 

Flow of Cache in Sitecore


  

Creating the Cache

Creating a cache consists of creating a new instance of Sitecore.Caching.Cache. When the instance is created, the cache is automatically registered within the system. This means that Sitecore recognizes the cache and can control it.
  var mycache = new Sitecore.Caching.Cache("test cache",1024); 

Referencing the Cache

A cache is accessed by name using the Cache Manager.
      var mycache = Sitecore.Caching.CacheManager.FindCacheByName("test cache");

Adding a Value to the Cache
A value is added to the cache using the Add method. 
var mycache =Sitecore.Caching.CacheManager.FindCacheByName("test cache");
mycache.Add("name","value");

Reading a Value from the Cache
A value is read from the cache using the GetValue method.  
var mycache = Sitecore.Caching.CacheManager.FindCacheByName("test cache");
var value =mycache.GetValue("name");

Removing a Value from the Cache
A value is removed from the cache using the Remove method. 
var mycache = Sitecore.Caching.CacheManager.FindCacheByName("test cache");
mycache.Remove("name");

Clearing the Cache
A cache can be cleared using the Clear method. 
var mycache =Sitecore.Caching.CacheManager.FindCacheByName("test cache");
mycache.Clear();


How cache clearing works?
HTML cache
On publishing of any item, HTML cache is cleared.
Item cache
Whenever any item is published, its Item Cache is also updated. If you publish an item which is linked to other items, then these items are cleared as well.

If you publish standard values or a template, it will clear all items based on that template.
Data cache
Data cache is updated incrementally when changes take effect after a publish. It is rebuild incrementally when the items are requested again.
Prefetch cache
Is cleared in the same way as the data cache, but the items specified in the config files are prefetched from the database.


That's all. I Hope now you have a clear view about how caching works in sitecore. Please share this with your friends. Happy Coding :)

No comments:

Post a Comment

Check the users Logged into the sitecore backend

Hi, Today I'm going to discuss about " how to check the concurrent users logged into the sitecore backend ? ". For this ...