/// <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; try { cacheKey = asset.GenerateCacheKey(context); } catch (FileNotFoundException) { _logger.LogFileNotFound(context.Request.Path); return null; } 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); } }