diff --git a/src/SwiftLink.Application/Common/Interfaces/ICacheProvider.cs b/src/SwiftLink.Application/Common/Interfaces/ICacheProvider.cs index 193d8fd..9643a39 100644 --- a/src/SwiftLink.Application/Common/Interfaces/ICacheProvider.cs +++ b/src/SwiftLink.Application/Common/Interfaces/ICacheProvider.cs @@ -1,26 +1,25 @@ namespace SwiftLink.Application.Common.Interfaces; -internal interface ICacheProvider +public interface ICacheProvider { /// /// Associate a value with a key in the Cache such as Redis. /// /// The key of the entry to add. /// The value to associate with the key. - /// The value that was set. - public string Set(string key, string value); + /// The value that was set or not. + public Task Set(string key, string value); /// /// Removes the object associated with the given key. /// /// An object identifying the requested entry. - void Remove(string key); + Task Remove(string key); /// /// Gets the item associated with this key if present. /// /// An object identifying the requested entry. - /// The located value or default url. - /// - bool TryGetValue(string key, out string value); + /// The located value or default url + Task Get(string key); } diff --git a/src/SwiftLink.Infrastructure/CacheProvider/RedisCacheProvider.cs b/src/SwiftLink.Infrastructure/CacheProvider/RedisCacheProvider.cs new file mode 100644 index 0000000..a0fff9a --- /dev/null +++ b/src/SwiftLink.Infrastructure/CacheProvider/RedisCacheProvider.cs @@ -0,0 +1,25 @@ +using Microsoft.Extensions.Caching.Distributed; +using System.Text; + +namespace SwiftLink.Infrastructure.CacheProvider; + +public class RedisCacheService(IDistributedCache cache) : ICacheProvider +{ + private readonly IDistributedCache _cache = cache; + + public Task Remove(string key) + => _cache.RemoveAsync(key); + + public async Task Set(string key, string value) + { + DistributedCacheEntryOptions options = new DistributedCacheEntryOptions() + .SetAbsoluteExpiration(DateTime.Now.AddMinutes(5)) + .SetSlidingExpiration(TimeSpan.FromMinutes(3)); + var dataToCache = Encoding.UTF8.GetBytes(value); + await _cache.SetAsync(key, dataToCache, options); + return true; + } + + public async Task Get(string key) + =>await _cache.GetStringAsync(key); +} diff --git a/src/SwiftLink.Infrastructure/Context/ApplicationDbContext.cs b/src/SwiftLink.Infrastructure/Context/ApplicationDbContext.cs index cd8960e..483fa38 100644 --- a/src/SwiftLink.Infrastructure/Context/ApplicationDbContext.cs +++ b/src/SwiftLink.Infrastructure/Context/ApplicationDbContext.cs @@ -38,5 +38,4 @@ public async Task SaveChangesAsync() return Result.Failure(ex.InnerException.Message); } } - } diff --git a/src/SwiftLink.Infrastructure/SwiftLink.Infrastructure.csproj b/src/SwiftLink.Infrastructure/SwiftLink.Infrastructure.csproj index ea38d59..86223eb 100644 --- a/src/SwiftLink.Infrastructure/SwiftLink.Infrastructure.csproj +++ b/src/SwiftLink.Infrastructure/SwiftLink.Infrastructure.csproj @@ -8,6 +8,7 @@ + diff --git a/src/SwiftLink.Presentation/Program.cs b/src/SwiftLink.Presentation/Program.cs index e8cc1b4..baed13b 100644 --- a/src/SwiftLink.Presentation/Program.cs +++ b/src/SwiftLink.Presentation/Program.cs @@ -8,6 +8,11 @@ builder.Services.AddOptions() .Bind(builder.Configuration.GetSection(AppSettings.ConfigurationSectionName)) .ValidateDataAnnotations(); + + builder.Services.AddStackExchangeRedisCache(options => + { + options.Configuration = builder.Configuration["AppSettings:RedisCacheUrl"]; + }); } var app = builder.Build(); diff --git a/src/SwiftLink.Presentation/appsettings.Development.json b/src/SwiftLink.Presentation/appsettings.Development.json index 15973f1..7350e92 100644 --- a/src/SwiftLink.Presentation/appsettings.Development.json +++ b/src/SwiftLink.Presentation/appsettings.Development.json @@ -1,6 +1,7 @@ { "AppSettings": { - "DefaultUrlOnNotFound": "https://agah.com/" + "DefaultUrlOnNotFound": "https://agah.com/", + "RedisCacheUrl": "127.0.0.1:6379" }, "Logging": { "LogLevel": { diff --git a/src/SwiftLink.Presentation/appsettings.json b/src/SwiftLink.Presentation/appsettings.json index 3e2a4da..6aea40c 100644 --- a/src/SwiftLink.Presentation/appsettings.json +++ b/src/SwiftLink.Presentation/appsettings.json @@ -1,6 +1,7 @@ { "AppSettings": { - "DefaultUrlOnNotFound": "https://agah.com/" + "DefaultUrlOnNotFound": "https://agah.com/", + "RedisCacheUrl": "127.0.0.1:6379" }, "Logging": { "LogLevel": { diff --git a/src/SwiftLink.Shared/AppSettings.cs b/src/SwiftLink.Shared/AppSettings.cs index ff88ee2..26b5efa 100644 --- a/src/SwiftLink.Shared/AppSettings.cs +++ b/src/SwiftLink.Shared/AppSettings.cs @@ -7,4 +7,7 @@ public record AppSettings [Url] public string DefaultUrlOnNotFound { get; set; } + + [Url] + public string RedisCacheUrl { get; set; } }