Copied to clipboard

Flag this post as spam?

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


  • Phil Veloso 36 posts 248 karma points
    Sep 18, 2017 @ 12:17
    Phil Veloso
    0

    CachedPartial - How to force or auto refresh cache

    Hiya,

    Is there a way to force refresh all or an individual cached partial?

    For example, if I have updated the template and want to push these updates halfway through the caching period.

    Additionally, at the end of [this thread][1] it mentions that a scheduled task could be used to auto-refresh a cached page.

    I wanted to know whether it would be possible to write a page script embedded in a template which automatically launched every node or a specific document type and how such a script might look like.

    Many thanks, Phil

  • Alex Skrypnyk 5441 posts 20364 karma points MVP 3x admin c-trib
    Sep 18, 2017 @ 14:52
    Alex Skrypnyk
    0

    Hi Phil

    Please, have a loot source code of cached partial extension method:

    https://github.com/umbraco/Umbraco-CMS/blob/5397f2c53acbdeb0805e1fe39fda938f571d295a/src/Umbraco.Web/HtmlHelperRenderExtensions.cs#L80

    When you understand what keys you need to remove, try to use code like that:

    ApplicationContext.Current.ApplicationCache.ClearCacheByKeyExpression("regexExpression");
    

    Thanks,

    Alex

  • Phil Veloso 36 posts 248 karma points
    Sep 19, 2017 @ 12:29
    Phil Veloso
    0

    Thanks Alex for your reply and apologies for my limited response, I'm mainly a fe developer/designer.

    Presumably, I would want to remove the cachedSeconds parameter so that the cached period was infinite. Then add a new parameter, potentially to the cacheKey with the cached-on date. From which you could check against the current date and updates the key if they are different??? Instead of a date, it could also use a manually defined version number.

    Although on further reflection such a solution may not be ideal for my requirements as the site has a modular design with the same partial view used within numerous templates. Therefore, a custom dashboard with a master reset button for all cached items may be more appropriate and then use a web crawler to sequentially re-cache each page both periodically according to a standard cached time and on request.

    I would be very interested to hear your response for both options and any experience you may have had with similar caching issues.

    Many thanks, Phil

  • Dan Diplo 1467 posts 5653 karma points MVP 3x c-trib
    Sep 19, 2017 @ 11:35
    Dan Diplo
    2

    Simplest way to clear the cache is to simply publish any page in Umbraco - all partial caches are then cleared.

    Also, if you run the site in debug mode (ie. debug="true" in web.config) then partials won't be cached at all, which is what you normally do in development.

    I've also got a package called God Mode that lets you clear the various caches from the Developer tree in Umbraco., via the Utilities section.

  • Phil Veloso 36 posts 248 karma points
    Sep 19, 2017 @ 11:40
    Phil Veloso
    0

    Thanks Dan,

    Looks like an awesome package and seems to do exactly what I wanted + more!

    Any suggestions/ideas on automatically rebuilding the cache after the cache period has expired?

  • Dan Diplo 1467 posts 5653 karma points MVP 3x c-trib
    Sep 19, 2017 @ 12:08
    Dan Diplo
    0
    Any suggestions/ideas on automatically rebuilding the cache after the cache period has expired?
    

    I'm not sure why you'd need to do that? Basically, a partial is cached the first time it is accessed, so it will automatically be re-cached by visiting the page it is on.

    However... In God Mode, in Utilities, there is button called "Warm up Templates" that, when depressed, basically will hit one instance of a page for every template you have in the site. (ie. if you have 10 templates in your site it will hit 10 different URLs, each being one page that uses that template). The main idea was to ensure that views are compiled, but it would also ensure all partials are cached (assuming they are within templates!).

  • Phil Veloso 36 posts 248 karma points
    Sep 19, 2017 @ 12:28
    Phil Veloso
    0

    Thanks again for your help, yes I am using partial views in templates and the feature you mentioned would be good to rebuild manually after clearing the cache once a partial has changed. Although for example, if there is more that one node, of a specific doctype and using a given template, in my case "countries or locations", would it hit all or just one of the nodes?

    The reason I wanted to pre-cache pages for visitors is that I noticed a significant difference, 5-6 seconds, in page speed with vs without caching.

    The site does not have a lot of traffic and most pages are not frequently visited, therefore on the occasion a user does visit a page, I want to make sure that it loads as quickly as possible to ensure the best possible user experience. Also, during the night our server has an idle application pool shutdown period which means that each day the first visited has a long wait.

    Ideally, an automated process would re-build the cache as soon as it expires that's why I thought about creating a crawler. Do you know if this would theoretically work?

  • Dan Diplo 1467 posts 5653 karma points MVP 3x c-trib
    Sep 19, 2017 @ 12:36
    Dan Diplo
    100
    "Although for example, if there is more that one node, of a specific doctype and using a given template, in my case "countries or locations", would it hit all or just one of the nodes?"
    

    It would just hit one of the nodes (chosen effectively at random).

    "The reason I wanted to pre-cache pages for visitors is that I noticed a significant difference, 5-6 seconds, in page speed with vs without caching."
    

    OK, that is a fair use. Though I'd first look at maybe optimising your queries if it's taking that long, if you can. Have a read of - https://our.umbraco.org/documentation/Reference/Common-Pitfalls/

    You can find my source code to warm-up templates from God Mode - it's on GitHub - https://github.com/DanDiplo/Umbraco.GodMode/blob/master/Diplo.GodMode/Services/UmbracoDataService.cs#L294

    That gets the list of URLs - you just need to make an HTTP request to each of them (a HEAD request would do). You could then put that as a scheduled job or, if you don't mind Umbraco starting slowly, in application start.

  • Phil Veloso 36 posts 248 karma points
    Sep 19, 2017 @ 12:47
    Phil Veloso
    0

    Thanks for the advice, I will have a look at the common pitfalls, as I'm sure my code you do with some optimising!

    Also, I like the idea of creating a scheduled job, for a quick look at the code, I could also target a specific template type to only refresh the most important page types.

    Consider the issue resolved for now and have a high five :)

    Thanks again

  • Hussein Khraibani 6 posts 76 karma points
    1 week ago
    Hussein Khraibani
    0

    Thats did the trick! Thanks

Please Sign in or register to post replies

Write your reply to:

Draft