Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Genc Kastrati 90 posts 415 karma points
    Feb 28, 2017 @ 17:40
    Genc Kastrati
    0

    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?

    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;
        }
    

    Thank you!!! Genc

  • Stefano 61 posts 313 karma points c-trib
    Feb 28, 2017 @ 19:02
    Stefano
    0

    Have you tried setting a default controller with OutputCache attribute set on it, or using @Html.CachedPartial?

  • Genc Kastrati 90 posts 415 karma points
    Feb 28, 2017 @ 19:07
    Genc Kastrati
    0

    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!

  • Stefano 61 posts 313 karma points c-trib
    Feb 28, 2017 @ 19:08
    Stefano
    0

    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! :)

  • Genc Kastrati 90 posts 415 karma points
    Mar 02, 2017 @ 17:00
    Genc Kastrati
    0

    Thank you Stefano! Are you using it in directly on the controller page with: @Response.OutputCache(3600, true);

    Merci! g

  • Stefano 61 posts 313 karma points c-trib
    Mar 02, 2017 @ 18:57
    Stefano
    0

    No, I'm setting up a default controller in Umbraco and using the C# attribute!

  • Stefano 61 posts 313 karma points c-trib
    Mar 02, 2017 @ 18:59
    Stefano
    101
    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);
        }
    }
    
  • Genc Kastrati 90 posts 415 karma points
    Mar 03, 2017 @ 09:20
    Genc Kastrati
    0

    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!

  • Stefano 61 posts 313 karma points c-trib
    Mar 03, 2017 @ 09:22
    Stefano
    0

    Glad it helped. That beauty dropped the time to first byte on one of our websites on Azure from 5s to 25ms :D

  • Stefano 61 posts 313 karma points c-trib
    Mar 05, 2017 @ 10:00
    Stefano
    0

    Could you mark the post as solution if that's what you were looking for please?

  • Genc Kastrati 90 posts 415 karma points
    Mar 06, 2017 @ 08:24
    Genc Kastrati
    0

    yes or course! sorry forgot :)

  • Per Bolmstedt 84 posts 380 karma points
    Dec 17, 2020 @ 13:56
    Per Bolmstedt
    0

    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

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies