Exemple #1
0
        public static object GetOrAddUntyped(this ICache cache, string key, Func <object> add, TimeSpan?timeout = null)
        {
            var item = cache.Get(key);

            if (item == null)
            {
                if (add == null)
                {
                    return(null);
                }
                using (TimedLock.Lock(CacheLockScope.AcquireLock <object>(key), timeout ?? Depot.ContentionTimeout))
                {
                    var itemToAdd = add();
                    if (itemToAdd != null)
                    {
                        cache.Add(key, itemToAdd);
                    }
                }
                return(cache.Get(key));
            }
            return(item);
        }
Exemple #2
0
        public static T GetOrAddTyped <T>(this ICache cache, string key, Func <T> add, TimeSpan?timeout = null) where T : class
        {
            var item = cache.Get(key);

            if (item == null)
            {
                if (add == null)
                {
                    return(default(T));
                }
                using (TimedLock.Lock(CacheLockScope.AcquireLock <T>(key), timeout ?? Depot.ContentionTimeout))
                {
                    var itemToAdd = add();
                    if (itemToAdd != null)
                    {
                        cache.Add(key, itemToAdd);
                    }
                }
                return(cache.Get <T>(key));
            }
            return((T)item);
        }