Skip to content

Commit

Permalink
Disable caching
Browse files Browse the repository at this point in the history
  • Loading branch information
MSWS committed Sep 8, 2024
1 parent 37c041f commit 1a104b9
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 61 deletions.
3 changes: 3 additions & 0 deletions GangsAPI/Data/ICacher.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
namespace GangsAPI.Data;

[Obsolete]
public interface ICacher {
[Obsolete]
void ClearCache();
[Obsolete]
Task Load();
}
2 changes: 1 addition & 1 deletion GangsAPI/Services/Gang/IGangManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace GangsAPI.Services.Gang;
/// The Gang Manager should not be used to manage perks or stats.
/// Use the respective <see cref="IPerkManager" /> and <see cref="IStatManager" /> to manage those.
/// </summary>
public interface IGangManager : IPluginBehavior, ICacher {
public interface IGangManager : IPluginBehavior {
/// <summary>
/// Gets all gangs.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion GangsAPI/Services/Gang/IGangStatManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace GangsAPI.Services.Gang;

public interface IGangStatManager : IPluginBehavior, ICacher {
public interface IGangStatManager : IPluginBehavior {
Task<(bool, TV?)> GetForGang<TV>(int key, string statId);
Task<bool> SetForGang<TV>(int gangId, string statId, TV value);
Task<bool> RemoveFromGang(int gangId, string statId);
Expand Down
2 changes: 1 addition & 1 deletion GangsAPI/Services/IStatManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace GangsAPI.Services;
/// <summary>
/// A manager for statistics. Allows for the registration, retrieval, and updating of statistics.
/// </summary>
public interface IStatManager : IPluginBehavior, ICacher {
public interface IStatManager : IPluginBehavior {
/// <summary>
/// Retrieves all statistics.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion GangsAPI/Services/Player/IPlayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace GangsAPI.Services.Player;
/// <summary>
/// A manager for players. Allows for the retrieval and creation of players.
/// </summary>
public interface IPlayerManager : IPluginBehavior, ICacher {
public interface IPlayerManager : IPluginBehavior {
/// <summary>
/// Gets a player by their SteamID64.
/// If the player does not exist, attempts to create them.
Expand Down
2 changes: 1 addition & 1 deletion GangsAPI/Services/Player/IPlayerStatManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace GangsAPI.Services.Player;

public interface IPlayerStatManager : IPluginBehavior, ICacher {
public interface IPlayerStatManager : IPluginBehavior {
Task<(bool, TV?)> GetForPlayer<TV>(ulong steam, string statId);
Task<bool> SetForPlayer<TV>(ulong steam, string statId, TV value);
Task<bool> RemoveFromPlayer(ulong steam, string statId);
Expand Down
46 changes: 27 additions & 19 deletions GangsImpl/AbstractDB/AbstractDBGangManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@
using CounterStrikeSharp.API.Core;
using Dapper;
using GangsAPI.Data.Gang;
using GangsAPI.Services.Gang;
using GangsAPI.Services.Player;
using Mock;

namespace GenericDB;

public abstract class AbstractDBGangManager(IPlayerManager playerMgr,
string connectionString, string table = "gang_gangs", bool testing = false)
: MockGangManager(playerMgr) {
: IGangManager {
protected DbConnection Connection = null!;
protected DbTransaction? Transaction;
public override void ClearCache() { CachedGangs.Clear(); }
public void ClearCache() { }
public Task Load() { return Task.CompletedTask; }

public override void Start(BasePlugin? plugin, bool hotReload) {
public void Start(BasePlugin? plugin, bool hotReload) {
Connection = CreateDbConnection(connectionString);

Connection.Open();
Expand Down Expand Up @@ -43,22 +45,30 @@ virtual protected string CreateTableQuery(string tableName, bool inTesting) {
$"CREATE TABLE IF NOT EXISTS {tableName} (GangId INT NOT NULL AUTO_INCREMENT PRIMARY KEY, Name VARCHAR(255) NOT NULL)";
}

public override async Task Load() {
public async Task<IEnumerable<IGang>> GetGangs() {
var query = $"SELECT * FROM {table}";
var gangs = await Connection.QueryAsync<DBGang>(query);
foreach (var gang in gangs) CachedGangs.Add(gang);
return await Connection.QueryAsync<DBGang>(query, transaction: Transaction);
}

public override async Task<bool> UpdateGang(IGang gang) {
var result = await base.UpdateGang(gang);
var query = $"UPDATE {table} SET Name = @Name WHERE GangId = @GangId";
await Connection.ExecuteAsync(query, new { gang.Name, gang.GangId },
public async Task<IGang?> GetGang(int id) {
var query = $"SELECT * FROM {table} WHERE GangId = @id";
return await Connection.QueryFirstOrDefaultAsync<DBGang>(query, new { id },
Transaction);
return result;
}

public override async Task<bool> DeleteGang(int id) {
await base.DeleteGang(id);
public async Task<IGang?> GetGang(ulong steam) {
var player = await playerMgr.GetPlayer(steam);
if (player?.GangId == null) return null;
return await GetGang(player.GangId.Value);
}

public async Task<bool> UpdateGang(IGang gang) {
var query = $"UPDATE {table} SET Name = @Name WHERE GangId = @GangId";
return await Connection.ExecuteAsync(query, new { gang.Name, gang.GangId },
Transaction) == 1;
}

public async Task<bool> DeleteGang(int id) {
var query = $"DELETE FROM {table} WHERE GangId = @id";
return await Connection.ExecuteAsync(query, new { id }, Transaction) > 0;
}
Expand All @@ -68,9 +78,8 @@ virtual protected async Task<int> GetLastId() {
transaction: Transaction);
}

public override async Task<IGang?> CreateGang(string name, ulong owner) {
if (CachedGangs.Any(g => g.Name == name)) return null;
var player = await PlayerMgr.GetPlayer(owner);
public async Task<IGang?> CreateGang(string name, ulong owner) {
var player = await playerMgr.GetPlayer(owner);
if (player == null) return null;
if (player.GangId != null)
throw new InvalidOperationException(
Expand All @@ -81,13 +90,12 @@ virtual protected async Task<int> GetLastId() {
if (result == 0) return null;
var id = await GetLastId();
player.GangId = id;
await PlayerMgr.UpdatePlayer(player);
await playerMgr.UpdatePlayer(player);
var gang = new DBGang(id, name);
CachedGangs.Add(gang);
return gang.Clone() as IGang;
}

public override void Dispose() {
public void Dispose() {
Transaction?.Dispose();
Connection.Dispose();
}
Expand Down
2 changes: 1 addition & 1 deletion GangsImpl/AbstractDB/DBGang.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ namespace GenericDB;
/// Dapper-compatible representation of a gang.
/// </summary>
public class DBGang(int id, string name) : MockGang(id, name) {
// public DBGang(IGang gang) : this(gang.GangId, gang.Name) { }
public DBGang() : this(0, "") { }
}
5 changes: 3 additions & 2 deletions GangsImpl/SQLite/SQLiteGangManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Data.Common;
using Dapper;
using GangsAPI.Data.Gang;
using GangsAPI.Services.Player;
using GenericDB;
using Microsoft.Data.Sqlite;
Expand All @@ -11,8 +12,8 @@ public class SQLiteGangManager(IPlayerManager playerMgr,
: AbstractDBGangManager(playerMgr, connectionString, table, testing) {
override protected string CreateTableQuery(string tableName, bool inTesting) {
return inTesting ?
$"CREATE TEMPORARY TABLE IF NOT EXISTS {tableName} (GangId INT PRIMARY KEY, Name VARCHAR(255) NOT NULL)" :
$"CREATE TABLE IF NOT EXISTS {tableName} (GangId INT PRIMARY KEY, Name VARCHAR(255) NOT NULL)";
$"CREATE TEMPORARY TABLE IF NOT EXISTS {tableName} (GangId INTEGER PRIMARY KEY, Name VARCHAR(255) NOT NULL)" :
$"CREATE TABLE IF NOT EXISTS {tableName} (GangId INTEGER PRIMARY KEY, Name VARCHAR(255) NOT NULL)";
}

override protected Task<int> GetLastId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ public async Task Create_Already_Ganged() {
public async Task Create_Already_Ganged_Uncached() {
Assert.Equal(CommandResult.SUCCESS,
await Commands.ProcessCommand(player, "create", "foo bar"));
gangMgr.ClearCache();
await gangMgr.Load();
Assert.Equal(CommandResult.FAILURE,
await Commands.ProcessCommand(player, "create", "bar foo"));
Assert.Contains("You are already in a gang", player.ConsoleOutput);
Expand Down
46 changes: 46 additions & 0 deletions GangsTest/API/Services/Gang/PersistenceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using GangsAPI.Services.Gang;
using GangsAPI.Services.Player;

namespace GangsTest.API.Services.Gang;

public class PersistenceTests {
[Theory]
[ClassData(typeof(TestData))]
public async Task Load_Steam(IGangManager mgr) {
var id = (ulong)new Random().NextInt64();
Assert.NotNull(await mgr.CreateGang("Test Gang", id));
var gang = await mgr.GetGang(id);
Assert.NotNull(gang);
Assert.Equal("Test Gang", gang.Name);
}

[Theory]
[ClassData(typeof(TestData))]
public async Task Load_By_ID(IGangManager mgr) {
var gang =
await mgr.CreateGang("Test Gang", (ulong)new Random().NextInt64());
Assert.NotNull(gang);
var id = gang.GangId;
gang = await mgr.GetGang(id);
Assert.NotNull(gang);
Assert.Equal("Test Gang", gang.Name);
}

[Theory]
[ClassData(typeof(TestData))]
public async Task Load_By_ID_Multiple(IGangManager mgr) {
var gang1 =
await mgr.CreateGang("Test Gang 1", (ulong)new Random().NextInt64());
var gang2 =
await mgr.CreateGang("Test Gang 2", (ulong)new Random().NextInt64());
var gang3 =
await mgr.CreateGang("Test Gang 3", (ulong)new Random().NextInt64());
Assert.NotNull(gang1);
Assert.NotNull(gang2);
Assert.NotNull(gang3);
var id = gang2.GangId;
var gang = await mgr.GetGang(id);
Assert.NotNull(gang);
Assert.Equal("Test Gang 2", gang.Name);
}
}
32 changes: 0 additions & 32 deletions GangsTest/API/Services/Stat/PersistenceTests.cs

This file was deleted.

0 comments on commit 1a104b9

Please sign in to comment.