public T CreateOrGet()

in src/DeloitteDigital.Atlas/Caching/WebCacheService.cs [27:66]


        public T CreateOrGet<T>(string key, TimeSpan duration, Func<T> resolver, bool invalidateOnPublish = false)
        {
            if (!CacheHelper.EnsureWebDatabase())
            {
                // we only support caching against the master or web database
                // core and unknown which may happen from a sitecore event handler or indexing
                // are just too difficult

                return resolver();
            }

            // prefix items this class manages to reduce side effects
            var prefixedKey = CreateKey(key);
                      
            var result = cache.Get(prefixedKey);

            if (result != null)
            {
                var item = result as CacheItem;
                if (item != null)
                {
                    logService.Debug(string.Format("DeloitteDigital.Atlas.Caching.WebCacheService.CreateOrGet - found item with key '{0}' in cache.", prefixedKey), this);
                    return (T)item.Data;
                }
            }

            result = resolver();

            if (result == null)
            {
                logService.Debug(string.Format("DeloitteDigital.Atlas.Caching.WebCacheService.CreateOrGet - resolution of item with key '{0}' was null. Nothing was cached.", prefixedKey), this);
                return default(T);
            }

            AddToCache(prefixedKey, key, result, duration, invalidateOnPublish);

            // cache miss
            logService.Debug(string.Format("DeloitteDigital.Atlas.Caching.WebCacheService.CreateOrGet - added item with key '{0}' in cache.", prefixedKey), this);
            return (T)result;
        }