From 27af4eca5b63905e056ddec7b384f49b5ed88cd0 Mon Sep 17 00:00:00 2001 From: MSWS Date: Mon, 16 Dec 2024 21:29:24 -0800 Subject: [PATCH] Enable caching --- src/CS2/Leaderboard/Leaderboard.csproj | 4 +++ .../AbstractDB/AbstractDBGangManager.cs | 12 ++++----- .../AbstractDB/AbstractDBPlayerManager.cs | 12 ++++----- .../AbstractDB/AbstractInstanceManager.cs | 26 ++++++++----------- 4 files changed, 26 insertions(+), 28 deletions(-) diff --git a/src/CS2/Leaderboard/Leaderboard.csproj b/src/CS2/Leaderboard/Leaderboard.csproj index 539d7ac..ff474bf 100644 --- a/src/CS2/Leaderboard/Leaderboard.csproj +++ b/src/CS2/Leaderboard/Leaderboard.csproj @@ -16,4 +16,8 @@ ..\..\..\..\..\..\.nuget\packages\mysqlconnector\2.3.7\lib\net8.0\MySqlConnector.dll + + + + diff --git a/src/GangsImpl/AbstractDB/AbstractDBGangManager.cs b/src/GangsImpl/AbstractDB/AbstractDBGangManager.cs index 186e398..64b5477 100644 --- a/src/GangsImpl/AbstractDB/AbstractDBGangManager.cs +++ b/src/GangsImpl/AbstractDB/AbstractDBGangManager.cs @@ -21,7 +21,7 @@ public abstract class AbstractDBGangManager(IServiceProvider provider, protected DbConnection Connection = null!; protected DbTransaction? Transaction; - // private readonly Dictionary cache = new(); + private readonly Dictionary cache = new(); public void Start(BasePlugin? plugin, bool hotReload) { Connection = CreateDbConnection(connectionString); @@ -50,13 +50,14 @@ public async Task> GetGangs() { } public async Task GetGang(int id) { + if (cache.TryGetValue(id, out var cached)) return cached; var query = $"SELECT * FROM {table} WHERE GangId = @id"; var result = await Connection.QueryFirstOrDefaultAsync(query, new { id }, Transaction); if (result == null) return null; - // cache[id] = result; + cache[id] = result; return result; } @@ -68,7 +69,7 @@ public async Task> GetGangs() { public async Task UpdateGang(IGang gang) { var query = $"UPDATE {table} SET Name = @Name WHERE GangId = @GangId"; - // cache[gang.GangId] = gang; + cache[gang.GangId] = gang; return await Connection.ExecuteAsync(query, new { gang.Name, gang.GangId }, Transaction) == 1; } @@ -83,7 +84,7 @@ public async Task DeleteGang(int id) { await ranks.DeleteAllRanks(id); - // cache.Remove(id); + cache.Remove(id); var query = $"DELETE FROM {table} WHERE GangId = @id"; return await Connection.ExecuteAsync(query, new { id }, Transaction) > 0; } @@ -124,9 +125,6 @@ public void Dispose() { Connection.Dispose(); } - public void ClearCache() { } - public Task Load() { return Task.CompletedTask; } - abstract protected DbConnection CreateDbConnection(string connectionString); virtual protected string CreateTableQuery(string tableName, bool inTesting) { diff --git a/src/GangsImpl/AbstractDB/AbstractDBPlayerManager.cs b/src/GangsImpl/AbstractDB/AbstractDBPlayerManager.cs index 3741f3f..cdd105a 100644 --- a/src/GangsImpl/AbstractDB/AbstractDBPlayerManager.cs +++ b/src/GangsImpl/AbstractDB/AbstractDBPlayerManager.cs @@ -11,7 +11,7 @@ public abstract class AbstractDBPlayerManager(string connectionString, protected DbConnection Connection = null!; protected DbTransaction? Transaction; - // private Dictionary cache = new(); + private readonly Dictionary cache = new(); public void Start(BasePlugin? plugin, bool hotReload) { Connection = CreateDbConnection(connectionString); @@ -40,11 +40,11 @@ public void Dispose() { } public async Task GetPlayer(ulong steamId, bool create = true) { - // if (cache.TryGetValue(steamId, out var player)) return player; + if (cache.TryGetValue(steamId, out var player)) return player; var query = $"SELECT * FROM {table} WHERE Steam = @steamId"; var result = await Connection.QueryFirstOrDefaultAsync(query, new { steamId }, Transaction); - // if (result != null) cache[steamId] = result; + if (result != null) cache[steamId] = result; if (result != null || !create) return result; return await CreatePlayer(steamId); } @@ -56,7 +56,7 @@ public async Task CreatePlayer(ulong steamId, var player = new DBPlayer { Steam = steamId, Name = name }; var query = $"INSERT INTO {table} (Steam, Name) VALUES (@Steam, @Name)"; await Connection.ExecuteAsync(query, player, Transaction); - // cache[steamId] = player; + cache[steamId] = player; return player; } @@ -80,13 +80,13 @@ public async Task UpdatePlayer(IGangPlayer player) { var query = $"UPDATE {table} SET Name = @Name, GangId = @GangId, GangRank = @GangRank WHERE Steam = @Steam"; - // cache[player.Steam] = player; + cache[player.Steam] = player; return await Connection.ExecuteAsync(query, player, Transaction) == 1; } public async Task DeletePlayer(ulong steamId) { var query = $"DELETE FROM {table} WHERE Steam = @steamId"; - // cache.Remove(steamId); + cache.Remove(steamId); return await Connection.ExecuteAsync(query, new { steamId }, Transaction) == 1; } diff --git a/src/GangsImpl/AbstractDB/AbstractInstanceManager.cs b/src/GangsImpl/AbstractDB/AbstractInstanceManager.cs index e52e4ec..440b5ad 100644 --- a/src/GangsImpl/AbstractDB/AbstractInstanceManager.cs +++ b/src/GangsImpl/AbstractDB/AbstractInstanceManager.cs @@ -12,16 +12,12 @@ public abstract class AbstractInstanceManager(string connectionString, abstract protected string PrimaryKey { get; } private string primaryTypeString => GetDBType(typeof(TK)); - // private Dictionary> cache = new(); - - public void ClearCache() { } - - public Task Load() { return Task.CompletedTask; } + private readonly Dictionary> cache = new(); public async Task<(bool, TV?)> Get(TK key, string statId) { - // if (cache.TryGetValue(statId, out var dict) - // && dict.TryGetValue(key, out var value)) - // return (true, (TV)value); + if (cache.TryGetValue(statId, out var dict) + && dict.TryGetValue(key, out var value)) + return (true, (TV)value); await createTable(statId); try { var dynamic = new DynamicParameters(); @@ -30,8 +26,8 @@ public void ClearCache() { } $"SELECT {(typeof(TV).IsBasicallyPrimitive() ? statId : GetFieldNames())} FROM {table_prefix}_{statId} WHERE {PrimaryKey} = @{PrimaryKey}", dynamic); if (result == null) return (true, result); - // if (!cache.ContainsKey(statId)) cache[statId] = new(); - // cache[statId][key] = result; + if (!cache.ContainsKey(statId)) cache[statId] = new(); + cache[statId][key] = result; return (true, result); } catch (InvalidOperationException e) { if (!e.Message.Contains("Sequence contains no elements")) throw; @@ -76,9 +72,9 @@ public async Task Set(TK key, string statId, TV value) { fieldValues.Add($"@{field.Name}", field.GetValue(value)); if (value != null) { - // if (!cache.ContainsKey(statId)) - // cache[statId] = new Dictionary(); - // cache[statId][key] = value; + if (!cache.ContainsKey(statId)) + cache[statId] = new Dictionary(); + cache[statId][key] = value; } await Connection.ExecuteAsync(cmd, fieldValues); @@ -92,8 +88,8 @@ public async Task Remove(TK key, string statId) { await Connection.ExecuteAsync( $"DELETE FROM {table_prefix}_{statId} WHERE {PrimaryKey} = @{PrimaryKey}", dynamicParameters); - // if (!cache.TryGetValue(statId, out var value)) return true; - // value.Remove(key); + if (!cache.TryGetValue(statId, out var value)) return true; + value.Remove(key); return true; } catch (DbException e) { if (e.Message.Contains("no such table")) return false;