Copied to clipboard

Flag this post as spam?

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


  • Emil Lindgren 7 posts 97 karma points
    6 days ago
    Emil Lindgren
    0

    Hello, so i have a problem i need to understand better.

    I need to cache my side navigation. I have it in a partial view, so i thought i could use the @html.CachePartial(), but im new to all this caching, so i dont know really how to cache it, i might be able to make my own cache method but still, might not be the best/simplest. This is my cache code, @Html.CachedPartial("~/Views/Partials/Navigation/SideNavigation.cshtml", Model, 3600, true)

    but could someone explain where or how to use it? I've read this article

    https://our.umbraco.com/documentation/Reference/Templating/Mvc/partial-views

  • Louis Jackson-Rees 41 posts 167 karma points
    6 days ago
    Louis Jackson-Rees
    100

    Hi Emil,

    So they way you are using it there is slightly incorrect for the particular use case of a navigation,

    @Html.CachedPartial("~/Views/Partials/Navigation/SideNavigation.cshtml", Model, 3600, true)
    

    the last parameter here is saying to cache by page, meaning that instead of caching the content of the partial for the entire site, it is caching it for only that page (this is great for pages where you want to cache content managed content that is different between pages).

    So this code should work for your navigation:

    @Html.CachedPartial("~/Views/Partials/Navigation/SideNavigation.cshtml", Model, 3600)
    

    So this will cache the content of your nav partial and then any page that is loaded within 1hour (3600 seconds / 60), will get served the nav partial with the data from the cache, after that the cache will be renewed and then that content will get served for another 60mins, so on and so on,

    So a good way to test this would be to use the code i have supplied above, view the page and see the navigation and then in the cms, add a navigation item and see that the nav doesn't update until an hour later when the cache gets renewed.

    NOTE:

    There are no additional steps you need to take simply calling that line of code will handle all of the caching for you

  • Emil Lindgren 7 posts 97 karma points
    5 days ago
    Emil Lindgren
    0

    Hello Louis, the caching now works, i tested it like you said, I made a new item in the nav, and when the cache renewed it was there, and i tested to see if the nav was there before the hour too, just in case.

    But i have a question then, im using the nav in different places with different models, arent that gonna effect the output of that? i read that if you have different models, then the nav will just take the first model of where the the cached nav is (and the rest gets the same)?

    So is this going to work if i go to for example, Vehicle -> Cars -> Doors, and when it goes to doors page it shows where in the nav i am. In the Nav on that page is the Vehicle -> Cars -> Doors.

  • Louis Jackson-Rees 41 posts 167 karma points
    5 days ago
    Louis Jackson-Rees
    0

    This will ultimately depend on how you are retrieving your nav partial, could you provide the code?

  • Emil Lindgren 7 posts 97 karma points
    5 days ago
    Emil Lindgren
    0

    Im doing is like ->

    @Html.CachedPartial("~/Views/Partials/Navigation/SideNavigation.cshtml", Model, 3600)
    

    shouldnt i use the cachesPartial where i want the nav to be ?

    but i dont think this is right, cus its calling the same state every time ?

  • Louis Jackson-Rees 41 posts 167 karma points
    5 days ago
    Louis Jackson-Rees
    0

    I meant the code within that partial, it is correct im not sure what you mean by 'state'?

  • Emil Lindgren 7 posts 97 karma points
    5 days ago
    Emil Lindgren
    0

    My bad,

    This is what im using

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    @using Website.Models;
    @{
        Layout = null;
        //var backgroundImage = Model.Content.HeroImage != null ? Model.Content.HeroImage.Url : String.Empty;
        var categories = Umbraco.Content(1270);
    }
    
    
    
    
    <nav class="productNav" id="productNav">
        <a href="@categories.Url" class="productNav__overview">
            <div class="productNav__overview-smallbox" data-toggle="collapse" data-target="#navigation-0" aria-expanded="true" aria-controls="navigation-0">
                <div class="icon__block icon__block-active"></div>
                <div class="product__overview-smallbox--text">Products Overview</div>
            </div>
            <div id="navigation-0" class="collapse show" data-parent="#productNav"></div>
        </a>
        <div class="productNav__accordion accordion">
            @{
                var count = 1;
                foreach (var category in categories.Children())
                {
                    <div class="productNav__accordion-smallbox">
                        <a href="@category.Url" class="productNav__accordion-smallbox--textbox">
                            <div class="icon__block "></div>
                            <div class="productNav__accordion-smallbox--textbox--text">@category.Name</div>
                        </a>
                        <div class="icon__plus collapsed" data-toggle="collapse" data-target="#navigation-@count" aria-expanded="true" aria-controls="navigation-@count">
                            <div class="icon__plus-one"></div>
                            <div class="icon__plus-two"></div>
                        </div>
                    </div>
                    <div class="productNav__accordion-bigbox collapse" id="navigation-@count" class="collapse show" data-parent="#productNav">
    
                        @foreach (var childs in category.Children())
                        {
                        <a href="@childs.Url" class="productNav__accordion-bigbox--itembox" >
                            <div class="productNav__accordion-bigbox--itembox--icon icon__block"></div>
                            <div class="productNav__accordion-bigbox--itembox--item">@childs.Name</div>
                        </a>
                        }
                    </div>
                    count++;
                }
            }
        </div>
    
        </nav>
    

    King regards.

Please Sign in or register to post replies

Write your reply to:

Draft