-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from akazad13/feature/issue-61-add-redis-cache
Issue #61: Add Redis cache
- Loading branch information
Showing
17 changed files
with
190 additions
and
38 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
namespace Shopizy.Application.Common.Caching; | ||
|
||
/// <summary> | ||
/// Provides methods for caching operations. | ||
/// </summary> | ||
public interface ICacheHelper | ||
{ | ||
/// <summary> | ||
/// Retrieves a cached item by its key. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the cached item.</typeparam> | ||
/// <param name="key">The key of the cached item.</param> | ||
/// <returns>A task that represents the asynchronous operation. The task result contains the cached item.</returns> | ||
Task<T> GetAsync<T>(string key); | ||
|
||
/// <summary> | ||
/// Adds or updates a cached item with the specified key and value. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the item to cache.</typeparam> | ||
/// <param name="key">The key of the cached item.</param> | ||
/// <param name="value">The value of the item to cache.</param> | ||
/// <param name="expiration">The optional expiration time for the cached item.</param> | ||
/// <returns>A task that represents the asynchronous operation.</returns> | ||
Task SetAsync<T>(string key, T value, TimeSpan? expiration = null); | ||
|
||
/// <summary> | ||
/// Removes a cached item by its key. | ||
/// </summary> | ||
/// <param name="key">The key of the cached item to remove.</param> | ||
/// <returns>A task that represents the asynchronous operation.</returns> | ||
Task RemoveAsync(string key); | ||
} |
67 changes: 67 additions & 0 deletions
67
src/Shopizy.Application/Common/Caching/RedisCacheHelper.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,67 @@ | ||
using System.Text.Json; | ||
using Microsoft.Extensions.Options; | ||
using StackExchange.Redis; | ||
|
||
namespace Shopizy.Application.Common.Caching; | ||
|
||
/// <summary> | ||
/// Helper class for interacting with Redis cache. | ||
/// </summary> | ||
public class RedisCacheHelper : ICacheHelper | ||
{ | ||
private readonly RedisSettings _redisSettings; | ||
private readonly Lazy<ConnectionMultiplexer> _redisDbConnectionLazy; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="RedisCacheHelper"/> class. | ||
/// </summary> | ||
/// <param name="redisSettingsOptions">The Redis settings options.</param> | ||
public RedisCacheHelper(IOptions<RedisSettings> redisSettingsOptions) | ||
{ | ||
_redisSettings = redisSettingsOptions.Value; | ||
_redisDbConnectionLazy = new Lazy<ConnectionMultiplexer>( | ||
() => | ||
ConnectionMultiplexer.Connect( | ||
string.Format(_redisSettings.ConnectionString, _redisSettings.Password) | ||
), | ||
LazyThreadSafetyMode.ExecutionAndPublication | ||
); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the cached value for the specified key. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the cached value.</typeparam> | ||
/// <param name="key">The cache key.</param> | ||
/// <returns>The cached value, or default if the key does not exist.</returns> | ||
public async Task<T> GetAsync<T>(string key) | ||
{ | ||
var db = _redisDbConnectionLazy.Value.GetDatabase(); | ||
var data = await db.StringGetAsync(key); | ||
|
||
return data.HasValue ? JsonSerializer.Deserialize<T>(data) : default; | ||
} | ||
|
||
/// <summary> | ||
/// Sets the specified value in the cache with the specified key. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the value to cache.</typeparam> | ||
/// <param name="key">The cache key.</param> | ||
/// <param name="value">The value to cache.</param> | ||
/// <param name="expiration">The expiration time for the cached value. If null, the value does not expire.</param> | ||
public async Task SetAsync<T>(string key, T value, TimeSpan? expiration = null) | ||
{ | ||
var db = _redisDbConnectionLazy.Value.GetDatabase(); | ||
await db.StringSetAsync(key, JsonSerializer.Serialize(value), expiration); | ||
} | ||
|
||
/// <summary> | ||
/// Removes the cached value for the specified key. | ||
/// </summary> | ||
/// <param name="key">The cache key.</param> | ||
public async Task RemoveAsync(string key) | ||
{ | ||
var db = _redisDbConnectionLazy.Value.GetDatabase(); | ||
await db.KeyDeleteAsync(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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Shopizy.Application.Common.Caching; | ||
|
||
public class RedisSettings | ||
{ | ||
public const string Section = "RedisCacheSettings"; | ||
public string ConnectionString { get; set; } = string.Empty; | ||
public string Password { get; set; } = string.Empty; | ||
} |
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
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
Oops, something went wrong.