Copied to clipboard

Flag this post as spam?

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

  • Ilyas Balgabekov 21 posts 132 karma points
    Jan 18, 2021 @ 04:42
    Ilyas Balgabekov

    Static files caching Umbraco 7/8

    Hello! Have anyone good practice with static files in Umbraco? Right now I have only two methods.

    1) use @Html.ChachedPartial()
    2) add in web.config. (here I very much doubt that this is the best method )

          <clientCache cacheControlMode="UseExpires"
                httpExpires="Tue, 19 Jan 2038 03:14:07 GMT" />

    I'll be greateful for any help.

  • AddWeb Solution Pvt. Ltd 109 posts 353 karma points
    Jan 18, 2021 @ 06:15
    AddWeb Solution Pvt. Ltd

    Hello llyas,

    (system.webServer/staticContent/clientCache is for static resources (files))

    you can create your own implementation of CachedPartial, something like this:

    using System;
    using System.Web.Mvc;
    using System.Web.Mvc.Html;
    using Umbraco.Web;
    using System.Web;
    using System.Runtime.Caching;
    public static class CachedPartialExtensions
    //(From version 7 there is an overload for CachedPartial that allows the key to be passed in)
    public static IHtmlString CachedPartial(
    this HtmlHelper htmlHelper,
    string partialViewName,
    object model,
    int cachedSeconds,
    bool cacheByPage = false,
    bool cacheByMember = false,
    ViewDataDictionary viewData = null,
    Func<object, ViewDataDictionary, string> contextualKeyBuilder = null);
    var newCacheKey = "fpc-"; //prefix to know which keys to clear on page publish (in Bootstraper.cs file)
    newCacheKey += partialViewName;
    if (cacheByPage)
    newCacheKey += "page-" + UmbracoContext.Current.PageId;
    if (!string.IsNullOrEmpty(cacheKey))
    newCacheKey += "key-" + cacheKey;
    var result = MemoryCache.Default.Get(newCacheKey) as MvcHtmlString;
    if(result == null)
    result = htmlHelper.Partial(partialViewName, model, viewData);
    MemoryCache.Default.Add(new CacheItem(newCacheKey, result), new CacheItemPolicy
    AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(cachedSeconds)
    return result;

    The use case for this would be:

    cacheByPage: true,
    contextualKeyBuilder: (model, viewData) =>
    return (model as MyViewModel).Id.ToString();

    you will be able to provide your own keys for cache:

    @Html.MyCachedPartial("PartialView", Model, 60, cacheKey: "model1key", cacheByPage: true)
    @Html.MyCachedPartial("PartialView", Model2, 60, cacheKey: "model2key", cacheByPage: true)
  • Ilyas Balgabekov 21 posts 132 karma points
    Jan 18, 2021 @ 06:50
    Ilyas Balgabekov

    Thanks, it looks interesting I will definitely try it. Is it possible to manage all static elements? For example, I want ALL the images(.png or .jpg) on the site to be stored in the cache for 3 days.

  • AddWeb Solution Pvt. Ltd 109 posts 353 karma points
    Jan 18, 2021 @ 08:23
    AddWeb Solution Pvt. Ltd

    Try to use Client side caching Easy way to add client side caching to site - add this section to your Web.config:

    <location path="media">
    <clientCache cacheControlMode="UseMaxAge" cacheControlCustom="public" cacheControlMaxAge="7.00:00:00" />

    (For example, cache-control: max-age=120 means that the returned resource is valid for 120 seconds, after which the browser has to request a newer version)

Please Sign in or register to post replies

Write your reply to: