-
Notifications
You must be signed in to change notification settings - Fork 3
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 #853 from DFE-Digital/feat/234422/initial-redis-in…
…tegration feat: Initial redis integration
- Loading branch information
Showing
38 changed files
with
1,789 additions
and
36 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
100 changes: 100 additions & 0 deletions
100
src/Dfe.PlanTech.Application/Caching/Interfaces/IDistributedCache.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,100 @@ | ||
namespace Dfe.PlanTech.Application.Caching.Interfaces; | ||
|
||
/// <summary> | ||
/// Represents a distributed cache interface for managing cache items. | ||
/// </summary> | ||
public interface IDistributedCache | ||
{ | ||
/// <summary> | ||
/// Gets or creates a cache item asynchronously. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the cache item.</typeparam> | ||
/// <param name="key">The key of the cache item.</param> | ||
/// <param name="action">The function to create the cache item if it does not exist.</param> | ||
/// <param name="expiry">The optional expiration time for the cache item.</param> | ||
/// <param name="onCacheItemCreation">An optional callback to invoke after the cache item is created.</param> | ||
/// <param name="databaseId">The optional database identifier.</param> | ||
/// <returns>The cached item or default value if not found.</returns> | ||
Task<T?> GetOrCreateAsync<T>(string key, Func<Task<T>> action, TimeSpan? expiry = null, Func<T, Task>? onCacheItemCreation = null, int databaseId = -1); | ||
|
||
/// <summary> | ||
/// Sets a cache item asynchronously. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the cache item.</typeparam> | ||
/// <param name="key">The key of the cache item.</param> | ||
/// <param name="value">The value of the cache item.</param> | ||
/// <param name="expiry">The optional expiration time for the cache item.</param> | ||
/// <param name="databaseId">The optional database identifier.</param> | ||
/// <returns>The key of the cache item.</returns> | ||
Task<string> SetAsync<T>(string key, T value, TimeSpan? expiry = null, int databaseId = -1); | ||
|
||
/// <summary> | ||
/// Removes a cache item asynchronously. | ||
/// </summary> | ||
/// <param name="key">The key of the cache item.</param> | ||
/// <param name="databaseId">The optional database identifier.</param> | ||
/// <returns>True if the item was removed; otherwise, false.</returns> | ||
Task<bool> RemoveAsync(string key, int databaseId = -1); | ||
|
||
/// <summary> | ||
/// Removes multiple cache items asynchronously. | ||
/// </summary> | ||
/// <param name="keys">The keys of the cache items.</param> | ||
Task RemoveAsync(params string[] keys); | ||
|
||
/// <summary> | ||
/// Removes multiple cache items from a specified database asynchronously. | ||
/// </summary> | ||
/// <param name="databaseId">The database identifier.</param> | ||
/// <param name="keys">The keys of the cache items.</param> | ||
Task RemoveAsync(int databaseId, params string[] keys); | ||
|
||
/// <summary> | ||
/// Appends an item to a cache entry asynchronously. | ||
/// </summary> | ||
/// <param name="key">The key of the cache item.</param> | ||
/// <param name="item">The item to append.</param> | ||
/// <param name="databaseId">The optional database identifier.</param> | ||
Task AppendAsync(string key, string item, int databaseId = -1); | ||
|
||
/// <summary> | ||
/// Retrieves a cache item asynchronously. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the cache item.</typeparam> | ||
/// <param name="key">The key of the cache item.</param> | ||
/// <param name="databaseId">The optional database identifier.</param> | ||
/// <returns>The cached item or default value if not found.</returns> | ||
Task<T?> GetAsync<T>(string key, int databaseId = -1); | ||
|
||
/// <summary> | ||
/// Adds an item to a set in the cache asynchronously. | ||
/// </summary> | ||
/// <param name="key">The key of the set.</param> | ||
/// <param name="item">The item to add.</param> | ||
/// <param name="databaseId">The optional database identifier.</param> | ||
Task SetAddAsync(string key, string item, int databaseId = -1); | ||
|
||
/// <summary> | ||
/// Gets the members of a set from the cache asynchronously. | ||
/// </summary> | ||
/// <param name="key">The key of the set.</param> | ||
/// <param name="databaseId">The optional database identifier.</param> | ||
/// <returns>An array of set members.</returns> | ||
Task<string[]> GetSetMembersAsync(string key, int databaseId = -1); | ||
|
||
/// <summary> | ||
/// Removes an item from a set in the cache asynchronously. | ||
/// </summary> | ||
/// <param name="key">The key of the set.</param> | ||
/// <param name="item">The item to remove.</param> | ||
/// <param name="databaseId">The optional database identifier.</param> | ||
Task SetRemoveAsync(string key, string item, int databaseId = -1); | ||
|
||
/// <summary> | ||
/// Removes multiple items from a set in the cache asynchronously. | ||
/// </summary> | ||
/// <param name="key">The key of the set.</param> | ||
/// <param name="items">The items to remove.</param> | ||
/// <param name="databaseId">The optional database identifier.</param> | ||
Task SetRemoveItemsAsync(string key, string[] items, int databaseId = -1); | ||
} |
51 changes: 51 additions & 0 deletions
51
src/Dfe.PlanTech.Application/Caching/Interfaces/IDistributedLockProvider.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,51 @@ | ||
namespace Dfe.PlanTech.Application.Caching.Interfaces; | ||
|
||
/// <summary> | ||
/// Provides distributed locking functionality | ||
/// </summary> | ||
public interface IDistributedLockProvider | ||
{ | ||
/// <summary> | ||
/// Releases the lock for the specified key and lock value | ||
/// </summary> | ||
/// <param name="key">The key associated with the lock.</param> | ||
/// <param name="lockValue">The lock value used to acquire the lock.</param> | ||
/// <param name="databaseId">The Redis database ID to use. Defaults to -1, which means using the default database.</param> | ||
/// <returns>True if the lock was successfully released; otherwise, false.</returns> | ||
Task<bool> LockReleaseAsync(string key, string lockValue, int databaseId = -1); | ||
|
||
/// <summary> | ||
/// Extends the expiration of a distributed lock for the specified key and lock value. | ||
/// </summary> | ||
/// <param name="key">The key associated with the lock.</param> | ||
/// <param name="lockValue">The lock value used to acquire the lock.</param> | ||
/// <param name="duration">The new duration to set for the lock's expiration.</param> | ||
/// <param name="databaseId">The Redis database ID to use. Defaults to -1, which means using the default database.</param> | ||
/// <returns>True if the lock was successfully extended; otherwise, false.</returns> | ||
Task<bool> LockExtendAsync(string key, string lockValue, TimeSpan duration, int databaseId = -1); | ||
|
||
/// <summary> | ||
/// Waits for a distributed lock to be acquired. | ||
/// </summary> | ||
/// <param name="key">The key associated with the lock.</param> | ||
/// <param name="throwExceptionIfLockNotAcquired">Indicates whether an exception should be thrown if the lock is not acquired.</param> | ||
/// <returns>The lock value if the lock was acquired; otherwise, null.</returns> | ||
Task<string?> WaitForLockAsync(string key, bool throwExceptionIfLockNotAcquired = true); | ||
|
||
/// <summary> | ||
/// Executes an operation with a distributed lock. | ||
/// </summary> | ||
/// <param name="key">The key associated with the lock.</param> | ||
/// <param name="runWithLock">The action to execute while holding the lock.</param> | ||
/// <param name="databaseId">The Redis database ID to use. Defaults to -1, which means using the default database.</param> | ||
Task LockAndRun(string key, Func<Task> runWithLock, int databaseId = -1); | ||
|
||
/// <summary> | ||
/// Executes an operation with a distributed lock and returns the result. | ||
/// </summary> | ||
/// <param name="key">The key associated with the lock.</param> | ||
/// <param name="runWithLock">The function to execute while holding the lock.</param> | ||
/// <param name="databaseId">The Redis database ID to use. Defaults to -1, which means using the default database.</param> | ||
/// <returns>The result of the operation if the lock was acquired; otherwise, the default value.</returns> | ||
Task<T?> LockAndGet<T>(string key, Func<Task<T>> runWithLock, int databaseId = -1); | ||
} |
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,6 @@ | ||
namespace Dfe.PlanTech.Domain.Caching.Exceptions; | ||
|
||
public class GuardException(string message) : Exception(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Dfe.PlanTech.Domain.Caching.Models; | ||
|
||
internal static class CacheKey | ||
{ | ||
public static string Make(string name, string subname, params object[] items) => $"{name}.{subname}({string.Join(',', items.Select(x => x?.ToString()))})"; | ||
} |
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,3 @@ | ||
namespace Dfe.PlanTech.Domain.Caching.Models; | ||
|
||
public record CacheResult<T>(bool? ExistedInCache = null, T? CacheValue = default, string? Error = null); |
Oops, something went wrong.