more than one connection

alphons <alphons@heijden.com> 19 Jan 2025, 10:14
c66f0919140bd0b0ca2bdfb8e2729b6859fdcc2f
5 files changed
  • RedisGui/ConnectionPoint.cs
  • RedisGui/FormMain.Designer.cs
  • RedisGui/FormMain.cs
  • RedisGui/RedisGui.csproj
  • RedisGui/appsettings.json
diff --git a/RedisGui/ConnectionPoint.cs b/RedisGui/ConnectionPoint.cs
new file mode 100644
index 0000000..7f718cd
--- /dev/null
+++ b/RedisGui/ConnectionPoint.cs
@@ -0,0 +1,11 @@
+using StackExchange.Redis;
+
+namespace RedisGui;
+
+public class ConnectionPoint
+{
+ public string ConnectionString { get; set; } = "127.0.0.1:6379";
+ public ConnectionMultiplexer? Connection { get; set; }
+ public IServer? Server { get; set; }
+ public IDatabase? Db { get; set; }
+}
diff --git a/RedisGui/FormMain.Designer.cs b/RedisGui/FormMain.Designer.cs
index 733e3b9..19deaa8 100644
--- a/RedisGui/FormMain.Designer.cs
+++ b/RedisGui/FormMain.Designer.cs
@@ -31,6 +31,8 @@
components = new System.ComponentModel.Container();
menuStrip1 = new MenuStrip();
fileToolStripMenuItem = new ToolStripMenuItem();
+ toolStripMenuItem1 = new ToolStripMenuItem();
+ toolStripSeparator2 = new ToolStripSeparator();
exitToolStripMenuItem = new ToolStripMenuItem();
statusStrip1 = new StatusStrip();
toolStripStatusLabel1 = new ToolStripStatusLabel();
@@ -86,15 +88,28 @@
//
// fileToolStripMenuItem
//
- fileToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { exitToolStripMenuItem });
+ fileToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { toolStripMenuItem1, toolStripSeparator2, exitToolStripMenuItem });
fileToolStripMenuItem.Name = "fileToolStripMenuItem";
fileToolStripMenuItem.Size = new Size(37, 20);
fileToolStripMenuItem.Text = "File";
+ fileToolStripMenuItem.DropDownOpening += FileToolStripMenuItem_DropDownOpening;
+ //
+ // toolStripMenuItem1
+ //
+ toolStripMenuItem1.Name = "toolStripMenuItem1";
+ toolStripMenuItem1.Size = new Size(180, 22);
+ toolStripMenuItem1.Text = "Connections";
+ toolStripMenuItem1.DropDownItemClicked += ToolStripMenuItem1_DropDownItemClicked;
+ //
+ // toolStripSeparator2
+ //
+ toolStripSeparator2.Name = "toolStripSeparator2";
+ toolStripSeparator2.Size = new Size(177, 6);
//
// exitToolStripMenuItem
//
exitToolStripMenuItem.Name = "exitToolStripMenuItem";
- exitToolStripMenuItem.Size = new Size(93, 22);
+ exitToolStripMenuItem.Size = new Size(180, 22);
exitToolStripMenuItem.Text = "Exit";
exitToolStripMenuItem.Click += ExitToolStripMenuItem_Click;
//
@@ -257,7 +272,7 @@
listView1.GridLines = true;
listView1.Location = new Point(12, 49);
listView1.Name = "listView1";
- listView1.Size = new Size(679, 248);
+ listView1.Size = new Size(661, 248);
listView1.TabIndex = 0;
listView1.UseCompatibleStateImageBehavior = false;
listView1.View = View.Details;
@@ -284,7 +299,7 @@
txtData.Name = "txtData";
txtData.ReadOnly = true;
txtData.ScrollBars = ScrollBars.Both;
- txtData.Size = new Size(1059, 134);
+ txtData.Size = new Size(1059, 116);
txtData.TabIndex = 5;
//
// contextMenuStrip1
@@ -417,5 +432,7 @@
private Label label4;
private TextBox txtSearch;
private Button button1;
+ private ToolStripSeparator toolStripSeparator2;
+ private ToolStripMenuItem toolStripMenuItem1;
}
}
diff --git a/RedisGui/FormMain.cs b/RedisGui/FormMain.cs
index 8e2eb44..a398de6 100644
--- a/RedisGui/FormMain.cs
+++ b/RedisGui/FormMain.cs
@@ -1,11 +1,10 @@
using Microsoft.Extensions.Configuration;
-using RedisGui.Properties;
using StackExchange.Redis;
+using System.Configuration;
using System.Diagnostics;
using System.Net;
-using System.Collections.Generic;
namespace RedisGui;
@@ -13,9 +12,7 @@ public partial class FormMain : Form
{
private readonly IConfiguration configuration;
- private ConnectionMultiplexer? connection;
- private IServer? server;
- private IDatabase? db;
+ private readonly List<ConnectionPoint> connections = [];
public FormMain()
{
@@ -23,41 +20,62 @@ public partial class FormMain : Form
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
- configuration = builder.Build();
+ this.configuration = builder.Build();
- imageList1.Images.Add(SystemIcons.GetStockIcon(StockIconId.World, 24));
- imageList1.Images.Add(SystemIcons.GetStockIcon(StockIconId.NetworkConnect, 24));
- imageList1.Images.Add(SystemIcons.GetStockIcon(StockIconId.Key, 24));
+ var connectionStrings = configuration.GetSection("AppSettings:ConnectionStrings").Get<string[]>();
+
+ if(connectionStrings != null)
+ connections = connectionStrings.Select(x => new ConnectionPoint { ConnectionString = x }).ToList();
+
+ this.imageList1.Images.Add(SystemIcons.GetStockIcon(StockIconId.World, 24));
+ this.imageList1.Images.Add(SystemIcons.GetStockIcon(StockIconId.NetworkConnect, 24));
+ this.imageList1.Images.Add(SystemIcons.GetStockIcon(StockIconId.Key, 24));
}
private async void Form_Load(object sender, EventArgs e)
{
- await MakeConnectionAsync();
+ await MakeConnectionAsync("127.0.0.1:6379");
}
private async void AddConnectionToolStripMenuItem_Click(object sender, EventArgs e)
{
- await MakeConnectionAsync();
+ await MakeConnectionAsync("127.0.0.1:6379");
}
- private async Task MakeConnectionAsync()
+ private async Task MakeConnectionAsync(string ConnectionString)
{
+ var connectionPoint = this.connections.FirstOrDefault(x => x.ConnectionString == ConnectionString);
- List<string> connectionStrings = configuration
- .GetSection("AppSettings:ConnectionStrings")
- .Get<List<string>>() ?? [];
+ if (connectionPoint == null)
+ return;
+
+ foreach (TreeNode item in treeView1.Nodes)
+ {
+ if (item.Text == ConnectionString)
+ return;
+ }
- var ConnectionString = connectionStrings[0];
+ try
+ {
+ connectionPoint.Connection = await ConnectionMultiplexer.ConnectAsync(ConnectionString, x => x.AllowAdmin = true);
+ }
+ catch(RedisConnectionException)
+ {
+ return;
+ }
- this.connection = ConnectionMultiplexer.Connect(ConnectionString, x => x.AllowAdmin = true);
+ EndPoint endPoint = connectionPoint.Connection.GetEndPoints().First();
- EndPoint endPoint = connection.GetEndPoints().First();
+ connectionPoint.Server = connectionPoint.Connection.GetServer(endPoint);
- this.server = connection.GetServer(endPoint);
+ connectionPoint.Db = connectionPoint.Connection.GetDatabase();
- this.db = connection.GetDatabase();
+ var connectionNode = new TreeNode(ConnectionString, 1, 1)
+ {
+ Tag = connectionPoint
+ };
- var node = this.treeView1.Nodes.Add("", endPoint.ToString(), 1, 1);
+ var node = this.treeView1.Nodes.Add(connectionNode);
await SearchAsync();
@@ -71,13 +89,16 @@ public partial class FormMain : Form
var key = new RedisKey(e.Node.Text);
- if (this.db == null)
+ if (e.Node.Parent == null)
return;
- if (e.Node.Parent == null)
+ var connectionNode = e.Node.Parent;
+
+
+ if (connectionNode.Tag is not ConnectionPoint connection || connection.Db == null)
return;
- if (!db.KeyExists(key))
+ if (!connection.Db.KeyExists(key))
{
e.Node.Remove();
return;
@@ -96,13 +117,19 @@ public partial class FormMain : Form
this.listView1.Items.Clear();
this.txtData.Clear();
- if (this.db == null)
+ if (this.treeView1.SelectedNode == null || this.treeView1.SelectedNode.Parent == null)
+ return;
+
+
+ if (this.treeView1.SelectedNode.Parent.Tag is not ConnectionPoint connectionNode || connectionNode.Db == null)
return;
+ var db = connectionNode.Db;
+
if (!db.KeyExists(key))
return;
- RedisType type = this.db.KeyType(key);
+ RedisType type = db.KeyType(key);
this.lblType.Text = type.ToString();
@@ -186,10 +213,14 @@ public partial class FormMain : Form
this.listView1.Items.Clear();
- if (this.server == null)
+ if (this.treeView1.SelectedNode == null || this.treeView1.SelectedNode.Parent == null)
+ return;
+
+
+ if (this.treeView1.SelectedNode.Parent.Tag is not ConnectionPoint connectionNode || connectionNode.Server == null)
return;
- var kvs = await this.server.ConfigGetAsync("*");
+ var kvs = await connectionNode.Server.ConfigGetAsync("*");
foreach (var item in kvs)
{
@@ -204,10 +235,14 @@ public partial class FormMain : Form
this.listView1.Items.Clear();
- if (this.server == null)
+ if (this.treeView1.SelectedNode == null || this.treeView1.SelectedNode.Parent == null)
+ return;
+
+
+ if (this.treeView1.SelectedNode.Parent.Tag is not ConnectionPoint connectionNode || connectionNode.Server == null)
return;
- var groups = await this.server.InfoAsync();
+ var groups = await connectionNode.Server.InfoAsync();
foreach (var group in groups)
{
@@ -251,12 +286,21 @@ public partial class FormMain : Form
async private void CloseConnectionToolStripMenuItem_Click(object sender, EventArgs e)
{
+ if (this.treeView1.SelectedNode == null)
+ return;
+
+
+ if (this.treeView1.SelectedNode.Tag is not ConnectionPoint connectionNode)
+ return;
+
+ if (connectionNode.Connection != null)
+ await connectionNode.Connection.CloseAsync();
+
+ connectionNode.Db = null;
+ connectionNode.Server = null;
+ connectionNode.Connection = null;
+
this.treeView1.SelectedNode.Remove();
- this.db = null;
- this.server = null;
- if (this.connection != null)
- await this.connection.CloseAsync();
- this.connection = null;
}
private void ListView_SelectedIndexChanged(object sender, EventArgs e)
@@ -284,36 +328,31 @@ public partial class FormMain : Form
private async void Timer_Tick(object sender, EventArgs e)
{
- if (this.server == null)
- return;
-
- this.toolStripStatusLabel1.Image = Resources.green;
-
await SearchAsync();
-
- this.toolStripStatusLabel1.Image = Resources.blue;
}
private async Task SearchAsync()
{
- if (this.server == null)
- return;
-
var strSearch = this.txtSearch.Text.Trim();
_ = int.TryParse(this.txtMaxKeys.Text, out int PageSize);
- IAsyncEnumerator<RedisKey> keysAsync = this.server
- .KeysAsync(pattern: $"*{strSearch}*", pageSize: PageSize)
- .GetAsyncEnumerator();
- List<RedisKey> keys = [];
- while (await keysAsync.MoveNextAsync())
- {
- keys.Add(keysAsync.Current);
- }
-
foreach (TreeNode connectionNode in this.treeView1.Nodes)
{
+ if (connectionNode.Tag is not ConnectionPoint connectionPoint || connectionPoint.Server == null)
+ continue;
+
+ IAsyncEnumerator<RedisKey> keysAsync = connectionPoint
+ .Server
+ .KeysAsync(pattern: $"*{strSearch}*", pageSize: PageSize)
+ .GetAsyncEnumerator();
+
+ List<RedisKey> keys = [];
+ while (await keysAsync.MoveNextAsync())
+ {
+ keys.Add(keysAsync.Current);
+ }
+
for (int j = connectionNode.Nodes.Count - 1; j >= 0; j--)
{
TreeNode keyNode = connectionNode.Nodes[j];
@@ -343,10 +382,11 @@ public partial class FormMain : Form
private async void DeleteKeyToolStripMenuItem_Click(object sender, EventArgs e)
{
- if (this.db == null)
+ if (this.treeView1.SelectedNode == null)
return;
- if (this.treeView1.SelectedNode == null)
+
+ if (this.treeView1.SelectedNode.Tag is not ConnectionPoint connectionPoint || connectionPoint.Db == null)
return;
var key = this.treeView1.SelectedNode.Text;
@@ -355,14 +395,29 @@ public partial class FormMain : Form
if (result == DialogResult.OK)
{
-
- await this.db.KeyDeleteAsync(new RedisKey(key));
+ await connectionPoint.Db.KeyDeleteAsync(new RedisKey(key));
this.treeView1.SelectedNode.Remove();
this.listView1.Items.Clear();
}
+ }
+ private void FileToolStripMenuItem_DropDownOpening(object sender, EventArgs e)
+ {
+ this.toolStripMenuItem1.DropDownItems.Clear();
+
+ List<string> connectionStrings = configuration
+ .GetSection("AppSettings:ConnectionStrings")
+ .Get<List<string>>() ?? [];
+ this.toolStripMenuItem1.DropDownItems.AddRange(connectionStrings.Select(x => new ToolStripMenuItem(x)).ToArray());
}
+ private async void ToolStripMenuItem1_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
+ {
+ var connectionString = e.ClickedItem?.Text;
+ if (connectionString == null)
+ return;
+ await MakeConnectionAsync(connectionString);
+ }
}
diff --git a/RedisGui/RedisGui.csproj b/RedisGui/RedisGui.csproj
index 0bc011d..ef0af5c 100644
--- a/RedisGui/RedisGui.csproj
+++ b/RedisGui/RedisGui.csproj
@@ -29,4 +29,10 @@
</EmbeddedResource>
</ItemGroup>
+ <ItemGroup>
+ <None Update="appsettings.json">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </None>
+ </ItemGroup>
+
</Project>
\ No newline at end of file
diff --git a/RedisGui/appsettings.json b/RedisGui/appsettings.json
index e10879b..435091c 100644
--- a/RedisGui/appsettings.json
+++ b/RedisGui/appsettings.json
@@ -1,7 +1,8 @@
{
"AppSettings": {
"ConnectionStrings": [
- "127.0.0.1:6379"
+ "127.0.0.1:6379",
+ "192.168.74.200:6379"
]
}