Skip to content

Commit

Permalink
Refactor WebOptimizerOptions.EnsureDefaults() (#85)
Browse files Browse the repository at this point in the history
* Ensure DI always injects WebOptimizerOptions with appropriate defaults

* Can't consume a Transiet from a Singleton

* Can't consume a Transiet from a Singleton
  • Loading branch information
hellfirehd authored and madskristensen committed Apr 12, 2018
1 parent 5f211d3 commit f3b4e32
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 42 deletions.
1 change: 0 additions & 1 deletion src/WebOptimizer.Core/AssetBuilder.cs
Expand Up @@ -35,7 +35,6 @@ public AssetBuilder(IMemoryCache cache, IAssetResponseStore assetResponseCache,
/// </summary>
public async Task<IAssetResponse> BuildAsync(IAsset asset, HttpContext context, IWebOptimizerOptions options)
{
options.EnsureDefaults(_env);
string cacheKey = asset.GenerateCacheKey(context);

if (options.EnableMemoryCache == true && _cache.TryGetValue(cacheKey, out AssetResponse value))
Expand Down
10 changes: 3 additions & 7 deletions src/WebOptimizer.Core/AssetResponseStore.cs
Expand Up @@ -14,25 +14,21 @@ internal class AssetResponseStore : IAssetResponseStore
private readonly ILogger<AssetResponseStore> _logger;
private readonly IHostingEnvironment _env;
private readonly WebOptimizerOptions _options = new WebOptimizerOptions();
private string _cacheDir;

public AssetResponseStore(ILogger<AssetResponseStore> logger, IHostingEnvironment env, IConfigureOptions<WebOptimizerOptions> options)
{
_logger = logger;
_env = env;
options.Configure(_options);
_cacheDir = string.IsNullOrWhiteSpace(_options.CacheDirectory)
? Path.Combine(env.ContentRootPath, "obj", "WebOptimizerCache")
: _options.CacheDirectory;
}

public async Task AddAsync(string bucket, string cachekey, AssetResponse assetResponse)
{
string name = CleanName(bucket);
Directory.CreateDirectory(_cacheDir);
Directory.CreateDirectory(_options.CacheDirectory);

// First delete old cached files
IEnumerable<string> oldCachedFiles = Directory.EnumerateFiles(_cacheDir, name + "__*.cache");
IEnumerable<string> oldCachedFiles = Directory.EnumerateFiles(_options.CacheDirectory, name + "__*.cache");
foreach (string oldFile in oldCachedFiles)
{
await DeleteFileAsync(oldFile).ConfigureAwait(false);
Expand Down Expand Up @@ -81,7 +77,7 @@ private string GetPath(string bucket, string cachekey)
{
// cachekey is Base64 encoded which uses / as one of the characters. So for Linux
// we need to clean both the bucket and the cachekey.
return Path.Combine(_cacheDir, CleanName($"{bucket}__{cachekey}.cache"));
return Path.Combine(_options.CacheDirectory, CleanName($"{bucket}__{cachekey}.cache"));
}

private async Task DeleteFileAsync(string filePath, int attempts = 5)
Expand Down
24 changes: 0 additions & 24 deletions src/WebOptimizer.Core/Extensions/OptionsExtensions.cs

This file was deleted.

3 changes: 0 additions & 3 deletions src/WebOptimizer.Core/TagHelpersDynamic/Helpers.cs
Expand Up @@ -41,9 +41,6 @@ private class AssetItem
serviceProvider.GetService(typeof(IOptionsSnapshot<WebOptimizerOptions>)))
.Value;

//Ensures that defaults are set
options.EnableCaching = options.EnableCaching ?? !env.IsDevelopment();
options.EnableTagHelperBundling = options.EnableTagHelperBundling ?? true;
_webOptimizerOptions = options;
}

Expand Down
2 changes: 0 additions & 2 deletions src/WebOptimizer.Core/Taghelpers/LinkTagHelper.cs
Expand Up @@ -40,8 +40,6 @@ public override void Process(TagHelperContext context, TagHelperOutput output)

if (Pipeline.TryGetAssetFromRoute(href, out IAsset asset) && !output.Attributes.ContainsName("inline"))
{
Options.EnsureDefaults(HostingEnvironment);

if (Options.EnableTagHelperBundling == true)
{
href = GenerateHash(asset);
Expand Down
2 changes: 0 additions & 2 deletions src/WebOptimizer.Core/Taghelpers/ScriptTagHelper.cs
Expand Up @@ -38,8 +38,6 @@ public override void Process(TagHelperContext context, TagHelperOutput output)

if (Pipeline.TryGetAssetFromRoute(src, out IAsset asset) && !output.Attributes.ContainsName("inline"))
{
Options.EnsureDefaults(HostingEnvironment);

if (Options.EnableTagHelperBundling == true)
{
src = GenerateHash(asset);
Expand Down
19 changes: 16 additions & 3 deletions src/WebOptimizer.Core/WebOptimizerConfig.cs
@@ -1,4 +1,6 @@
using Microsoft.Extensions.Configuration;
using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;

namespace WebOptimizer
Expand All @@ -7,11 +9,13 @@ internal class WebOptimizerConfig : IConfigureOptions<WebOptimizerOptions>
{
private IConfiguration _config;
private IOptionsMonitorCache<WebOptimizerOptions> _options;
private readonly IHostingEnvironment _hostingEnvironment;

public WebOptimizerConfig(IConfiguration config, IOptionsMonitorCache<WebOptimizerOptions> options)
public WebOptimizerConfig(IConfiguration config, IOptionsMonitorCache<WebOptimizerOptions> options, IHostingEnvironment hostingEnvironment)
{
_config = config;
_options = options;
_hostingEnvironment = hostingEnvironment;
}

public void Configure(WebOptimizerOptions options)
Expand All @@ -21,7 +25,16 @@ public void Configure(WebOptimizerOptions options)
_options.TryRemove(Options.DefaultName);
}, null);

ConfigurationBinder.Bind(_config.GetSection("WebOptimizer"), options);
_config.GetSection("WebOptimizer").Bind(options);

options.EnableCaching = options.EnableCaching ?? !_hostingEnvironment.IsDevelopment();
options.EnableDiskCache = options.EnableDiskCache ?? !_hostingEnvironment.IsDevelopment();
options.EnableMemoryCache = options.EnableMemoryCache ?? true;
options.EnableTagHelperBundling = options.EnableTagHelperBundling ?? true;
options.CacheDirectory = string.IsNullOrWhiteSpace(options.CacheDirectory)
? Path.Combine(_hostingEnvironment.ContentRootPath, "obj", "WebOptimizerCache")
: options.CacheDirectory;

}
}
}

0 comments on commit f3b4e32

Please sign in to comment.