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

Micro refactoring #36

Merged
merged 4 commits into from
Nov 11, 2021
Merged
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
20 changes: 10 additions & 10 deletions src/CloudStructures/Internals/RedisOperationHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ internal static class RedisOperationHelpers
/// Execute specified command with expiration time.
/// </summary>
/// <typeparam name="TRedis"></typeparam>
/// <typeparam name="TArgs"></typeparam>
/// <typeparam name="TState"></typeparam>
/// <param name="structure"></param>
/// <param name="command"></param>
/// <param name="args"></param>
/// <param name="state"></param>
/// <param name="expiry"></param>
/// <param name="flags"></param>
/// <returns></returns>
public static async Task ExecuteWithExpiryAsync<TRedis, TArgs>(this TRedis structure, Func<IDatabaseAsync, TArgs, Task> command, TArgs args, TimeSpan? expiry, CommandFlags flags)
public static async Task ExecuteWithExpiryAsync<TRedis, TState>(this TRedis structure, Func<IDatabaseAsync, TState, Task> command, TState state, TimeSpan? expiry, CommandFlags flags)
where TRedis : IRedisStructure
{
if (expiry.HasValue)
{
//--- Execute multiple commands in tracsaction
var t = structure.Connection.Transaction;
_ = command(t, args); // forget
_ = command(t, state); // forget
_ = t.KeyExpireAsync(structure.Key, expiry.Value, flags); // forget

//--- commit
Expand All @@ -39,7 +39,7 @@ public static async Task ExecuteWithExpiryAsync<TRedis, TArgs>(this TRedis struc
else
{
var database = structure.Connection.Database;
await command(database, args).ConfigureAwait(false);
await command(database, state).ConfigureAwait(false);
}
}

Expand All @@ -48,22 +48,22 @@ public static async Task ExecuteWithExpiryAsync<TRedis, TArgs>(this TRedis struc
/// Execute specified command with expiration time.
/// </summary>
/// <typeparam name="TRedis"></typeparam>
/// <typeparam name="TArgs"></typeparam>
/// <typeparam name="TState"></typeparam>
/// <typeparam name="TResult"></typeparam>
/// <param name="structure"></param>
/// <param name="command"></param>
/// <param name="args"></param>
/// <param name="state"></param>
/// <param name="expiry"></param>
/// <param name="flags"></param>
/// <returns></returns>
public static async Task<TResult> ExecuteWithExpiryAsync<TRedis, TArgs, TResult>(this TRedis structure, Func<IDatabaseAsync, TArgs, Task<TResult>> command, TArgs args, TimeSpan? expiry, CommandFlags flags)
public static async Task<TResult> ExecuteWithExpiryAsync<TRedis, TState, TResult>(this TRedis structure, Func<IDatabaseAsync, TState, Task<TResult>> command, TState state, TimeSpan? expiry, CommandFlags flags)
where TRedis : IRedisStructure
{
if (expiry.HasValue)
{
//--- Execute multiple commands in tracsaction
var t = structure.Connection.Transaction;
var result = command(t, args);
var result = command(t, state);
_ = t.KeyExpireAsync(structure.Key, expiry.Value, flags); // forget

//--- commit
Expand All @@ -75,7 +75,7 @@ public static async Task<TResult> ExecuteWithExpiryAsync<TRedis, TArgs, TResult>
else
{
var database = structure.Connection.Database;
return await command(database, args).ConfigureAwait(false);
return await command(database, state).ConfigureAwait(false);
}
}
}
2 changes: 1 addition & 1 deletion src/CloudStructures/RedisConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace CloudStructures;
/// <summary>
/// Represents connection configuration.
/// </summary>
public class RedisConfig
public sealed class RedisConfig
{
#region Properties
/// <summary>
Expand Down
30 changes: 15 additions & 15 deletions src/CloudStructures/RedisConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ internal ITransaction Transaction
internal IServer[] Servers
=> this.Config.Options
.EndPoints
.Select(this.GetConnection(), (x, c) => c.GetServer(x))
.Select(this.GetConnection(), static (x, c) => c.GetServer(x))
.ToArray();
#endregion

Expand Down Expand Up @@ -92,37 +92,37 @@ public RedisConnection(RedisConfig config, IValueConverter? converter = null, IC
/// <returns></returns>
public ConnectionMultiplexer GetConnection()
{
lock (this.gate)
lock (this._gate)
{
if (this.connection is null || !this.connection.IsConnected)
if (this._connection is null || !this._connection.IsConnected)
{
try
{
//--- create inner connection
var stopwatch = Stopwatch.StartNew();
this.connection = ConnectionMultiplexer.Connect(this.Config.Options, this.Logger);
this._connection = ConnectionMultiplexer.Connect(this.Config.Options, this.Logger);
stopwatch.Stop();
this.Handler?.OnConnectionOpened(this, new(stopwatch.Elapsed));

//--- attach events
this.connection.ConfigurationChanged += (_, e) => this.Handler?.OnConfigurationChanged(this, e);
this.connection.ConfigurationChangedBroadcast += (_, e) => this.Handler?.OnConfigurationChangedBroadcast(this, e);
this.connection.ConnectionFailed += (_, e) => this.Handler?.OnConnectionFailed(this, e);
this.connection.ConnectionRestored += (_, e) => this.Handler?.OnConnectionRestored(this, e);
this.connection.ErrorMessage += (_, e) => this.Handler?.OnErrorMessage(this, e);
this.connection.HashSlotMoved += (_, e) => this.Handler?.OnHashSlotMoved(this, e);
this.connection.InternalError += (_, e) => this.Handler?.OnInternalError(this, e);
this._connection.ConfigurationChanged += (_, e) => this.Handler?.OnConfigurationChanged(this, e);
this._connection.ConfigurationChangedBroadcast += (_, e) => this.Handler?.OnConfigurationChangedBroadcast(this, e);
this._connection.ConnectionFailed += (_, e) => this.Handler?.OnConnectionFailed(this, e);
this._connection.ConnectionRestored += (_, e) => this.Handler?.OnConnectionRestored(this, e);
this._connection.ErrorMessage += (_, e) => this.Handler?.OnErrorMessage(this, e);
this._connection.HashSlotMoved += (_, e) => this.Handler?.OnHashSlotMoved(this, e);
this._connection.InternalError += (_, e) => this.Handler?.OnInternalError(this, e);
}
catch
{
this.connection = null;
this._connection = null;
throw;
}
}
return this.connection;
return this._connection;
}
}
private readonly object gate = new();
private ConnectionMultiplexer? connection = null;
private readonly object _gate = new();
private ConnectionMultiplexer? _connection = null;
#endregion
}
6 changes: 3 additions & 3 deletions src/CloudStructures/Structures/RedisBit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public Task<long> OperationAsync(Bitwise operation, IReadOnlyCollection<RedisBit
if (bits.Count == 0)
throw new ArgumentException("bits length is 0.");

var keys = bits.Select(x => x.Key).ToArray();
var keys = bits.Select(static x => x.Key).ToArray();
return this.Connection.Database.StringBitOperationAsync(operation, this.Key, keys, flags);
}

Expand All @@ -111,8 +111,8 @@ public Task<bool> SetAsync(long offset, bool bit, TimeSpan? expiry = null, Comma
expiry ??= this.DefaultExpiry;
return this.ExecuteWithExpiryAsync
(
(db, a) => db.StringSetBitAsync(a.key, a.offset, a.bit, a.flags),
(key: this.Key, offset, bit, flags),
static (db, state) => db.StringSetBitAsync(state.key, state.offset, state.bit, state.flags),
state: (key: this.Key, offset, bit, flags),
expiry,
flags
);
Expand Down
70 changes: 36 additions & 34 deletions src/CloudStructures/Structures/RedisDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public Task<long> DecrementAsync(TKey field, long value = 1, TimeSpan? expiry =
var hashField = this.Connection.Converter.Serialize(field);
return this.ExecuteWithExpiryAsync
(
(db, a) => db.HashDecrementAsync(a.key, a.hashField, a.value, a.flags),
(key: this.Key, hashField, value, flags),
static (db, state) => db.HashDecrementAsync(state.key, state.hashField, state.value, state.flags),
state: (key: this.Key, hashField, value, flags),
expiry,
flags
);
Expand All @@ -92,8 +92,8 @@ public Task DecrementAsync(TKey field, double value, TimeSpan? expiry = null, Co
var hashField = this.Connection.Converter.Serialize(field);
return this.ExecuteWithExpiryAsync
(
(db, a) => db.HashDecrementAsync(a.key, a.hashField, a.value, a.flags),
(key: this.Key, hashField, value, flags),
static (db, state) => db.HashDecrementAsync(state.key, state.hashField, state.value, state.flags),
state: (key: this.Key, hashField, value, flags),
expiry,
flags
);
Expand Down Expand Up @@ -138,13 +138,13 @@ public async Task<Dictionary<TKey, TValue>> GetAllAsync(IEqualityComparer<TKey>?
var comparer = dictionaryEqualityComparer ?? EqualityComparer<TKey>.Default;
var entries = await this.Connection.Database.HashGetAllAsync(this.Key, flags).ConfigureAwait(false);
return entries
.Select(this.Connection.Converter, (x, c) =>
.Select(this.Connection.Converter, static (x, c) =>
{
var field = c.Deserialize<TKey>(x.Name);
var value = c.Deserialize<TValue>(x.Value);
return (field, value);
})
.ToDictionary(x => x.field, x => x.value, comparer);
.ToDictionary(static x => x.field, static x => x.value, comparer);
}


Expand All @@ -169,14 +169,14 @@ public async Task<Dictionary<TKey, TValue>> GetAsync(IEnumerable<TKey> fields, I
var hashFields = fields.Select(this.Connection.Converter.Serialize).ToArray();
var values = await this.Connection.Database.HashGetAsync(this.Key, hashFields, flags).ConfigureAwait(false);
return fields
.Zip(values, (f, v) => (field: f, value: v))
.Select(this.Connection.Converter, (x, c) =>
.Zip(values, static (f, v) => (field: f, value: v))
.Select(this.Connection.Converter, static (x, c) =>
{
var result = x.value.ToResult<TValue>(c);
return (x.field, result);
})
.Where(x => x.result.HasValue)
.ToDictionary(x => x.field, x => x.result.Value, comparer);
.Where(static x => x.result.HasValue)
.ToDictionary(static x => x.field, static x => x.result.Value, comparer);
}


Expand All @@ -189,8 +189,8 @@ public Task<long> IncrementAsync(TKey field, long value = 1, TimeSpan? expiry =
var hashField = this.Connection.Converter.Serialize(field);
return this.ExecuteWithExpiryAsync
(
(db, a) => db.HashIncrementAsync(a.key, a.hashField, a.value, a.flags),
(key: this.Key, hashField, value, flags),
static (db, state) => db.HashIncrementAsync(state.key, state.hashField, state.value, state.flags),
state: (key: this.Key, hashField, value, flags),
expiry,
flags
);
Expand All @@ -206,8 +206,8 @@ public Task IncrementAsync(TKey field, double value, TimeSpan? expiry = null, Co
var hashField = this.Connection.Converter.Serialize(field);
return this.ExecuteWithExpiryAsync
(
(db, a) => db.HashIncrementAsync(a.key, a.hashField, a.value, a.flags),
(key: this.Key, hashField, value, flags),
static (db, state) => db.HashIncrementAsync(state.key, state.hashField, state.value, state.flags),
state: (key: this.Key, hashField, value, flags),
expiry,
flags
);
Expand All @@ -220,7 +220,7 @@ public Task IncrementAsync(TKey field, double value, TimeSpan? expiry = null, Co
public async Task<TKey[]> KeysAsync(CommandFlags flags = CommandFlags.None)
{
var keys = await this.Connection.Database.HashKeysAsync(this.Key, flags).ConfigureAwait(false);
return keys.Select(this.Connection.Converter, (x, c) => c.Deserialize<TKey>(x)).ToArray();
return keys.Select(this.Connection.Converter, static (x, c) => c.Deserialize<TKey>(x)).ToArray();
}


Expand All @@ -241,8 +241,8 @@ public Task<bool> SetAsync(TKey field, TValue value, TimeSpan? expiry = null, Wh
var v = this.Connection.Converter.Serialize(value);
return this.ExecuteWithExpiryAsync
(
(db, a) => db.HashSetAsync(a.key, a.f, a.v, a.when, a.flags),
(key: this.Key, f, v, when, flags),
static (db, state) => db.HashSetAsync(state.key, state.f, state.v, state.when, state.flags),
state: (key: this.Key, f, v, when, flags),
expiry,
flags
);
Expand All @@ -257,22 +257,24 @@ public Task SetAsync(IEnumerable<KeyValuePair<TKey, TValue>> entries, TimeSpan?
expiry ??= this.DefaultExpiry;
var hashEntries
= entries
.Select(this.Connection.Converter, (x, c) =>
.Select(this.Connection.Converter, static (x, c) =>
{
var field = c.Serialize(x.Key);
var value = c.Serialize(x.Value);
return new HashEntry(field, value);
})
.ToArray();
return (hashEntries.Length == 0)
? Task.CompletedTask
: this.ExecuteWithExpiryAsync
(
(db, a) => db.HashSetAsync(a.key, a.hashEntries, a.flags),
(key: this.Key, hashEntries, flags),
expiry,
flags
);

if (hashEntries.Length == 0)
return Task.CompletedTask;

return this.ExecuteWithExpiryAsync
(
static (db, state) => db.HashSetAsync(state.key, state.hashEntries, state.flags),
state: (key: this.Key, hashEntries, flags),
expiry,
flags
);
}


Expand All @@ -282,7 +284,7 @@ var hashEntries
public async Task<TValue[]> ValuesAsync(CommandFlags flags = CommandFlags.None)
{
var values = await this.Connection.Database.HashValuesAsync(this.Key, flags).ConfigureAwait(false);
return values.Select(this.Connection.Converter, (x, c) => c.Deserialize<TValue>(x)).ToArray();
return values.Select(this.Connection.Converter, static (x, c) => c.Deserialize<TValue>(x)).ToArray();
}
#endregion

Expand Down Expand Up @@ -346,7 +348,7 @@ public async Task<Dictionary<TKey, TValue>> GetOrSetAsync(IEnumerable<TKey> fiel
//--- divides cached / non cached
var cached = new Dictionary<TKey, TValue>(comparer);
var notCached = new LinkedList<TKey>();
foreach (var x in fields.Zip(values, (f, v) => (f, v)))
foreach (var x in fields.Zip(values, static (f, v) => (f, v)))
{
var result = x.v.ToResult<TValue>(this.Connection.Converter);
if (result.HasValue)
Expand Down Expand Up @@ -385,7 +387,7 @@ public async Task<Dictionary<TKey, TValue>> GetOrSetAsync(IEnumerable<TKey> fiel
//--- divides cached / non cached
var cached = new Dictionary<TKey, TValue>(comparer);
var notCached = new LinkedList<TKey>();
foreach (var x in fields.Zip(values, (f, v) => (f, v)))
foreach (var x in fields.Zip(values, static (f, v) => (f, v)))
{
var result = x.v.ToResult<TValue>(this.Connection.Converter);
if (result.HasValue)
Expand Down Expand Up @@ -438,14 +440,14 @@ public async Task<Dictionary<TKey, TValue>> GetAndDeleteAsync(IEnumerable<TKey>
var values = await this.Connection.Database.HashGetAsync(this.Key, hashFields, flags).ConfigureAwait(false);
var result
= fields
.Zip(values, (f, v) => (field: f, value: v))
.Select(this.Connection.Converter, (x, c) =>
.Zip(values, static (f, v) => (field: f, value: v))
.Select(this.Connection.Converter, static (x, c) =>
{
var result = x.value.ToResult<TValue>(c);
return (x.field, result);
})
.Where(x => x.result.HasValue)
.ToDictionary(x => x.field, x => x.result.Value, comparer);
.Where(static x => x.result.HasValue)
.ToDictionary(static x => x.field, static x => x.result.Value, comparer);

//--- DeleteAsync
if (0 < result.Count)
Expand Down
14 changes: 7 additions & 7 deletions src/CloudStructures/Structures/RedisGeo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public Task<bool> AddAsync(RedisGeoEntry<T> value, TimeSpan? expiry = null, Comm
var entry = value.ToNonGenerics(this.Connection.Converter);
return this.ExecuteWithExpiryAsync
(
(db, a) => db.GeoAddAsync(a.key, a.entry, a.flags),
(key: this.Key, entry, flags),
static (db, state) => db.GeoAddAsync(state.key, state.entry, state.flags),
state: (key: this.Key, entry, flags),
expiry,
flags
);
Expand All @@ -84,11 +84,11 @@ public Task<bool> AddAsync(RedisGeoEntry<T> value, TimeSpan? expiry = null, Comm
public Task<long> AddAsync(IEnumerable<RedisGeoEntry<T>> values, TimeSpan? expiry = null, CommandFlags flags = CommandFlags.None)
{
expiry ??= this.DefaultExpiry;
var entries = values.Select(this.Connection.Converter, (x, c) => x.ToNonGenerics(c)).ToArray();
var entries = values.Select(this.Connection.Converter, static (x, c) => x.ToNonGenerics(c)).ToArray();
return this.ExecuteWithExpiryAsync
(
(db, a) => db.GeoAddAsync(a.key, a.entries, a.flags),
(key: this.Key, entries, flags),
static (db, state) => db.GeoAddAsync(state.key, state.entries, state.flags),
state: (key: this.Key, entries, flags),
expiry,
flags
);
Expand Down Expand Up @@ -163,7 +163,7 @@ public Task<string[]> HashAsync(IEnumerable<T> members, CommandFlags flags = Com
public async Task<RedisGeoRadiusResult<T>[]> RadiusAsync(double longitude, double latitude, double radius, GeoUnit unit = GeoUnit.Meters, int count = -1, Order? order = null, GeoRadiusOptions options = GeoRadiusOptions.Default, CommandFlags flags = CommandFlags.None)
{
var results = await this.Connection.Database.GeoRadiusAsync(this.Key, longitude, latitude, radius, unit, count, order, options, flags).ConfigureAwait(false);
return results.Select(this.Connection.Converter, (x, c) => x.ToGenerics<T>(c)).ToArray();
return results.Select(this.Connection.Converter, static (x, c) => x.ToGenerics<T>(c)).ToArray();
}


Expand All @@ -174,7 +174,7 @@ public async Task<RedisGeoRadiusResult<T>[]> RadiusAsync(T member, double radius
{
var value = this.Connection.Converter.Serialize(member);
var results = await this.Connection.Database.GeoRadiusAsync(this.Key, value, radius, unit, count, order, options, flags).ConfigureAwait(false);
return results.Select(this.Connection.Converter, (x, c) => x.ToGenerics<T>(c)).ToArray();
return results.Select(this.Connection.Converter, static (x, c) => x.ToGenerics<T>(c)).ToArray();
}


Expand Down
Loading