Custom Caching Example

IT Dept asked on September 8, 2015 15:04

I want to store a cache of products in memory due to the way I want to retrieve this data later. I have had a look at various documentation but I am still at a loss on the best way to implement this.

I have looked at the Dev docs on this,, and made this method to get the data back. I have also read:

but I have taken most of what I need from this article:

which seems a lot clearer than the others.

Currently I have a separate class called Products where I have:

class Products
    public List<Product> ProductsCache()
        List<Product> products = null;

        using (CachedSection<List<Product>> cs = new CachedSection<List<Product>>(ref products, 10, true, null, "productsKey"))
            if (cs.LoadData)
                products = GetProducts();
                cs.CacheDependency = CacheHelper.GetCacheDependencies("products");
                cs.Data = products;

        return products;

    private List<Product> GetProducts()
        List<Product> result = new List<Product>();

        var data = SKUInfoProvider.GetSKUList("", "", "", -1);

        if (data != null)
            foreach (DataRow item in data.Tables[0].Rows)
                result.Add(new Product()
                    sku = item["skunumber"].ToString(),
                    quantity = item["SKUAvailableItems"].ToString()

        return result;

    public class Product
        public string sku { get; set; }
        public string masterSku { get; set; }
        public string name { get; set; }
        public string description { get; set; }
        public string price { get; set; }
        public string rrp { get; set; }
        public string quantity { get; set; }
        public string department { get; set; }
        public string dimensions { get; set; }
        public string size { get; set; }
        public string colour { get; set; }
        public string materials { get; set; }
        public string brand { get; set; }
        public List<PersOption> persOptions { get; set; }
        public List<Uri> imageURLs { get; set; }

    public class PersOption
        public string Name { get; set; }
        public string DataType { get; set; }
        public int MaxLength { get; set; }

as you can see I am currently just accessing some of the sku data but I am going to hook this up to a view later on. What I want to know is how to access the cache. Do I just need to put in a line like:

var products = CacheUtility.GetCached(ProductsCache, "productsKey");

Correct Answer

Dawid Jachnik answered on September 8, 2015 15:37

Hello Chris,

You just need simple type:

var products = Products.ProductsCache();
2 votesVote for this answer Unmark Correct answer

Recent Answers

Petar Kozjak answered on September 8, 2015 15:51

When the "ProductsCache()" method is called and the cached section is entered, Kentico checks if the cache expiration time has passed (10 minutes in this case).

If the cache has expired the cs.LoadData Boolean will be set to true and the if block will execute, filling the new data into the cache.

If the cache is not expired the cached products will be automatically retrieved into the referenced product variable.

So in conclusion, there is no need to use any special syntax to retrieve data from the cache, the kentico CachedSection will take care of that automatically when you call the ProductsCache method.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.