Copied to clipboard

Flag this post as spam?

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


  • Bo Jacobsen 605 posts 2403 karma points
    Mar 28, 2022 @ 09:58
    Bo Jacobsen
    0

    Cache with dependecy file

    Hi all.

    We are caching redirect urls and are trying to convert our code from Umbraco 8 to Umbraco 9 and are missing the good old CacheItemRemovedCallback from System.Web.Caching.

    In Umbraco 8 we used Umbraco.Core.Cache.AppCacheExtensions:

    public static void InsertCacheItem<T>(this IAppPolicyCache provider, string cacheKey, Func<T> getCacheItem, TimeSpan? timeout = null, bool isSliding = false, CacheItemPriority priority = CacheItemPriority.Normal, CacheItemRemovedCallback removedCallback = null, string[] dependentFiles = null);
    

    Like this:

    protected void InsertIntoCache(object item)
    {
        appCaches.RuntimeCache.InsertCacheItem("customKey", () => item, null, false, CacheItemPriority.Normal, new CacheItemRemovedCallback(CachedItemRemovedCallback), new string[] { "D:\\root\\web\\app_data\\somefile.txt" });
    }
    
    protected void CachedItemRemovedCallback(string key, object val, CacheItemRemovedReason reason)
    {       
        if (key.Contains(Key))
        {
            var item = Get(); // Some function to get item for Cache.
            InsertIntoCache(item);
        }
    }
    

    In Umbraco 9 there is no CachedItemRemovedCallback in Umbraco.Extensions.AppCacheExtensions:

    public static void InsertCacheItem<T>(this IAppPolicyCache provider, string cacheKey, Func<T> getCacheItem, TimeSpan? timeout = null, bool isSliding = false, string[] dependentFiles = null)
    

    So how do we setup something similar in Umbraco 9?

Please Sign in or register to post replies

Write your reply to:

Draft