Transcript
Page 1: Caching and caching dependencies explained in Kentico CMS

Caching and caching dependencies explainedin Kentico CMS

Boris PocatkoSolution ArchitectKentico Software

[email protected]

Page 2: Caching and caching dependencies explained in Kentico CMS

Agenda

1. Problem

2. Describing different caching mechanisms

3. Demo for cache dependencies

4. API caching and additional info

Page 3: Caching and caching dependencies explained in Kentico CMS

Problem

• Caching is a interesting topic useful for speeding up your pages and understanding all cache types in Kentico can be a challenge.

• Examples

1. What to do if I have dynamic content.

2. What to do with static content

3. How to delete the cache on specific scenarios

4. How to cache data in code

5. Caching in macros

Page 4: Caching and caching dependencies explained in Kentico CMS

Types of caching mechanisms

• Client cache (browser)• IIS cache (Kernel cache blog post)• Output cache (Kentico)• File system cache (Kentico)• Partial cache (Kentico/.NET)• Content cache (Kentico)• Custom code cache (Kentico API)

Page 5: Caching and caching dependencies explained in Kentico CMS

Data retrieval (schema)

Output cache (html)

File system cache (html)

Partial cache (html)

Content cache (data)Custom code cache (data)

Client cache (files)

Code (Page_Load, Init …)

Kernel cache (html)

Serv

erCl

ient

IISKe

ntico

Database

Brow

ser

Request

Server cache (files)

Page 6: Caching and caching dependencies explained in Kentico CMS

Kentico file cache

The system caches files for faster response times (images, JavaScript files, style sheet files and other types of files)

Two types of file caches:

Client-side – stored by the browser on the side of the clientServer-side – stored on the server itself, in it’s memory

Page 7: Caching and caching dependencies explained in Kentico CMS

Kentico client-side cache- uses HTTP request headers to control file caching in client

browsers and intermediate network caches- Is set in SiteManager > Settings > System > Performance > Client

cache (minutes) (this specifies basically, that the client browser doesn’t have to get new content until the given time expires)

- Data saved on the browser- The ideal solution, cache revalidation needs to be implemented- Must revalidate, Etag, Last-Modified, Expires, Cache-Control

values influence how the browser revalidates the content- You can include the Must revalidate attribute with a setting in

SiteManager > Settings > System > Performance > Must revalidate

- The Must revalidate attribute is interpreted by different browsers in different ways (blog)

- The revalidation needs an additional request

Page 8: Caching and caching dependencies explained in Kentico CMS

Kentico client-side files cache

Page 9: Caching and caching dependencies explained in Kentico CMS

Kentico client-side files cache

Page 10: Caching and caching dependencies explained in Kentico CMS

Kentico client-side files cache

Page 11: Caching and caching dependencies explained in Kentico CMS

Kentico client-side files cache

Page 12: Caching and caching dependencies explained in Kentico CMS

IIS Kernel cache

- IIS caching is the second best caching option- The request isn’t processed by the Kentico at all, IIS

is returning the content- Configured on IIS (link)- More details available in our webinar

Page 13: Caching and caching dependencies explained in Kentico CMS

Kentico server-side files cache - The same content is stored as in the client cache- Stores files in the application's memory, so that the CMS doesn't have to access

the database or file system every time a user requests an image or other file.- Site Manager > Settings -> System -> Performance > Cache files

(minutes) setting- The system always caches files for at least one minute to prevent the application

from overloading in case of DoS attacks.- Make sure not too big files are cached, so you don’t run out of memory (set the

Maximum file size to cache setting)- You can redirect files to the disk, if you have the files stored in the file system

(not only in DB), so you allow to process it via IIS (the request uses redirection to the physical file), with this property disabled, they are process directly via our files processing script (if you store files in the file system and don’t have this setting checked, the requests are still processed with our custom script and not with IIS)

Page 14: Caching and caching dependencies explained in Kentico CMS

Kentico cache in general - Every cached item in Kentico (it doesn’t matter if it’s the output cache,

partial, data cache, file system cache) has a key according to which the data is cached and retrieved. This key consists of variables, which can cause different data to be cached.

- Examples of these variables are: - protocol, url, username, sitename, lang, browser, cookielevel,

deviceprofile

- This key is used for retrieval of the cache - If a cache item with such a key doesn’t exist, the output is generated from

scratch

Key Data

Page 15: Caching and caching dependencies explained in Kentico CMS

Kentico output cache - Enabled in Site Manager > Settings > System > Performance > Enable

output caching.- Configured on individual pages (Properties > General > Use output cache

and Cache minutes)- Saves the generated output HTML of whole pages to the memory (by

default)- The most effective way how to speed up your pages- Not usable with pages with very frequently changing content (dynamic

content, such as random products web part), but great for static pages where the content is updated once in a while (e.g. once an hour or day)

