/// <summary>
/// Returns Category object from cache. If category does not already exist in cache, it
/// will be read and added to cache
/// </summary>
/// <param name="id">The id of the Category to read</param>
/// <returns></returns>
public static CategoryCache Read(int id)
{
string cacheKey = CategoryCache.CacheKey(id);
ObjectCache cache = MemoryCache.Default;
CategoryCache category = cache[cacheKey] as CategoryCache;
if (category != null)
{
return(category);
}
else
{
var categoryService = new Rock.Model.CategoryService();
var categoryModel = categoryService.Get(id);
if (categoryModel != null)
{
category = new CategoryCache(categoryModel);
var cachePolicy = new CacheItemPolicy();
cache.Set(cacheKey, category, cachePolicy);
cache.Set(category.Guid.ToString(), category.Id, cachePolicy);
return(category);
}
else
{
return(null);
}
}
}