コード例 #1
0
        private dynamic BuildCachedShape(IContent content, string displayType, string groupId)
        {
            _cacheSettings     = GetOutputCacheSettings(content);
            _cacheKey          = ComputeCacheKey(content, displayType, groupId);
            _invariantCacheKey = string.Format("tenant={0};id={1};", _shellSettings.Name, content.ContentItem.Id);
            var cacheKeyLock  = _cacheKeyLocks.GetOrAdd(_cacheKey, x => new object());
            var cacheDuration = _cacheSettings.CacheDuration;

            _now = _clock.UtcNow;
            //dynamic result = null;

            try
            {
                var cacheItem = _cacheStorageProvider.GetCacheItem(_cacheKey);
                if (cacheItem != null)
                {
                    if (_now > cacheItem.ValidUntilUtc && _now < cacheItem.CachedOnUtc.AddSeconds(cacheDuration + _cacheSettings.CacheGraceTime))
                    {
                        if (Monitor.TryEnter(cacheKeyLock))
                        {
                            return(BeginRenderItem(content, displayType, groupId));
                        }
                    }

                    return(ServeCachedItem(cacheItem, content, displayType, groupId));
                }

                if (Monitor.TryEnter(cacheKeyLock))
                {
                    cacheItem = _cacheStorageProvider.GetCacheItem(_cacheKey);
                    if (cacheItem != null)
                    {
                        Monitor.Exit(cacheKeyLock);
                        return(ServeCachedItem(cacheItem, content, displayType, groupId));
                    }
                }

                return(BeginRenderItem(content, displayType, groupId));
            }
            catch
            {
                if (Monitor.IsEntered(cacheKeyLock))
                {
                    Monitor.Exit(cacheKeyLock);
                }
                throw;
            }
        }
コード例 #2
0
        private dynamic BeginRenderItem(IContent content, string displayType, string groupId)
        {
            WorkContext workContext = _workContextAccessor.GetContext();
            var         output      = _shapeDisplay.Display(BuildShape(content, displayType, groupId));

            _cacheSettings     = GetOutputCacheSettings(content);
            _cacheKey          = ComputeCacheKey(content, displayType, groupId);
            _invariantCacheKey = string.Format("tenant={0};id={1};", _shellSettings.Name, content.ContentItem.Id);

            var cacheItem = new CacheItem()
            {
                CachedOnUtc = _now,
                ValidFor    = _cacheSettings.CacheDuration,
                Output      = _jsonConverter.Serialize(new OutputCacheItem
                {
                    Id        = content.ContentItem.Id,
                    Output    = output,
                    Resources = _resourceManager.GetRequiredResources("script")
                                .Concat(_resourceManager.GetRequiredResources("stylesheet")).Select(GetCacheItemResource).ToList()
                }),
                ContentType       = content.ContentItem.ContentType,
                QueryString       = workContext.HttpContext.Request.Url.Query,
                CacheKey          = _cacheKey,
                InvariantCacheKey = _invariantCacheKey,
                Url        = workContext.HttpContext.Request.Url.AbsolutePath,
                Tenant     = _shellSettings.Name,
                StatusCode = workContext.HttpContext.Response.StatusCode,
                Tags       = new[] { _invariantCacheKey, content.ContentItem.Id.ToString(CultureInfo.InvariantCulture) }
            };

            _cacheStorageProvider.Remove(_cacheKey);
            _cacheStorageProvider.Set(_cacheKey, cacheItem);

            foreach (var tag in cacheItem.Tags)
            {
                _tagCache.Tag(tag, _cacheKey);
            }

            return(ServeCachedItem(cacheItem, content, displayType, groupId));
        }