- You can use cache dependencies with the Output cache dependencies web part

- You can use substitution macros for displaying dynamic content on a cached page

Page 16: Caching and caching dependencies explained in Kentico CMS

Kentico

Kentico output cache (substitution macros)

Output filter

Output cache (html)

Client

Replace substitution macros

Substitution macros (“{~myexpression~}”) are developed the same way as custom macros, it’s up to you what content is replaced for them

Page 17: Caching and caching dependencies explained in Kentico CMS

Kentico output cache (substitution macros)

Page 18: Caching and caching dependencies explained in Kentico CMS

Kentico file system output cache- Prerequisite: Globally enabled in System > Performance > Output caching > Cache

output in file system (minutes)- Configured additionally individual pages (same place as Output cache)- Saves the output HTML to the file system- Useful during site restarts, when the cache from the server memory isn’t available

Page 19: Caching and caching dependencies explained in Kentico CMS

Kentico partial cache - Partial cache is used when you are not able to use full page (output) caching

(e.g. a lot of dynamic content on your page)- This type of caching can be used to cache the HTML of the page at least

partially- Ideally, resource intensive components should be cached (e.g. repeaters with

nested controls)- We are utilizing the PartialCachingControl .NET object - Set on the web part directly- CMSPartialCacheItems - List of the cache key items for partial caching. Default

value is "username;sitename;lang"- cache dependencies- Editable web parts, have additionally the dependency to the current document- Default cache dependency is based on instance GUID of the web part

Page 20: Caching and caching dependencies explained in Kentico CMS

Kentico partial cache

- By default the cached content is flushed once you change the settings of the web part

Page 21: Caching and caching dependencies explained in Kentico CMS

Kentico cache dependencies - They can be used on different cached objects:

- Full page cache- Partial cache- Custom cache- Content cache- Macro cache

- Cache dependencies are basically dummy cache keys pointing to other cached content

- If the content, which is listed in the dummy key, is changed, the associated cache (e.g. the cache of a web part) is flushed

Page 22: Caching and caching dependencies explained in Kentico CMS

Kentico cache dependencies

Page 23: Caching and caching dependencies explained in Kentico CMS

Kentico cache dependencies - There are two main object types used in cache dependencies:

- Document (TreeNode)- Any object (except document)

How to set up custom table dependencies is described here

Page 24: Caching and caching dependencies explained in Kentico CMS

Kentico content cache - You can set it up on two levels - either on a global level, which influences a

whole site, or you can enable content cache for individual instances of web parts and controls.

- Globally enabled in Site Manager > Settings > System > Performance > Cache content (minutes)

- Cache minutes property on web parts:- You can specify a custom Cache item

name, which will allow you to share the cached data between web parts. It’s used instead of the cache Key string of variables

- Don’t forget that properties can contain macros = dynamic cache item names based on the context (e.g query string values)

Page 25: Caching and caching dependencies explained in Kentico CMS

Kentico content cache

Page 26: Caching and caching dependencies explained in Kentico CMS

Kentico custom code cache

using CMS.SiteProvider;using CMS.GlobalHelper; private void CachingExample(){  DataSet data = null;   // Cache the data for 10 minutes with key "mykey"  using (CachedSection<DataSet> cs = new CachedSection<DataSet>(ref data, 10, true, null, "mykey"))   {      if (cs.LoadData)       {

// Get data from database           data = UserInfoProvider.GetAllUsers();

// Don’t configure cache settings if not necessary (e.g. condition set to false, cache minutes = 0) if (cs.Cached) { 

// Cache dependency            cs.CacheDependency = CacheHelper.GetCacheDependency("node|corporatesite|/news|childnodes");        }

cs.Data = data; // Save data to cache }   }}

Page 27: Caching and caching dependencies explained in Kentico CMS

Kentico custom code cache

protected void CreateTouchableCacheItem_Click(object sender, EventArgs e) { string data = "";

//caching using (CachedSection<String> cs = new CachedSection<String>(ref data, 15, true, null, "MyNewKey")) { if (cs.LoadData) { // Get from database or set it differently (here only assigned for presentation purposes) data = "MyData";

cs.CacheDependency = CacheHelper.GetCacheDependency("touchthis".ToLower()); cs.Data = data; } } }

protected void ToucheItem_Click(object sender, EventArgs e) { CacheHelper.TouchKey("touchthis"); }

Clearing the cache manually (not with help of build in dependencies)

Page 28: Caching and caching dependencies explained in Kentico CMS

Kentico page info cache

- The PageInfo object contains some common properties of documents, which are used quite often (e.g. DocumentName, DocumentID, NodeAliasPath…)

