1 / 36

Caching and caching dependencies explained in Kentico CMS

Caching and caching dependencies explained in Kentico CMS. Boris Pocatko Solution Architect Kentico Software borisp@kentico.com. Agenda. Problem Describing different caching mechanisms Demo for cache dependencies API caching and additional info. Problem.

talmai
Download Presentation

Caching and caching dependencies explained in Kentico CMS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Caching and caching dependencies explainedin Kentico CMS Boris Pocatko Solution Architect Kentico Software borisp@kentico.com

  2. Agenda • Problem • Describing different caching mechanisms • Demo for cache dependencies • API caching and additional info

  3. Problem • Caching 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

  4. 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)

  5. Data retrieval (schema) Request Browser Client cache (files) Client Kernel cache (html) IIS Server cache (files) Output cache (html) File system cache (html) Server Partial cache (html) Kentico Content cache (data) Custom code cache (data) Code (Page_Load, Init…) Database

  6. 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 client Server-side – stored on the server itself, in it’s memory

  7. 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

  8. Kentico client-side files cache

  9. Kentico client-side files cache

  10. Kentico client-side files cache

  11. Kentico client-side files cache

  12. 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

  13. 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)

  14. 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 Data Key

  15. 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

  16. Kentico output cache (substitution macros) Client Substitution macros (“{~myexpression~}”) are developed the same way as custom macros, it’s up to you what content is replaced for them Kentico Output filter Replace substitution macros Output cache (html)

  17. Kentico output cache (substitution macros)

  18. 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

  19. 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

  20. Kentico partial cache - By default the cached content is flushed once you change the settings of the web part

  21. 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

  22. Kentico cache dependencies

  23. 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

  24. 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. 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)

  25. Kentico content cache

  26. Kentico custom code cache using CMS.SiteProvider; using CMS.GlobalHelper; privatevoid CachingExample() { DataSet data = null; // Cache the data for 10 minutes with key "mykey" using (CachedSection<DataSet> cs = newCachedSection<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 }    } }

  27. Kentico custom code cache Clearing the cache manually (not with help of build in dependencies) protectedvoidCreateTouchableCacheItem_Click(object sender, EventArgs e) { string data = ""; //caching using (CachedSection<String> cs = newCachedSection<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; } } } protectedvoidToucheItem_Click(objectsender, EventArgs e) { CacheHelper.TouchKey("touchthis"); }

  28. 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_Joinedproperties (Document and Node fields) • Use it as often as possible (custom code, macros): • CMS.CMSHelper.CMSContext.CurrentPageInfo.DocumentName • {% CurrentPageInfo.DocumentName%}

  29. 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"))#%}

  30. Kentico cache dependencies DEMO – caching

  31. 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)

  32. 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

  33. Interesting web.config keys CMSAlwaysCacheResources(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

  34. Additional resources Cache 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

  35. Questions & Answers ?

  36. Thank you!

More Related