Skip to content

Commit

Permalink
Implement Cache provider with Redis,
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammadKarimi committed Jan 9, 2024
1 parent 9203c69 commit e59d905
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 10 deletions.
13 changes: 6 additions & 7 deletions src/SwiftLink.Application/Common/Interfaces/ICacheProvider.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
namespace SwiftLink.Application.Common.Interfaces;

internal interface ICacheProvider
public interface ICacheProvider
{
/// <summary>
/// Associate a value with a key in the Cache such as Redis.
/// </summary>
/// <param name="key">The key of the entry to add.</param>
/// <param name="value">The value to associate with the key.</param>
/// <returns>The value that was set.</returns>
public string Set(string key, string value);
/// <returns>The value that was set or not.</returns>
public Task<bool> Set(string key, string value);

/// <summary>
/// Removes the object associated with the given key.
/// </summary>
/// <param name="key">An object identifying the requested entry.</param>
void Remove(string key);
Task Remove(string key);

/// <summary>
/// Gets the item associated with this key if present.
/// </summary>
/// <param name="key">An object identifying the requested entry.</param>
/// <param name="value">The located value or default url.</param>
/// <returns></returns>
bool TryGetValue(string key, out string value);
/// <returns>The located value or default url</returns>
Task<string> Get(string key);
}
25 changes: 25 additions & 0 deletions src/SwiftLink.Infrastructure/CacheProvider/RedisCacheProvider.cs
Original file line number Diff line number Diff line change
@@ -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<bool> 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<string> Get(string key)
=>await _cache.GetStringAsync(key);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,4 @@ public async Task<Result> SaveChangesAsync()
return Result.Failure(ex.InnerException.Message);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
5 changes: 5 additions & 0 deletions src/SwiftLink.Presentation/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
builder.Services.AddOptions<AppSettings>()
.Bind(builder.Configuration.GetSection(AppSettings.ConfigurationSectionName))
.ValidateDataAnnotations();

builder.Services.AddStackExchangeRedisCache(options =>
{
options.Configuration = builder.Configuration["AppSettings:RedisCacheUrl"];
});
}

var app = builder.Build();
Expand Down
3 changes: 2 additions & 1 deletion src/SwiftLink.Presentation/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"AppSettings": {
"DefaultUrlOnNotFound": "https://agah.com/"
"DefaultUrlOnNotFound": "https://agah.com/",
"RedisCacheUrl": "127.0.0.1:6379"
},
"Logging": {
"LogLevel": {
Expand Down
3 changes: 2 additions & 1 deletion src/SwiftLink.Presentation/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"AppSettings": {
"DefaultUrlOnNotFound": "https://agah.com/"
"DefaultUrlOnNotFound": "https://agah.com/",
"RedisCacheUrl": "127.0.0.1:6379"
},
"Logging": {
"LogLevel": {
Expand Down
3 changes: 3 additions & 0 deletions src/SwiftLink.Shared/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ public record AppSettings

[Url]
public string DefaultUrlOnNotFound { get; set; }

[Url]
public string RedisCacheUrl { get; set; }
}

0 comments on commit e59d905

Please sign in to comment.