- To reduce overhead these values are cached by default- Kentico sets automatic cache dependencies, so if data related to this

object is updated, the cache is flushed- Contains most of the View_CMS_Tree_Joined properties (Document and

Node fields)- Use it as often as possible (custom code, macros):

- CMS.CMSHelper.CMSContext.CurrentPageInfo.DocumentName

- {% CurrentPageInfo.DocumentName%}

Page 29: Caching and caching dependencies explained in Kentico CMS

Caching in macros

- You can specify caching dependencies, conditional caching and caching time also directly in macros

- {%CMSContext.Current.Documents["/News"].Children.WithAllData.ApplyTransformation("CMS.News.default_macro","","").Cache(10, true, "my_macro_html", "", GetCacheDependency("node|corporatesite|/news|childnodes")) #%}

Page 30: Caching and caching dependencies explained in Kentico CMS

Kentico cache dependencies

DEMO – caching

Page 31: Caching and caching dependencies explained in Kentico CMS

Kentico cache debug

- Cache items (Site Manager > Administration > System > Debug) are listed by default, no additional setup is needed

- For cache access debug you need to enable the setting in Site Manager > Settings > System > Debug

- Use both of them if developing custom controls with caching or custom caching dependencies

- Missing Partial cache debug (we are using the .NET implementation)

Page 32: Caching and caching dependencies explained in Kentico CMS

Additional notes

- The system sometimes caches data separately for each logged-in user. This means that even though some data is cached for one user, when another user requests the same data, the system retrieves the data from the database and stores it in the cache as another cache item.

- Don’t over-cache - Check the Site Manager > Administration > System for general cache

statistics- Cache item name (web part properties), Cache key, CacheItemNameParts

(API) are synonyms

Page 33: Caching and caching dependencies explained in Kentico CMS

Interesting web.config keysCMSAlwaysCacheResources (true) -Enables/disables client caching of (minifiable) resources.Please note that caching on the client side is not done at all if the Client cache (minutes) setting is disabled (set to 0) in Site Manager -> Settings -> System -> Performance.

CMSPhysicalFilesCacheMinutes (10080) - Determines expiration time in minutes that should be set for the physical files in the client cache, e.g. stylesheets not managed via the CMS.

CMSAzureCachePath - Specifies a folder on a local disk where files requested from the storage account will be cached. This helps minimize the amount of blob storage operations, which saves time and resources. Do not use this key if the entire application is deployed as a Windows Azure hosted service.

CMSAllowCacheByCulture (true) - If true, the system adds culture code to the cache keys where needed, if false, it shares the cache for all cultures.

CMSPartialCacheItems ("username;sitename;lang") - Defines the parts of the cache key used for partial cache of the controls. List of items separated by semicolon. Available options: viewmode, username, sitename, lang, browser, domain, gzip

CMSOutputCacheItems (“username;sitename;lang;browser;cookielevel;deviceprofile”) - Defines the parts of the cache key used for output caching. List of items separated by semicolon. Available options: viewmode, username, sitename, lang, browser, domain, gzip

CMSAllowCacheByUserName (true)- If true, the system adds user name to the cache keys where needed, if false, it shares the cache for all users.

CMSClearOutputCacheOnPostback (true) – if output cache is cleared on post back

CMSCacheURLRewriting (0)- Cache url rewriting in minutes

Page 34: Caching and caching dependencies explained in Kentico CMS

Additional resourcesCache dependencies explained:devnet.kentico.com/Blogs/Martin-Hejtmanek/August-2010/Deep-dive--Cache-dependencies.aspx

New caching mechanism (version 5.5+):devnet.kentico.com/Blogs/Martin-Hejtmanek/June-2010/New-in-5-5--Caching-API.aspx

Old webinar about caching: devnet.kentico.com/Blogs/Martin-Hejtmanek/October-2009/Webinar-2---Performance-optimization-and-caching-i.aspx (old API)

Documentation:devnet.kentico.com/docs/devguide/caching_options.htm

Deep dive caching blog post:devnet.kentico.com/Blogs/Martin-Hejtmanek/April-2009/Deep-Dive---Kentico-CMS-Caching.aspx

Blog post about optimization of your code:devnet.kentico.com/Blogs/Martin-Hejtmanek/January-2010/Optimization-tip--Write-better-code,-disable-Outpu.aspx

Cache item name usage:devnet.kentico.com/Blogs/Martin-Hejtmanek/December-2009/Tip--Is-your-menu-and-controls-cache-settings-effi.aspx

API caching examples:http://devnet.kentico.com/Knowledge-Base/API-and-Internals/Caching-API-examples.aspx

Page 35: Caching and caching dependencies explained in Kentico CMS

?Questions & Answers

Page 36: Caching and caching dependencies explained in Kentico CMS

Thank you!


Recommended