-
-
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
Showing
7 changed files
with
551 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,74 @@ | ||
using PSTk.Redis.Exceptions; | ||
using StackExchange.Redis; | ||
using System; | ||
using System.Text; | ||
|
||
namespace PSTk.Redis.Database | ||
{ | ||
/// <summary> | ||
/// Base class to initialize Redis storage engine. | ||
/// </summary> | ||
public sealed class RedisEngine : IDisposable | ||
{ | ||
private readonly RedisSettings redisSettings; | ||
|
||
private IConnectionMultiplexer connectionMultiplexer; | ||
private IDatabase database; | ||
|
||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member | ||
|
||
public RedisEngine(RedisSettings redisSettings) => this.redisSettings = redisSettings; | ||
|
||
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member | ||
|
||
/// <summary> | ||
/// Gets attached <see cref="IDatabase"/> of current instance. | ||
/// </summary> | ||
public IDatabase Database => database; | ||
|
||
/// <summary> | ||
/// Verify if <see cref="IConnectionMultiplexer"/> is connected. | ||
/// </summary> | ||
public bool IsConnected => connectionMultiplexer != null && connectionMultiplexer.IsConnected; | ||
|
||
/// <summary> | ||
/// Dispose <see cref="IConnectionMultiplexer"/> asynchronously and close connection. | ||
/// </summary> | ||
public async void Dispose() | ||
{ | ||
if (!IsConnected) | ||
return; | ||
|
||
await connectionMultiplexer.CloseAsync(); | ||
connectionMultiplexer.Dispose(); | ||
} | ||
|
||
/// <summary> | ||
/// Tries to create a new <see cref="IConnectionMultiplexer"/> connection asynchronously. | ||
/// </summary> | ||
/// <exception cref="ConnectionException"></exception> | ||
public async void StartAsync() | ||
{ | ||
var connectionStr = new StringBuilder(); | ||
connectionStr.Append($"{redisSettings.Host}:{redisSettings.Port}"); | ||
if (!string.IsNullOrWhiteSpace(redisSettings.Password)) | ||
connectionStr.Append($",password={redisSettings.Password}"); | ||
|
||
connectionStr.Append($",syncTimeout={redisSettings.SyncTimeout}"); | ||
connectionStr.Append($",asyncTimeout={redisSettings.AsyncTimeout}"); | ||
|
||
try | ||
{ | ||
connectionMultiplexer = await ConnectionMultiplexer.ConnectAsync(connectionStr.ToString()); | ||
database = connectionMultiplexer.GetDatabase(redisSettings.Index); | ||
} | ||
catch (RedisConnectionException e) | ||
{ | ||
throw new ConnectionException( | ||
"Redis service cannot initialize. Turn on cluster server to begin database transactions.", | ||
e | ||
); | ||
} | ||
} | ||
} | ||
} |
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 @@ | ||
namespace PSTk.Redis.Database | ||
{ | ||
/// <summary> | ||
/// Base class to iterate with <see cref="RedisObject"/>. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
public sealed class RedisField<T> | ||
{ | ||
private string key; | ||
private RedisObject redisObject; | ||
|
||
private RedisField() | ||
{ } | ||
|
||
/// <summary> | ||
/// Getter and setter for current <see cref="RedisObject"/>. | ||
/// </summary> | ||
public T Field | ||
{ | ||
get => redisObject.GetValue<T>(key); | ||
set => redisObject.SetValue(key, value); | ||
} | ||
|
||
/// <summary> | ||
/// Create a generic version of object for <see cref="RedisField{T}"/>. | ||
/// </summary> | ||
/// <param name="redisObject"></param> | ||
/// <param name="key"></param> | ||
/// <returns></returns> | ||
public static RedisField<T> Create(RedisObject redisObject, string key) | ||
{ | ||
var redisField = new RedisField<T>() | ||
{ | ||
redisObject = redisObject, | ||
key = key | ||
}; | ||
return redisField; | ||
} | ||
} | ||
} |
Oops, something went wrong.