Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor SERedis.BaseSetAll to set all values at once #395

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions src/EasyCaching.Redis/DefaultRedisCachingProvider.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,20 @@ public override async Task BaseSetAllAsync<T>(IDictionary<string, T> values, Tim
{
ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration));
ArgumentCheck.NotNullAndCountGTZero(values, nameof(values));

var tasks = new List<Task>();

foreach (var item in values)
tasks.Add(SetAsync(item.Key, item.Value, expiration, cancellationToken));

await Task.WhenAll(tasks);

var keyValuePairs = values
.ToDictionary(
keySelector: item => (RedisKey)item.Key,
elementSelector: item => (RedisValue)_serializer.Serialize(item.Value))
.ToArray();

ITransaction transaction = _cache.CreateTransaction();

_ = transaction.StringSetAsync(keyValuePairs);
foreach (RedisKey key in keyValuePairs.Select(keyVp => keyVp.Key))
_ = transaction.KeyExpireAsync(key, expiration);

await transaction.ExecuteAsync();
}

/// <summary>
Expand Down
21 changes: 14 additions & 7 deletions src/EasyCaching.Redis/DefaultRedisCachingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,13 +367,20 @@ public override void BaseSetAll<T>(IDictionary<string, T> values, TimeSpan expir
{
ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration));
ArgumentCheck.NotNullAndCountGTZero(values, nameof(values));

var batch = _cache.CreateBatch();

foreach (var item in values)
batch.StringSetAsync(item.Key, _serializer.Serialize(item.Value), expiration);

batch.Execute();

var keyValuePairs = values
.ToDictionary(
keySelector: item => (RedisKey)item.Key,
elementSelector: item => (RedisValue)_serializer.Serialize(item.Value))
.ToArray();

ITransaction transaction = _cache.CreateTransaction();

transaction.StringSetAsync(keyValuePairs);
foreach (RedisKey key in keyValuePairs.Select(keyVp => keyVp.Key))
transaction.KeyExpireAsync(key, expiration);

transaction.Execute();
}

/// <summary>
Expand Down