Client Pay Portal

How to Leverage the Kentico Caching API in Your Code

Kentico has numerous APIs to facilitate any functionality that is needed. A savvy developer will know which one to use where to save themselves a lot of work and headache when implementing they custom code. One of the most beneficial APIs I can think of the Caching API.

Under the hood, Kentico caches all kinds of data for various reasons. Being a data driven application, this methodology cuts down on database calls and round trips to deliver information and content quicker. If data isn’t changing, it just makes sense to use something in memory and save the extra processing time required by hitting the database.

The Process

The process that Kentico uses to get cached items is pretty simple.
  • Request comes into the site
  • Kentico checks if it is in cache
  • Kentico validates it if it’s in cache
  • Kentico refreshes the cached value (if needed)
  • Kentico returns the cached data



Implementing the Caching API is one of the easiest ways to get performance enhancements in your code. To implement, decide what type of data your will be caching (string, dataset, etc.). From there, leverage the API to check for the cache value, validate it, and then populate it.
DataSet ds = null;
// Cache the data for 10 minutes with a key
using (CachedSection<DataSet> cs = new CachedSection<DataSet>(ref ds, 10, truenull"myawesomedata"))
    if (cs.LoadData)
        GeneralConnection conn = ConnectionHelper.GetConnection();
        ds = conn.ExecuteQuery("usp_GetSomeAwesomeData", null, QueryTypeEnum.StoredProcedure, false);
        cs.Data = ds; // Save data to cache
    ds = cs.Data;

Let’s look at the code a little closer…
using (CachedSection<DataSet> cs = new CachedSection<DataSet>(ref ds, 10, truenull"myawesomedata"))

This line of code is what checks to see if the data is in cac
he. The second parameter denotes how long you want to store it in cache (in minutes). The last parameter is the cache item name. This must be unique so Kentico can find the value when dealing with it.
if (cs.LoadData)

This line checks if the data in cache is still valid. If “LoadData” returns true, Kentico knows it needs to reload this data. The code in the “if block” goes and gets the data. This can be a database call, a macro, or any other type of lookup you need.
cs.Data = ds; // Save data to cache

This line saves the refreshed data to cache. This will use the settings you defined in the using(CachedSection… code. The type of data is defined in your using statement.

ds = cs.Data;

Finally, we set the returned data to our cache value. This will allow return the valid, cached data from you function.


  • Be careful what you caching. Behind the scenes, you’re saving data to the web server’s memory. If you start loading all kinds of stuff there and leaving it, you can quickly eat up your server’s resources.
  • Think about how often you want to cache your data. Some data makes sense to cache for a day, other data maybe only for 5 minutes.
  • When naming your cache items, you can append in identifiers (Example: Customer ID, etc.) to ensure you have a unique name for each item.
  • You can use the CMS Site Manager / Administration / System / Debug / Cache Items UI to see your new values.


You can find lots of great info on the Kentico APIs here:

That’s it! With that code you can start leveraging the Kentico caching API in your code and save yourself a lot of database trips. Hopefully this blogs helps you see how you can leverage the Kentico APIs to do your bidding. Now go out and start coding something awesome!


Wiz E. Wig, Mascot & Director of Magic
Wiz E. Wig

Director of Magic