Caching API examples

   —   
This article gives you two examples how to cache data and how to flush them.
There are two ways to flush the cache. The first one is to create a cache item and clear the cache by touching the dummy key directly. At first you need to create a data object which is stored in the cache and a valid cache dependency. Please be aware that the cache keys are always lower case. You can always include a “.ToLower()” cast to your strings to be sure that you don’t make any mistakes on this rule. You can copy and paste the code below into your code file to get a working example:

    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 TouchItem_Click(object sender, EventArgs e)
    {
        CacheHelper.TouchKey("touchthis");
    }


To run the preceding code, please paste the code below into the ascx (or aspx) file to create buttons which will run the mentioned code:

<asp:UpdatePanel>
<asp:Button ID="CreateTouchableCacheItem" runat="server" Text="Create touchable cache item" onclick="CreateTouchableCacheItem_Click" />
</asp:UpdatePanel>

<asp:UpdatePanel>
<asp:Button ID="TouchItem" runat="server" onclick="TouchItem_Click" Text="Touche the cached data" />
</asp:UpdatePanel>


Now you can try to create your cached item and clear it from cache by clicking the created buttons. To check how the cache behaves, please use our cache debug. You can enable it by adding the below keys into the web.config file:

<add key="CMSDebugCache" value="true"/>
<add key="CMSDebugCacheLive" value="true"/>
<add key="CMSDebugAllCaches" value="true"/>
<add key="CMSDebugCacheLogLength" value="10"/>
<add key="CMSLogCache" value="true" />


Now you can see the cached data in CMSSiteManager -> Administration -> System -> Debug or on the live page. The second example creates a cached object which is flushed after a node is changed in the content tree of your site. You can see that the code is very similar. The only thing you need to change is the cache dependency.  Lets say that the cache item needs to be flushed after the node with the node alias path „/News“ is updated on the site. The code name of the site in this example is „corporatesite“. In this case the cache dependency key will look something like this:

node|corporatesite|/news

The complete code of the code behind file is below. You can copy and paste it the same way as the code before.

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

        //caching
        using (CachedSection<String> cs = new CachedSection<String>(ref data, 15, true, null, "MyNewKeyForFlushingOnUpdate"))
        {
            if (cs.LoadData)
            {
                // Get from database or set it differently
                data = "MyDataForFlushingOnUpdate";

                cs.CacheDependency = CacheHelper.GetCacheDependency("node|corporatesite|/news".ToLower());               
                cs.Data = data;
            }
        }
    }


To run the above code please add the following code into the design file of the above code file:

<asp:UpdatePanel>
<asp:Button ID="CreateFlushableItemByUpdate" runat="server" onclick="CreateFlushableItemByUpdate_Click" Text="Add item to cache which will be cleared by node update" />
</asp:UpdatePanel>


Now run this code by clicking the newly created button. By checking the cache you will see that the object is added to cache every time you click the button. Both examples are set to hold the cached item for 15 minutes. You can play around with those examples and change the cache dependencies to fit your project. For more advanced cache dependencies and information on the debug mode in Kentico please check the links below.
-bp-


See also: Cache dependencies deep dive
Cache debugging
CAching in custom code

Applies to: Kentico CMS 5.5 and later
Share this article on   LinkedIn

Juraj Ondrus

Hi, I am the Technical support leader at Kentico. I'm here to help you use Kentico and get as much as possible out of it.