Copied to clipboard

Flag this post as spam?

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

  • Andy Boot 23 posts 115 karma points
    Apr 22, 2021 @ 08:54
    Andy Boot

    Caching strings/objects & best practices

    Hi There,

    I'm looking for some advice on the best ways to cache strings returned from an API, local file contents and/or other sources external to the database.

    I've already achieved what I set out to do and works flawlessly. The only thing now is, I'm left wondering if there could have been a far better and potentially native way of doing this. So basically I'm using a combination of DevTrends MvcDonutCaching and the Client Dependency version ID. See my code below:

    var clientDependencyVersion = ClientDependency.Core.Config.ClientDependencySettings.Instance.Version;
            var cacheManager = new DevTrends.MvcDonutCaching.OutputCacheManager();
            var cacheName = $"CacheExample-{clientDependencyVersion}";
            var cacheItem = cacheManager.GetItem(cacheName);
            string value;
            if (cacheItem == null)
                value = System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath(path));
                cacheManager.AddItem(cacheName, new DevTrends.MvcDonutCaching.CacheItem() { Content = value }, DateTime.Now.AddHours(2));
                value = cacheItem.Content;
            return value;

    Obviously, it goes without saying, changing the Client Dependency version ID produces another unique cache key and retrieves some fresh new data from our source.

    Any tips or advice would certainly be appreciated.



  • iNETZO 123 posts 463 karma points c-trib
    Apr 25, 2021 @ 18:32

    Hi Andy,

    I think your looking for AppCaches.RuntimeCache (Umbraco.Core.Cache)

            string fromCache = AppCaches.RuntimeCache.GetCacheItem<string>("stringfromcache", () =>
                return "This string is from cache"; // Your string, object, etc you want to cache
            }, TimeSpan.FromMinutes(30)); //clear cache after 30min

    You can set the amount of time you would like the data to be cached but this is optional.

    Best regards,


  • Andy Boot 23 posts 115 karma points
    Apr 25, 2021 @ 21:27
    Andy Boot

    Great! Looks like a much cleaner, simpler and the native solution that I was originally searching for. Thanks for that

Please Sign in or register to post replies

Write your reply to: