Skip to content

Commit

Permalink
optimize cache logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tinohager committed Feb 13, 2024
1 parent 6a82eb0 commit 4a5bfd7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
5 changes: 3 additions & 2 deletions src/Nager.PublicSuffix.UnitTest/RuleProviderTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nager.PublicSuffix.CacheProviders;
using Nager.PublicSuffix.RuleProviders;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

Expand All @@ -18,7 +18,8 @@ public async Task WebTldRuleProviderTest()

var configuration = builder.Build();

var webRuleProvider = new WebRuleProvider(configuration, httpClient);
var cacheProvider = new LocalFileSystemCacheProvider();
var webRuleProvider = new WebRuleProvider(configuration, cacheProvider, httpClient);
var domainDataStructure = await webRuleProvider.BuildAsync();
Assert.IsNotNull(domainDataStructure);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Nager.PublicSuffix.WebApi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Nager.PublicSuffix;
using Nager.PublicSuffix.CacheProviders;
using Nager.PublicSuffix.RuleProviders;
using System.Text.Json.Serialization;

Expand All @@ -7,6 +8,7 @@
// Add services to the container.

builder.Services.AddHttpClient();
builder.Services.AddSingleton<ICacheProvider, LocalFileSystemCacheProvider>();
builder.Services.AddSingleton<IRuleProvider, WebRuleProvider>();
builder.Services.AddSingleton<IDomainParser, DomainParser>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@
namespace Nager.PublicSuffix.CacheProviders
{
/// <summary>
/// FileCacheProvider, write the data to a cache file in the temp directory
/// LocalFileSystemCacheProvider, write the data to a cache file in the temp directory
/// </summary>
public class FileCacheProvider : ICacheProvider
public class LocalFileSystemCacheProvider : ICacheProvider
{
private readonly string _cacheFilePath;
private readonly TimeSpan _timeToLive;

/// <summary>
/// FileCacheProvider
/// LocalFileSystemCacheProvider
/// </summary>
/// <param name="cacheFileName"></param>
/// <param name="cachePath">The path of the cache file, default use the temp path of the os</param>
/// <param name="cacheTimeToLive"></param>
public FileCacheProvider(
public LocalFileSystemCacheProvider(
string cacheFileName = "publicsuffixcache.dat",
string? cachePath = null,

Check warning on line 23 in src/Nager.PublicSuffix/CacheProviders/LocalFileSystemCacheProvider.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 23 in src/Nager.PublicSuffix/CacheProviders/LocalFileSystemCacheProvider.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 23 in src/Nager.PublicSuffix/CacheProviders/LocalFileSystemCacheProvider.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
TimeSpan? cacheTimeToLive = null)
{
if (cacheTimeToLive.HasValue)
Expand All @@ -30,7 +32,7 @@ public FileCacheProvider(
this._timeToLive = TimeSpan.FromDays(1);
}

var tempPath = Path.GetTempPath();
var tempPath = cachePath ?? Path.GetTempPath();
this._cacheFilePath = Path.Combine(tempPath, cacheFileName);
}

Expand Down
16 changes: 5 additions & 11 deletions src/Nager.PublicSuffix/RuleProviders/WebRuleProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ public class WebRuleProvider : IRuleProvider
/// </summary>
/// <remarks>It is possible to overwrite the url via configuration parameters <c>Nager:PublicSuffix:DataUrl</c></remarks>
/// <param name="configuration"></param>
/// <param name="cacheProvider"></param>
/// <param name="httpClient"></param>
/// <param name="cacheProvider">default is <see cref="FileCacheProvider"/></param>
public WebRuleProvider(
IConfiguration configuration,
HttpClient httpClient,
ICacheProvider cacheProvider = null)
ICacheProvider cacheProvider,
HttpClient httpClient
)
{
this._cacheProvider = cacheProvider;
this._httpClient = httpClient;

var url = configuration["Nager:PublicSuffix:DataUrl"];
Expand All @@ -47,14 +49,6 @@ public WebRuleProvider(
}

this._dataFileUrl = url;

if (cacheProvider == null)
{
this._cacheProvider = new FileCacheProvider();
return;
}

this._cacheProvider = cacheProvider;
}

/// <inheritdoc/>
Expand Down

0 comments on commit 4a5bfd7

Please sign in to comment.