Skip to content

Commit

Permalink
Create setasync.md
Browse files Browse the repository at this point in the history
  • Loading branch information
gsilvamartin authored Feb 25, 2024
1 parent 047d56a commit ec4e8aa
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions docs/docs/serviceabstractions/cache/redis/setasync.md
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.

0 comments on commit ec4e8aa

Please sign in to comment.