Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh HttpRuntime.Cache on Azure

Tags:

c#

asp.net

azure

Is there a way that i can refresh the HttpRuntime.Cache on Azure without having to Reboot the instance?

Thanks

like image 253
Bongo Sharp Avatar asked Feb 20 '23 02:02

Bongo Sharp


1 Answers

When you're adding items to your cache, you should add them with a CacheDependency with a global key. This allows you to have your items expire after a given time but also to clear all items linked to this key.

Here is an example implementation of a method to clear all items (CacheProvider.cs):

/// <summary>
/// Caching provider
/// </summary>
public static class CacheProvider
{
    const string CacheDependencyKey = "1FADE275-2C84-4a9b-B3E1-68ABB15E53C8";
    static readonly object SyncRoot = new object();

    /// <summary>
    /// Gets an item from cache. If the item does not exist, one will be
    /// created and added to the cache.
    /// </summary>
    /// <param name="key">Caching key</param>
    /// <param name="valueFactory">Function to create the item of it does not exist in the cache.</param>
    /// <param name="expiresAfter">Time after the item wille be removed from cache.</param>
    public static TValue GetOrAdd<TValue>(string key, Func<TValue> valueFactory, TimeSpan expiresAfter)
    {
        object itemFromCache = HttpRuntime.Cache.Get(key);
        if (itemFromCache == null)
        {
            lock (SyncRoot)
            {
                itemFromCache = HttpRuntime.Cache.Get(key);
                if (itemFromCache == null)
                {
                    TValue value = valueFactory();
                    if (value != null)
                    {
                        if (HttpRuntime.Cache[CacheDependencyKey] == null)
                            HttpRuntime.Cache[CacheDependencyKey] = string.Empty;

                        HttpRuntime.Cache.Add(key, value, new CacheDependency(null, new string[] { CacheDependencyKey }), DateTime.Now.Add(expiresAfter), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
                    }
                    return value;
                }
            }
        }
        return (TValue)itemFromCache;
    }

    /// <summary>
    /// Invalidate all the items from the cache.
    /// </summary>
    public static void InvalidateCache()
    {
        HttpRuntime.Cache.Remove(CacheDependencyKey);
    }
}
like image 136
Sandrino Di Mattia Avatar answered Feb 28 '23 14:02

Sandrino Di Mattia