Sitecore uses caching to improve system performance and response time. 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.
Sitecore provides a number of caching layers. Each cache serves as a key/value collection.
These caching layers are:
1) HTML Cache
It caches actual HTML output generated from rendering and sublayout.
It can be configured from caching section of rendering controls.
Each website can be configured to enable or disable HTML cache along with allowed HTML cache size in web.config file under <sites> section.
<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" />
2) Item Cache
Sitecore items are cached under this cache layer. This cache contains objects of the Sitecore class Sitecore.Data.Items.Item, which are the one you use in your code most of the time.
Whenever any sitecore item is requested, it will be served from item cache and if it is not there then it will be served from data cache (next layer of cache) and will populate cache in item cache layer. So that whenever next request for same item is made, it will be directly served from item cache.
3) Data Cache
Data cache contains items of the Sitecore class ItemInformation. Whenever any request is made, data is pulled from data cache. But if it is not present there it will be pulled from prefetch cache and then will be put into data cache. So that whenever next request for same item is made, it will be directly served from data cache.
Data cache can be configured in web.config file under Caching.DefaultDataCacheSize setting:
<setting name = "Caching.DefaultDataCacheSize" value="10MB" />
4) Prefetch Cache
There is prefetch cache for each database. The cache contains items of Sitecore class PrefetchData which is same like data pulled from database. Prefetch cache load the cache at Sitecore start up as per specified in App_Config/Prefetch folder.
Below diagram illustrates how all these are actually working.
Managing Cache in Code
Creating new cache
Creating a cache consists of creating a new instance of
Sitecore.Caching.Cache. When the instance is created the cache is automatically registered with 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
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. Reading a value notifies that the value has been read, which resets the expiration timer.
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
var mycache = Sitecore.Caching.CacheManager.FindCacheByName("test cache"); mycache.Remove("name");
Clearing the Cache
A cache can be cleared using the
var mycache = Sitecore.Caching.CacheManager.FindCacheByName("test cache"); mycache.Clear();