-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Cache provider with Redis,
- Loading branch information
1 parent
9203c69
commit e59d905
Showing
8 changed files
with
44 additions
and
10 deletions.
There are no files selected for viewing
13 changes: 6 additions & 7 deletions
13
src/SwiftLink.Application/Common/Interfaces/ICacheProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
src/SwiftLink.Infrastructure/CacheProvider/RedisCacheProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,5 +38,4 @@ public async Task<Result> SaveChangesAsync() | |
return Result.Failure(ex.InnerException.Message); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters