add project
This commit is contained in:
112
Utils/RedisUtils.cs
Normal file
112
Utils/RedisUtils.cs
Normal file
@ -0,0 +1,112 @@
|
||||
using StackExchange.Redis;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace RedisManager.Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides utility methods for Redis operations, including instance retrieval, connection, array conversion, and table printing.
|
||||
/// </summary>
|
||||
public static class RedisUtils
|
||||
{
|
||||
/// <summary>
|
||||
/// Retrieves an InstanceConfig by name from the provided configuration. Exits if not found.
|
||||
/// </summary>
|
||||
/// <param name="config">The configuration containing Redis instances.</param>
|
||||
/// <param name="name">The name of the instance to retrieve.</param>
|
||||
/// <returns>The matching InstanceConfig.</returns>
|
||||
public static InstanceConfig GetInstance(Config config, string name)
|
||||
{
|
||||
var instance = config.Instances.Find(i => i.Name == name);
|
||||
if (instance == null)
|
||||
{
|
||||
Console.WriteLine(Output.Red($"Instance '{name}' not found."));
|
||||
Environment.Exit(1);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Connects to a Redis instance using the provided configuration.
|
||||
/// </summary>
|
||||
/// <param name="instance">The instance configuration to connect with.</param>
|
||||
/// <returns>A connected ConnectionMultiplexer.</returns>
|
||||
public static ConnectionMultiplexer ConnectRedis(InstanceConfig instance)
|
||||
{
|
||||
var options = new ConfigurationOptions
|
||||
{
|
||||
EndPoints = { $"{instance.Host}:{instance.Port}" },
|
||||
Password = instance.Password
|
||||
};
|
||||
return ConnectionMultiplexer.Connect(options);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts an enumerable of strings to a RedisValue array.
|
||||
/// </summary>
|
||||
/// <param name="values">The string values to convert.</param>
|
||||
/// <returns>An array of RedisValue.</returns>
|
||||
public static RedisValue[] ArrayFrom(IEnumerable<string> values)
|
||||
{
|
||||
var list = new List<RedisValue>();
|
||||
foreach (var v in values)
|
||||
list.Add(v);
|
||||
return list.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts an enumerable of strings to a RedisKey array.
|
||||
/// </summary>
|
||||
/// <param name="values">The string values to convert.</param>
|
||||
/// <returns>An array of RedisKey.</returns>
|
||||
public static RedisKey[] ArrayFromKeys(IEnumerable<string> values)
|
||||
{
|
||||
var list = new List<RedisKey>();
|
||||
foreach (var v in values)
|
||||
list.Add(v);
|
||||
return list.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Prints a formatted table to the console with headers and rows.
|
||||
/// </summary>
|
||||
/// <param name="headers">The column headers.</param>
|
||||
/// <param name="rows">The table rows.</param>
|
||||
public static void PrintTable(string[] headers, List<string[]> rows)
|
||||
{
|
||||
if (rows.Count == 0)
|
||||
{
|
||||
Console.WriteLine(Output.Yellow("No data"));
|
||||
return;
|
||||
}
|
||||
|
||||
// Calculate column widths
|
||||
var widths = new int[headers.Length];
|
||||
for (int i = 0; i < headers.Length; i++)
|
||||
{
|
||||
widths[i] = headers[i].Length;
|
||||
foreach (var row in rows)
|
||||
{
|
||||
if (i < row.Length && row[i] != null)
|
||||
widths[i] = Math.Max(widths[i], row[i].Length);
|
||||
}
|
||||
}
|
||||
|
||||
// Print header
|
||||
Console.WriteLine(Output.Blue(string.Join(" | ", headers.Select((h, i) => h.PadRight(widths[i])))));
|
||||
Console.WriteLine(Output.Blue(new string('-', widths.Sum() + (headers.Length - 1) * 3)));
|
||||
|
||||
// Print rows
|
||||
foreach (var row in rows)
|
||||
{
|
||||
var formattedRow = new string[headers.Length];
|
||||
for (int i = 0; i < headers.Length; i++)
|
||||
{
|
||||
formattedRow[i] = i < row.Length && row[i] != null ? row[i].PadRight(widths[i]) : "".PadRight(widths[i]);
|
||||
}
|
||||
Console.WriteLine(Output.Green(string.Join(" | ", formattedRow)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user