I am trying to introduce caching at a few places where the site is being slow, mostly from all the background processing. I am wondering if anyone can look at this code to tell me if I am doing it right or not?
public static List<ContentItems> getChildren(string language, string URL, int Id, string order)
{
List<ContentItems> children = new List<ContentItems>();
var cache = ApplicationContext.Current.ApplicationCache.RuntimeCache;
if (cache.GetCacheItem("TGFCache_GetChildren_" + Id + "-" + language + "-" + order) == null)
{
var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
IPublishedContent currentPage = umbracoHelper.TypedContent(Id);
IEnumerable<IPublishedContent> childrenList = currentPage.Children().Where("Visible").Where(x => x.Name != "0").OrderBy("Name " + order);
try
{
foreach (IPublishedContent child in childrenList)
{
ContentItems item = new ContentItems();
item = getTranslatedSet(language, child.Id);
children.Add(item);
}
}
catch { }
cache.InsertCacheItem("TGFCache_GetChildren_" + Id + "-" + language + "-" + order, () => children, timeout: TimeSpan.FromHours(24));
}
children = (List<ContentItems>)cache.GetCacheItem("TGFCache_GetChildren_" + Id + "-" + language + "-" + order);
return children;
}
Hi, I suggest you abuse the outputCache on a default controller, it works so well unless you have complicated requirements that it's not worth messing with anything else! :)
public class Startup : IApplicationEventHandler
{
public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication,
ApplicationContext applicationContext)
{
//RouteTable.Routes.MapRoute(
// "Confirmation",
// "Booking/Confirmation",
// new
// {
// controller = "Booking",
// action = "Confirmation",
// });
}
public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication,
ApplicationContext applicationContext)
{
DefaultRenderMvcControllerResolver.Current.SetDefaultControllerType(
typeof(CachedController));
}
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication,
ApplicationContext applicationContext)
{
}
and
/// <summary>
/// Cache requests with the "default" profile, manipulated with web tranforms.
/// HTTP POST are not affected (UmbracoForms works fine).
/// </summary>
public class CachedController : Umbraco.Web.Mvc.RenderMvcController
{
[OutputCache(CacheProfile = "default")]
public override ActionResult Index(RenderModel model)
{
return base.Index(model);
}
}
Question: Am I using Cache properly?
Dear all,
I am trying to introduce caching at a few places where the site is being slow, mostly from all the background processing. I am wondering if anyone can look at this code to tell me if I am doing it right or not?
Thank you!!! Genc
Have you tried setting a default controller with
OutputCache
attribute set on it, or using@Html.CachedPartial
?Hi Stefano, I completely forgot OutputCache. Will give it a try.
The above has been working really well. It isn't timing out in certain parts.
Thank you, I will try next with OutputCache too!
Hi, I suggest you abuse the outputCache on a default controller, it works so well unless you have complicated requirements that it's not worth messing with anything else! :)
Thank you Stefano! Are you using it in directly on the controller page with: @Response.OutputCache(3600, true);
Merci! g
No, I'm setting up a default controller in Umbraco and using the C# attribute!
and
Thank you Stefano! This is very useful. I've seen bits and pieces here and there but now it is a lot clearer to me. Thank you :) Grazie!
Glad it helped. That beauty dropped the time to first byte on one of our websites on Azure from 5s to 25ms :D
Could you mark the post as solution if that's what you were looking for please?
yes or course! sorry forgot :)
I can't get this exact thing to work, setting a default controller and enabling Output Cache for the Index method. Am I doing something wrong?
https://our.umbraco.com/forum/using-umbraco-and-getting-started/104538-output-caching-defaultrendermvccontroller
is working on a reply...