Skip to content

Commit

Permalink
Change to dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
LtRipley36706 committed Dec 18, 2024
1 parent 851f1ef commit b5827e9
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 33 deletions.
36 changes: 6 additions & 30 deletions Source/ACE.Database/DatabaseManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;

using Microsoft.EntityFrameworkCore;

Expand Down Expand Up @@ -72,40 +73,15 @@ public static void Stop()
serializedShardDb.Stop();
}

private static ServerVersion authServerVersion;
private static ServerVersion shardServerVersion;
private static ServerVersion worldServerVersion;
public static ServerVersion CachedServerVersionAutoDetect(int database, string connectionString)
{
var serverVersion = database switch
{
1 => authServerVersion,
2 => shardServerVersion,
_ => worldServerVersion,
};
private static readonly Dictionary<string, ServerVersion> cachedServerVersions = new();

if (serverVersion != null)
return serverVersion;
public static ServerVersion CachedServerVersionAutoDetect(string database, string connectionString)
{
if (cachedServerVersions.TryGetValue(database, out ServerVersion serverVersion)) { return serverVersion; }

serverVersion = ServerVersion.AutoDetect(connectionString);

//using var connection = new MySqlConnection(
// new MySqlConnectionStringBuilder(connectionString)
// {
// Database = string.Empty,
// AutoEnlist = false,
// //Pooling = false,
// }.ConnectionString);
//connection.Open();

//serverVersion = ServerVersion.Parse(connection.ServerVersion);

if (database == 1)
authServerVersion = serverVersion;
else if (database == 2)
shardServerVersion = serverVersion;
else
worldServerVersion = serverVersion;
cachedServerVersions[database] = serverVersion;

return serverVersion;
}
Expand Down
2 changes: 1 addition & 1 deletion Source/ACE.Database/Models/Auth/AuthDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

var connectionString = $"server={config.Host};port={config.Port};user={config.Username};password={config.Password};database={config.Database};{config.ConnectionOptions}";

optionsBuilder.UseMySql(connectionString, DatabaseManager.CachedServerVersionAutoDetect(1, connectionString), builder =>
optionsBuilder.UseMySql(connectionString, DatabaseManager.CachedServerVersionAutoDetect(config.Database, connectionString), builder =>
{
builder.EnableRetryOnFailure(10);
});
Expand Down
2 changes: 1 addition & 1 deletion Source/ACE.Database/Models/Shard/ShardDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

var connectionString = $"server={config.Host};port={config.Port};user={config.Username};password={config.Password};database={config.Database};{config.ConnectionOptions}";

optionsBuilder.UseMySql(connectionString, DatabaseManager.CachedServerVersionAutoDetect(2, connectionString), builder =>
optionsBuilder.UseMySql(connectionString, DatabaseManager.CachedServerVersionAutoDetect(config.Database, connectionString), builder =>
{
builder.EnableRetryOnFailure(10);
});
Expand Down
2 changes: 1 addition & 1 deletion Source/ACE.Database/Models/World/WorldDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

var connectionString = $"server={config.Host};port={config.Port};user={config.Username};password={config.Password};database={config.Database};{config.ConnectionOptions}";

optionsBuilder.UseMySql(connectionString, DatabaseManager.CachedServerVersionAutoDetect(3, connectionString), builder =>
optionsBuilder.UseMySql(connectionString, DatabaseManager.CachedServerVersionAutoDetect(config.Database, connectionString), builder =>
{
builder.EnableRetryOnFailure(10);
});
Expand Down

0 comments on commit b5827e9

Please sign in to comment.