Exemplo n.º 1
0
        private static T Get <T>(this ObjectCache cache, string key, Func <T> acquire, Action <string, object> set)
        {
            string cacheKey = CacheKeyGenerator.GenerateCacheKey <T>(key);

            if (cache.IsSet(cacheKey))
            {
                var acquiredValue = cache.Get(cacheKey);
                if (acquiredValue is NullObject)
                {
                    return(default(T));
                }
                return((T)acquiredValue);
            }

            using (CacheLock.Lock(cacheKey))
            {
                if (cache.IsSet(cacheKey))
                {
                    return((T)cache.Get(cacheKey));
                }

                var acquiredValue = acquire();
                if (acquiredValue != null)
                {
                    set(cacheKey, acquiredValue);
                }
                else
                {
                    set(cacheKey, NullObject.Instance);
                }

                return(acquiredValue);
            }
        }