Skip to content

Commit

Permalink
Merge pull request #21322 from abpframework/redispatch
Browse files Browse the repository at this point in the history
Always use pipelines to operate Redis
  • Loading branch information
maliming authored Nov 13, 2024
2 parents 9ebf579 + 9643d35 commit a745e7f
Showing 1 changed file with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ protected virtual async ValueTask<IDatabase> ConnectAsync(CancellationToken toke
return await (ValueTask<IDatabase>)ConnectAsyncMethod.Invoke(this, new object[] { token })!;
}

public byte[]?[] GetMany(
public virtual byte[]?[] GetMany(
IEnumerable<string> keys)
{
keys = Check.NotNull(keys, nameof(keys));

return GetAndRefreshMany(keys, true);
}

public async Task<byte[]?[]> GetManyAsync(
public virtual async Task<byte[]?[]> GetManyAsync(
IEnumerable<string> keys,
CancellationToken token = default)
{
Expand All @@ -104,7 +104,7 @@ protected virtual async ValueTask<IDatabase> ConnectAsync(CancellationToken toke
return await GetAndRefreshManyAsync(keys, true, token);
}

public void SetMany(
public virtual void SetMany(
IEnumerable<KeyValuePair<string, byte[]>> items,
DistributedCacheEntryOptions options)
{
Expand All @@ -121,7 +121,7 @@ public void SetMany(
}
}

public async Task SetManyAsync(
public virtual async Task SetManyAsync(
IEnumerable<KeyValuePair<string, byte[]>> items,
DistributedCacheEntryOptions options,
CancellationToken token = default)
Expand All @@ -141,15 +141,15 @@ public async Task SetManyAsync(
}
}

public void RefreshMany(
public virtual void RefreshMany(
IEnumerable<string> keys)
{
keys = Check.NotNull(keys, nameof(keys));

GetAndRefreshMany(keys, false);
}

public async Task RefreshManyAsync(
public virtual async Task RefreshManyAsync(
IEnumerable<string> keys,
CancellationToken token = default)
{
Expand All @@ -158,15 +158,15 @@ public async Task RefreshManyAsync(
await GetAndRefreshManyAsync(keys, false, token);
}

public void RemoveMany(IEnumerable<string> keys)
public virtual void RemoveMany(IEnumerable<string> keys)
{
keys = Check.NotNull(keys, nameof(keys));

var cache = Connect();

try
{
cache.KeyDelete(keys.Select(key => InstancePrefix.Append(key)).ToArray());
Task.WaitAll(PipelineRemoveManyAsync(cache, keys));
}
catch (Exception ex)
{
Expand All @@ -175,7 +175,7 @@ public void RemoveMany(IEnumerable<string> keys)
}
}

public async Task RemoveManyAsync(IEnumerable<string> keys, CancellationToken token = default)
public virtual async Task RemoveManyAsync(IEnumerable<string> keys, CancellationToken token = default)
{
keys = Check.NotNull(keys, nameof(keys));

Expand All @@ -184,14 +184,19 @@ public async Task RemoveManyAsync(IEnumerable<string> keys, CancellationToken to

try
{
await cache.KeyDeleteAsync(keys.Select(key => InstancePrefix.Append(key)).ToArray());
await Task.WhenAll(PipelineRemoveManyAsync(cache, keys));
}
catch (Exception ex)
{
OnRedisError(ex, cache);
throw;
}
}

protected virtual Task[] PipelineRemoveManyAsync(IDatabase cache, IEnumerable<string> keys)
{
return keys.Select(key => cache.KeyDeleteAsync(InstancePrefix.Append(key))).ToArray<Task>();
}

protected virtual byte[]?[] GetAndRefreshMany(
IEnumerable<string> keys,
Expand Down

0 comments on commit a745e7f

Please sign in to comment.