-
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.
- Loading branch information
1 parent
047d56a
commit ec4e8aa
Showing
1 changed file
with
40 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# SetAsync Method | ||
|
||
The `SetAsync` method is part of the `IRedisRepository<T>` interface in the `NetPlus.ServiceAbstractions.Cache.Redis` library. This method stores an object in the Redis cache asynchronously with the specified key. | ||
|
||
## Syntax | ||
|
||
```csharp | ||
public async Task<bool> SetAsync(string key, T value, TimeSpan? expiry = null) | ||
``` | ||
|
||
## Parameters | ||
|
||
- `key`: The unique identifier for the object in the Redis cache. This is a string. | ||
- `value`: The object to be stored in the Redis cache. This is of type `T`. | ||
- `expiry`: The optional expiration time for the stored object in the Redis cache. If null, the object won't expire. This is of type `TimeSpan?`. | ||
|
||
## Returns | ||
|
||
A task representing the asynchronous operation and containing a boolean indicating if the object was successfully stored. | ||
|
||
## Example | ||
|
||
```csharp | ||
var repository = serviceProvider.GetService<IRedisRepository<MyClass>>(); | ||
var myObject = new MyClass { Property1 = "Value1", Property2 = "Value2" }; | ||
bool success = await repository.SetAsync("myKey", myObject, TimeSpan.FromHours(1)); | ||
``` | ||
|
||
In this example, `myObject` will be stored in the Redis cache with the key "myKey" and an expiration time of 1 hour. The `success` variable will be `true` if the object was successfully stored, and `false` otherwise. | ||
|
||
## Remarks | ||
|
||
> [!NOTE] | ||
> The `SetAsync` method uses the `StringSetAsync` method from the `IDatabase` interface provided by the StackExchange.Redis library to store the value in Redis. It serializes the object of type `T` into a string using the `JsonConvert.SerializeObject` method from the Newtonsoft.Json library before storing it. | ||
> [!TIP] | ||
> If the `expiry` parameter is not provided or is null, the object will be stored in the Redis cache indefinitely, until it is manually removed or the Redis server evicts it due to memory pressure. | ||
> [!WARNING] | ||
> Be aware that the `SetAsync` method will throw an exception if the Redis server is not available or if there is a network issue. Make sure to handle these exceptions in your code. |