Caching and caching dependencies explained - PowerPoint Presentation

Download presentation
Caching and caching dependencies explained
Caching and caching dependencies explained

Embed / Share - Caching and caching dependencies explained


Presentation on theme: "Caching and caching dependencies explained"— Presentation transcript


Slide1

Caching and caching dependencies explainedin Kentico CMS

Boris PocatkoSolution ArchitectKentico Softwareborisp@kentico.comSlide2

Agenda

ProblemDescribing different caching mechanismsDemo for cache dependenciesAPI caching and additional infoSlide3

Problem

Caching is a interesting topic useful for speeding up your pages and understanding all cache types in Kentico can be a challenge. ExamplesWhat 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 macrosSlide4

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)Slide5

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)

Server

Client

IIS

Kentico

Database

Browser

Request

Server cache (files)Slide6

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 Slide7

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 Slide8

Kentico client-side files cacheSlide9

Kentico client-side files cacheSlide10

Kentico client-side files cacheSlide11

Kentico client-side files cacheSlide12

IIS Kernel cache

IIS caching is the second best caching optionThe request isn’t processed by the Kentico at all, IIS is returning the contentConfigured on IIS (link)More details available in our

webinarSlide13

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)Slide14

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:

p

rotocol,

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

DataSlide15

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 pageSlide16

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 Slide17

Kentico output cache (substitution macros) Slide18

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 availableSlide19

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

Kentico partial cache

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

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 flushedSlide22

Kentico cache dependencies Slide23

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

hereSlide24

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 variablesDon’t forget that properties can contain macros = dynamic cache item names based on the context (e.g query string values)Slide25

Kentico content cache Slide26

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

}

   }

}Slide27

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)Slide28

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%}Slide29

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

Kentico cache dependencies

DEMO – cachingSlide31

Kentico 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 dependencies

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

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 synonymsSlide33

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 backCMSCacheURLRewriting (0)- Cache url rewriting in minutes Slide34

Additional resources

Cache dependencies explained:devnet.kentico.com/Blogs/Martin-Hejtmanek/August-2010/Deep-dive--Cache-dependencies.aspxNew 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.aspxSlide35

?

Questions & AnswersSlide36

Thank you!

By: cheryl-pisano
Views: 92
Type: Public

Caching and caching dependencies explained - Description


in Kentico CMS Boris Pocatko Solution Architect Kentico Software borispkenticocom Agenda Problem Describing different caching mechanisms Demo for cache dependencies API caching and additional info ID: 232469 Download Presentation

Related Documents