Kentico CMS 6.0 Developer's Guide

Macro result caching

Macro result caching

Previous topic Next topic Mail us feedback on this topic!  

Macro result caching

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

It is possible to cache macro results so that certain expressions don't have to be evaluated repeatedly. This is possible using the Cache method. The method has the following signature:

 

Object Cache(Object expression, Int32 cacheMinutes, Boolean condition, String cacheItemName, String cacheItemNameParts, CMSCacheDependency cacheDependency )

 

The method is overloaded, while only the first parameter is mandatory — the rest of the parameters is optional. The parameters determine the following:

 

string macroExpression: Macro expression (without encapsulating character sequences, e.g. {% %}) to be evaluated and cached.

Int32 cacheMinutes: Number of minutes for which the value will be cached.

Boolean condition: Boolean expression whose value determines if the macro expression should be cached.

String cacheItemName: Name of the cache item under which the value will be stored in the cache.

String cacheItemNameParts: Any number of parameters whose values will be concatenated (together with the cacheItemName parameter value) and used as the name of the cache item.

CMSCacheDependency cacheDependency: Cache dependency (use the GetCacheDependency method to get the object).

 

The following example demonstrates how it can be used to cache a simple "test_string".ToUpper() expression.

 

{% Cache("test_string".ToUpper()) %}

 

When the macro is first evaluated, it is resolved as TEST_STRING and the result is stored in cache. In the screenshot below, you can see the respective cache access debug record for this operation.

 

devguide_clip1784

 

On each subsequent resolving, the expression is not resolved again and its value is taken from cache.

 

devguide_clip1785

 

The following example shows how the method can be used with the optional parameters to specify the cache item name, time period for which the value will be cached and a cache dependency.

 

{% Cache("test_string".ToUpper(), 5, true, "mykey", GetCacheDependency("mydependency")) %}

 

In this case, the cache access debug record looks as in the following screenshot.

 

devguide_clip1786