public AssetContext(HttpContext httpContext, IAsset asset, IWebOptimizerOptions options) { Content = new Dictionary <string, byte[]>(); HttpContext = httpContext ?? throw new ArgumentNullException(nameof(httpContext)); Asset = asset ?? throw new ArgumentNullException(nameof(asset)); Options = options ?? throw new ArgumentNullException(nameof(options)); }
// Only called when we expect to serve the body. private static void SetCompressionMode(HttpContext context, IWebOptimizerOptions options) { IHttpsCompressionFeature responseCompressionFeature = context.Features.Get <IHttpsCompressionFeature>(); if (responseCompressionFeature != null) { responseCompressionFeature.Mode = options.HttpsCompression; } }
/// <summary> /// Ensures that defaults are set /// </summary> public static void EnsureDefaults(this IWebOptimizerOptions options, IHostingEnvironment env) { if (env == null) { throw new ArgumentNullException(nameof(env)); } options.EnableCaching = options.EnableCaching ?? !env.IsDevelopment(); options.EnableTagHelperBundling = options.EnableTagHelperBundling ?? true; }
private void AddToCache(string cacheKey, AssetResponse value, IAsset asset, IWebOptimizerOptions options) { if (options.EnableMemoryCache == true) { var cacheOptions = new MemoryCacheEntryOptions(); cacheOptions.SetSlidingExpiration(TimeSpan.FromHours(24)); foreach (string file in asset.SourceFiles) { cacheOptions.AddExpirationToken(asset.GetFileProvider(_env).Watch(file)); } _cache.Set(cacheKey, value, cacheOptions); } }
public async Task <byte[]> ExecuteAsync(HttpContext context, IWebOptimizerOptions options) { var env = (IHostingEnvironment)context.RequestServices.GetService(typeof(IHostingEnvironment)); var config = new AssetContext(context, this, options); IEnumerable <string> files = ExpandGlobs(this, env); DateTime lastModified = DateTime.MinValue; // Read file content into memory foreach (string file in files) { if (!config.Content.ContainsKey(file)) { DateTime dateChanged = await LoadFileContentAsync(this.GetFileProvider(env), config, file); if (dateChanged > lastModified) { lastModified = dateChanged; } } } if (lastModified != DateTime.MinValue) { context.Response.Headers[HeaderNames.LastModified] = lastModified.ToString("R"); } // Attach the processors foreach (IProcessor processor in Processors) { await processor.ExecuteAsync(config).ConfigureAwait(false); } return(config.Content.FirstOrDefault().Value); }
/// <summary> /// Builds an asset by running it through all the processors. /// </summary> public async Task <IAssetResponse> BuildAsync(IAsset asset, HttpContext context, IWebOptimizerOptions options) { string cacheKey = asset.GenerateCacheKey(context); if (options.EnableMemoryCache == true && _cache.TryGetValue(cacheKey, out AssetResponse value)) { _logger.LogServedFromMemoryCache(context.Request.Path); return(value); } else if (options.EnableDiskCache == true && _assetResponseCache.TryGet(asset.Route, cacheKey, out value)) { AddToCache(cacheKey, value, asset, options); return(value); } else { byte[] bytes = await asset.ExecuteAsync(context, options).ConfigureAwait(false); var response = new AssetResponse(bytes, cacheKey); foreach (string name in context.Response.Headers.Keys) { response.Headers.Add(name, context.Response.Headers[name]); } if (options.AllowEmptyBundle == false && (bytes == null || bytes.Length == 0)) { return(null); } AddToCache(cacheKey, response, asset, options); if (options.EnableDiskCache == true) { await _assetResponseCache.AddAsync(asset.Route, cacheKey, response).ConfigureAwait(false); } _logger.LogGeneratedOutput(context.Request.Path); return(response); } }
/// <summary> /// Builds an asset by running it through all the processors. /// </summary> public async Task <IAssetResponse> BuildAsync(IAsset asset, HttpContext context, IWebOptimizerOptions options) { options.EnsureDefaults(_env); string cacheKey = asset.GenerateCacheKey(context); if (_cache.TryGetValue(cacheKey, out AssetResponse value)) { _logger.LogServedFromMemoryCache(context.Request.Path); return(value); } else if (AssetResponse.TryGetFromDiskCache(context.Request.Path, cacheKey, _cacheDir, out value)) { AddToCache(cacheKey, value, asset, options); return(value); } else { byte[] bytes = await asset.ExecuteAsync(context, options).ConfigureAwait(false); var response = new AssetResponse(bytes, cacheKey); foreach (string name in context.Response.Headers.Keys) { response.Headers.Add(name, context.Response.Headers[name]); } if (bytes == null || bytes.Length == 0) { return(null); } AddToCache(cacheKey, response, asset, options); await response.CacheToDiskAsync(context.Request.Path, cacheKey, _cacheDir).ConfigureAwait(false); _logger.LogGeneratedOutput(context.Request.Path); return(response); } }