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, http://devnet.kentico.com/docs/7_0/devguide/index.html, and made this method to get the data back. I have also read:
http://devnet.kentico.com/articles/deep-dive-kentico-cms-caching
http://devnet.kentico.com/articles/deep-dive--cache-dependencies
but I have taken most of what I need from this article:
http://www.kenticosolutions.com/Developer-Tips/Tip/August-2011/Caching-in-with-Kentico.aspx
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");