Skip to content

Commit

Permalink
PSTk.Redis implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Devwarlt committed Mar 6, 2021
1 parent 1095332 commit d00a36f
Show file tree
Hide file tree
Showing 7 changed files with 551 additions and 0 deletions.
74 changes: 74 additions & 0 deletions PSTk.Redis/Database/RedisEngine.cs
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
);
}
}
}
}
40 changes: 40 additions & 0 deletions PSTk.Redis/Database/RedisField.cs
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;
}
}
}
Loading

0 comments on commit d00a36f

Please sign in to comment.