Esempio n. 1
0
        private Task <IBundleCacheItem> GetBundleCacheItem(BundleCacheKey cacheKey, IBundleModel bundle, QueryString query, IDictionary <string, StringValues> @params, HttpContext httpContext,
                                                           bool lockFile)
        {
            return(_cache.GetOrAddAsync(
                       cacheKey,
                       async _ =>
            {
                long startTicks = Stopwatch.GetTimestamp();

                BundleCacheData cacheItem;
                try { cacheItem = await BuildBundleAsync(bundle, query, @params, httpContext); }
                catch (OperationCanceledException)
                {
                    _logger.LogInformation("Bundle [{MANAGER_ID}]:{PATH}{QUERY} was not built. Build was cancelled.", _id, bundle.Path, query);
                    throw;
                }
                catch
                {
                    _logger.LogInformation("Bundle [{MANAGER_ID}]:{PATH}{QUERY} was not built. Build failed.", _id, bundle.Path, query);
                    throw;
                }

                long endTicks = Stopwatch.GetTimestamp();

                if (_logger.IsEnabled(LogLevel.Information))
                {
                    long elapsedMs = (endTicks - startTicks) / (Stopwatch.Frequency / 1000);
                    _logger.LogInformation("Bundle [{MANAGER_ID}]:{PATH}{QUERY} was built in {ELAPSED}ms.", _id, bundle.Path, query, elapsedMs);
                }

                return cacheItem;
            },
                       httpContext.RequestAborted, bundle.CacheOptions, lockFile));
        }
Esempio n. 2
0
        protected virtual IBundleModel CreateModel(Bundle bundle)
        {
            IBundleModel result =
                _modelFactories.Select(f => f.Create(bundle)).FirstOrDefault(m => m != null) ??
                throw ErrorHelper.ModelFactoryNotAvailable(bundle.GetType());

            result.Changed += BundleChanged;
            return(result);
        }
Esempio n. 3
0
        protected virtual async Task <BundleCacheData> BuildBundleAsync(IBundleModel bundle, QueryString query, IDictionary <string, StringValues> @params, HttpContext httpContext)
        {
            var startTicks = Stopwatch.GetTimestamp();

            var builderContext = new BundleBuilderContext
            {
                BundlingContext   = _bundlingContext,
                AppBasePath       = httpContext.Request.PathBase,
                Params            = @params,
                Bundle            = bundle,
                ChangeSources     = _enableChangeDetection ? new HashSet <IChangeSource>() : null,
                CancellationToken = httpContext.RequestAborted
            };

            bundle.OnBuilding(builderContext);

            await bundle.Builder.BuildAsync(builderContext);

            var            content   = bundle.OutputEncoding.GetBytes(builderContext.Result);
            DateTimeOffset timestamp = _clock.UtcNow;

            var versionProviderContext = new BundleVersionProviderContext
            {
                Timestamp         = timestamp,
                Content           = content,
                CancellationToken = httpContext.RequestAborted
            };

            _versionProvider.Provide(versionProviderContext);

            bundle.OnBuilt(builderContext);

            var endTicks = Stopwatch.GetTimestamp();

            if (_logger.IsEnabled(LogLevel.Information))
            {
                var elapsedMs = (endTicks - startTicks) / (Stopwatch.Frequency / 1000);
                _logger.LogInformation("Bundle [{MANAGER_ID}]:{PATH}{QUERY} was built in {ELAPSED}ms.", _id, bundle.Path, query, elapsedMs);
            }

            return(new BundleCacheData
            {
                Content = content,
                Timestamp = timestamp,
                Version = versionProviderContext.Result,
            });
        }
Esempio n. 4
0
        protected virtual async Task <BundleCacheData> BuildBundleAsync(IBundleModel bundle, QueryString query, IDictionary <string, StringValues> @params, HttpContext httpContext)
        {
            var startTicks = Stopwatch.GetTimestamp();

            var builderContext = new BundleBuilderContext
            {
                BundlingContext = _bundlingContext,
                HttpContext     = httpContext,
                Params          = @params,
                Bundle          = bundle,
            };

            await bundle.Builder.BuildAsync(builderContext);

            var content   = bundle.OutputEncoding.GetBytes(builderContext.Result);
            var timestamp = _clock.UtcNow;

            var versionProviderContext = new BundleVersionProviderContext
            {
                HttpContext = httpContext,
                Timestamp   = timestamp,
                Content     = content,
            };

            _versionProvider.Provide(versionProviderContext);

            var endTicks = Stopwatch.GetTimestamp();

            if (_logger.IsEnabled(LogLevel.Information))
            {
                var elapsedMs = (endTicks - startTicks) / (Stopwatch.Frequency / 1000);
                _logger.LogInformation("Bundle instance [{MANAGER_ID}]:{PATH}{QUERY} was built in {ELAPSED}ms.", _id, bundle.Path, query, elapsedMs);
            }

            return(new BundleCacheData
            {
                Content = content,
                Timestamp = timestamp,
                Version = versionProviderContext.Result,
            });
        }
Esempio n. 5
0
        protected virtual async Task <BundleCacheData> BuildBundleAsync(IBundleModel bundle, QueryString query, IDictionary <string, StringValues> @params, HttpContext httpContext)
        {
            var builderContext = new BundleBuilderContext
            {
                BundlingContext   = _bundlingContext,
                AppBasePath       = httpContext.Request.PathBase,
                Params            = @params,
                Bundle            = bundle,
                ChangeSources     = _enableChangeDetection ? new HashSet <IChangeSource>() : null,
                CancellationToken = httpContext.RequestAborted
            };

            bundle.OnBuilding(builderContext);

            await bundle.Builder.BuildAsync(builderContext);

            var            content   = bundle.OutputEncoding.GetBytes(builderContext.Result);
            DateTimeOffset timestamp = _clock.UtcNow;

            var versionProviderContext = new BundleVersionProviderContext
            {
                Timestamp         = timestamp,
                Content           = content,
                CancellationToken = httpContext.RequestAborted
            };

            _versionProvider.Provide(versionProviderContext);

            bundle.OnBuilt(builderContext);

            return(new BundleCacheData
            {
                Content = content,
                Timestamp = timestamp,
                Version = versionProviderContext.Result,
            });
        }
Esempio n. 6
0
 private async void InvalidateBundleCache(IBundleModel bundle)
 {
     try { await _cache.RemoveAllAsync(_id, bundle.Path, _shutdownToken).ConfigureAwait(false); }
     catch (Exception ex) { _logger.LogError(ex, "Unexpected error occurred during updating cache."); }
 }