Caching and caching dependencies explained in Kentico CMS

of 36 /36
Caching and caching dependencies explained in Kentico CMS Boris Pocatko Solution Architect Kentico Software [email protected]

Embed Size (px)

description

Caching and caching dependencies explained in Kentico CMS. Boris Pocatko Solution Architect Kentico Software [email protected] Agenda. Problem Describing different caching mechanisms Demo for cache dependencies API caching and additional info. Problem. - PowerPoint PPT Presentation

Transcript of Caching and caching dependencies explained in Kentico CMS

Main Title here

Caching and caching dependencies explainedin Kentico CMSBoris PocatkoSolution ArchitectKentico Software

[email protected]

AgendaProblem

Describing different caching mechanisms

Demo for cache dependencies

API caching and additional info

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

Examples

What to do if I have dynamic content.

What to do with static content

How to delete the cache on specific scenarios

How to cache data in code

Caching in macros

Types of caching mechanismsClient 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)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)ServerClientIISKenticoDatabaseBrowserRequestServer cache (files)5

Kentico file cacheThe 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 its memory

Kentico client-side cacheuses HTTP request headers to control file caching in client browsers and intermediate network cachesIs set in SiteManager > Settings > System > Performance > Client cache (minutes)(this specifies basically, that the client browser doesnt have to get new content until the given time expires)Data saved on the browserThe ideal solution, cache revalidation needs to be implementedMust revalidate, Etag, Last-Modified, Expires, Cache-Control values influence how the browser revalidates the contentYou 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

Kentico client-side files cache

Kentico client-side files cache

Kentico client-side files cache

Kentico client-side files cache

IIS Kernel cache IIS caching is the second best caching optionThe request isnt processed by the Kentico at all, IIS is returning the contentConfigured on IIS (link)More details available in our webinar

12

Kentico server-side files cache The same content is stored as in the client cacheStores 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)settingThe system always caches files for at least one minute to prevent the application from overloading in case ofDoS attacks.Make sure not too big files are cached, so you dont 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 dont have this setting checked, the requests are still processed with our custom script and not with IIS)

13

Kentico cache in general Every cached item in Kentico (it doesnt matter if its 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 doesnt exist, the output is generated from scratch

KeyData14

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 pagesNot 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 partYou can use substitution macros for displaying dynamic content on a cached page

15

KenticoKentico output cache (substitution macros) Output filterOutput cache (html)ClientReplace substitution macrosSubstitution macros ({~myexpression~}) are developed the same way as custom macros, its up to you what content is replaced for them 16

Kentico output cache (substitution macros)

17

Kentico file system output cachePrerequisite: 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 systemUseful during site restarts, when the cache from the server memory isnt available

18

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 partiallyIdeally, resource intensive components should be cached (e.g. repeaters with nested controls)We are utilizing the PartialCachingControl.NET object Set on the web part directlyCMSPartialCacheItems - List of the cache key items for partial caching. Default value is "username;sitename;lang"cache dependenciesEditable web parts, have additionally the dependency to the current documentDefault cache dependency is based on instance GUID of the web part

19

Kentico partial cache

- By default the cached content is flushed once you change the settings of the web part20Kentico cache dependencies They can be used on different cached objects:Full page cachePartial cacheCustom cacheContent cacheMacro cacheCache dependencies are basically dummy cache keys pointing to other cached contentIf the content, which is listed in the dummy key, is changed, the associated cache (e.g. the cache of a web part) is flushed

21

Kentico cache dependencies

22Kentico 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 here23

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 minutesproperty on web parts:You can specify a custom Cache item name, which will allow you to share the cached data between web parts. Its used instead of the cache Key string of variablesDont forget that properties can contain macros = dynamic cache item names based on the context (e.g query string values)

24Kentico content cache

25

Kentico custom code cache usingCMS.SiteProvider;usingCMS.GlobalHelper;privatevoidCachingExample(){DataSetdata =null;// Cache the data for 10 minutes with key "mykey"using(CachedSection cs =newCachedSection(refdata, 10,true,null,"mykey")) { if(cs.LoadData) {// Get data from database data =UserInfoProvider.GetAllUsers();

// Dont 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 } }}26

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

//caching using (CachedSection cs = new CachedSection(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)27

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 defaultKentico sets automatic cache dependencies, so if data related to this object is updated, the cache is flushedContains most of the View_CMS_Tree_Joinedproperties (Document and Node fields)Use it as often as possible (custom code, macros):

CMS.CMSHelper.CMSContext.CurrentPageInfo.DocumentName {% CurrentPageInfo.DocumentName%}28Caching in macrosYou 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")) #%}

29Kentico cache dependencies DEMO caching

30Kentico cache debug Cache items(Site Manager > Administration > System > Debug) are listed by default, no additional setup is neededFor cache access debug you need to enable the setting in Site Manager > Settings > System > DebugUse both of them if developing custom controls with caching or custom caching dependenciesMissing Partial cache debug (we are using the .NET implementation)

31Additional 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.Dont over-cache Check the Site Manager > Administration > System for general cache statisticsCache item name (web part properties), Cache key, CacheItemNameParts (API) are synonyms

32Interesting 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 theClient cache (minutes)setting is disabled (set to0) inSite 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 33Additional 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?Questions & AnswersThank you!