Skip to content

Commit

Permalink
Enable caching
Browse files Browse the repository at this point in the history
  • Loading branch information
MSWS committed Dec 17, 2024
1 parent b615c3d commit 27af4ec
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 28 deletions.
4 changes: 4 additions & 0 deletions src/CS2/Leaderboard/Leaderboard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@
<HintPath>..\..\..\..\..\..\.nuget\packages\mysqlconnector\2.3.7\lib\net8.0\MySqlConnector.dll</HintPath>
</Reference>
</ItemGroup>

<ItemGroup>
<PackageReference Include="MySqlConnector" Version="2.3.7" />
</ItemGroup>
</Project>
12 changes: 5 additions & 7 deletions src/GangsImpl/AbstractDB/AbstractDBGangManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public abstract class AbstractDBGangManager(IServiceProvider provider,
protected DbConnection Connection = null!;
protected DbTransaction? Transaction;

// private readonly Dictionary<int, IGang> cache = new();
private readonly Dictionary<int, IGang> cache = new();

public void Start(BasePlugin? plugin, bool hotReload) {
Connection = CreateDbConnection(connectionString);
Expand Down Expand Up @@ -50,13 +50,14 @@ public async Task<IEnumerable<IGang>> GetGangs() {
}

public async Task<IGang?> 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<DBGang>(query, new { id },
Transaction);

if (result == null) return null;
// cache[id] = result;
cache[id] = result;
return result;
}

Expand All @@ -68,7 +69,7 @@ public async Task<IEnumerable<IGang>> GetGangs() {

public async Task<bool> 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;
}
Expand All @@ -83,7 +84,7 @@ public async Task<bool> 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;
}
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 6 additions & 6 deletions src/GangsImpl/AbstractDB/AbstractDBPlayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public abstract class AbstractDBPlayerManager(string connectionString,
protected DbConnection Connection = null!;
protected DbTransaction? Transaction;

// private Dictionary<ulong, IGangPlayer> cache = new();
private readonly Dictionary<ulong, IGangPlayer> cache = new();

public void Start(BasePlugin? plugin, bool hotReload) {
Connection = CreateDbConnection(connectionString);
Expand Down Expand Up @@ -40,11 +40,11 @@ public void Dispose() {
}

public async Task<IGangPlayer?> 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<DBPlayer>(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);
}
Expand All @@ -56,7 +56,7 @@ public async Task<IGangPlayer> 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;
}

Expand All @@ -80,13 +80,13 @@ public async Task<bool> 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<bool> 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;
}
Expand Down
26 changes: 11 additions & 15 deletions src/GangsImpl/AbstractDB/AbstractInstanceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,12 @@ public abstract class AbstractInstanceManager<TK>(string connectionString,
abstract protected string PrimaryKey { get; }
private string primaryTypeString => GetDBType(typeof(TK));

// private Dictionary<string, Dictionary<TK, object>> cache = new();

public void ClearCache() { }

public Task Load() { return Task.CompletedTask; }
private readonly Dictionary<string, Dictionary<TK, object>> cache = new();

public async Task<(bool, TV?)> Get<TV>(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<TV>(statId);
try {
var dynamic = new DynamicParameters();
Expand All @@ -30,8 +26,8 @@ public void ClearCache() { }
$"SELECT {(typeof(TV).IsBasicallyPrimitive() ? statId : GetFieldNames<TV>())} 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;
Expand Down Expand Up @@ -76,9 +72,9 @@ public async Task<bool> Set<TV>(TK key, string statId, TV value) {
fieldValues.Add($"@{field.Name}", field.GetValue(value));

if (value != null) {
// if (!cache.ContainsKey(statId))
// cache[statId] = new Dictionary<TK, object>();
// cache[statId][key] = value;
if (!cache.ContainsKey(statId))
cache[statId] = new Dictionary<TK, object>();
cache[statId][key] = value;
}

await Connection.ExecuteAsync(cmd, fieldValues);
Expand All @@ -92,8 +88,8 @@ public async Task<bool> 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;
Expand Down

0 comments on commit 27af4ec

Please sign in to comment.