Skip to content

Cache objects from database in memory to improve performance #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 66 additions & 3 deletions gaseous-server/Classes/Metadata/Storage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using gaseous_tools;
using IGDB;
using IGDB.Models;
using Microsoft.Extensions.Caching.Memory;

namespace gaseous_server.Classes.Metadata
{
Expand All @@ -16,14 +17,32 @@ public enum CacheStatus
Expired
}

private static Dictionary<string, MemoryCacheObject> ObjectCache = new Dictionary<string, MemoryCacheObject>();

public static CacheStatus GetCacheStatus(string Endpoint, string Slug)
{
return _GetCacheStatus(Endpoint, "slug", Slug);
CacheClean();
if (ObjectCache.ContainsKey(Endpoint + Slug))
{
return CacheStatus.Current;
}
else
{
return _GetCacheStatus(Endpoint, "slug", Slug);
}
}

public static CacheStatus GetCacheStatus(string Endpoint, long Id)
{
return _GetCacheStatus(Endpoint, "id", Id);
CacheClean();
if (ObjectCache.ContainsKey(Endpoint + Id))
{
return CacheStatus.Current;
}
else
{
return _GetCacheStatus(Endpoint, "id", Id);
}
}

public static CacheStatus GetCacheStatus(DataRow Row)
Expand Down Expand Up @@ -164,6 +183,21 @@ public static T GetCacheValue<T>(T EndpointType, string SearchField, object Sear
{
string Endpoint = EndpointType.GetType().Name;

if (ObjectCache.ContainsKey(Endpoint + SearchValue))
{
MemoryCacheObject cacheObject = ObjectCache[Endpoint + SearchValue];
if (cacheObject.ExpiryTime < DateTime.UtcNow)
{
// object has expired, remove it
ObjectCache.Remove(Endpoint + SearchValue);
}
else
{
// object is valid, return it
return (T)cacheObject.Object;
}
}

Database db = new gaseous_tools.Database(Database.databaseType.MySql, Config.DatabaseConfiguration.ConnectionString);

string sql = "SELECT * FROM " + Endpoint + " WHERE " + SearchField + " = @" + SearchField;
Expand All @@ -181,7 +215,11 @@ public static T GetCacheValue<T>(T EndpointType, string SearchField, object Sear
else
{
DataRow dataRow = dt.Rows[0];
return BuildCacheObject<T>(EndpointType, dataRow);
object returnObject = BuildCacheObject<T>(EndpointType, dataRow);
ObjectCache.Add(Endpoint + SearchValue, new MemoryCacheObject{
Object = returnObject
});
return (T)returnObject;
}
}

Expand Down Expand Up @@ -380,6 +418,31 @@ public static T BuildCacheObject<T>(T EndpointType, DataRow dataRow)

return EndpointType;
}

private static void CacheClean()
{
Dictionary<string, MemoryCacheObject> workCache = ObjectCache;
foreach (KeyValuePair<string, MemoryCacheObject> objectCache in workCache)
{
if (objectCache.Value.ExpiryTime < DateTime.UtcNow)
{
ObjectCache.Remove(objectCache.Key);
}
}
}

private class MemoryCacheObject
{
public object Object { get; set; }
public DateTime CreationTime { get; } = DateTime.UtcNow;
public DateTime ExpiryTime
{
get
{
return CreationTime.AddMinutes(60);
}
}
}
}
}

59 changes: 45 additions & 14 deletions gaseous-server/Models/PlatformMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ namespace gaseous_server.Models
{
public class PlatformMapping
{
private static Dictionary<string, PlatformMapItem> PlatformMapCache = new Dictionary<string, PlatformMapItem>();

/// <summary>
/// Updates the platform map from the embedded platform map resource
/// </summary>
Expand Down Expand Up @@ -98,7 +100,15 @@ public static List<PlatformMapItem> PlatformMap
List<PlatformMapItem> platformMaps = new List<PlatformMapItem>();
foreach (DataRow row in data.Rows)
{
platformMaps.Add(BuildPlatformMapItem(row));
long mapId = (long)row["Id"];
if (PlatformMapCache.ContainsKey(mapId.ToString()))
{
platformMaps.Add(PlatformMapCache[mapId.ToString()]);
}
else
{
platformMaps.Add(BuildPlatformMapItem(row));
}
}

platformMaps.Sort((x, y) => x.IGDBName.CompareTo(y.IGDBName));
Expand All @@ -109,23 +119,30 @@ public static List<PlatformMapItem> PlatformMap

public static PlatformMapItem GetPlatformMap(long Id)
{
Database db = new gaseous_tools.Database(Database.databaseType.MySql, Config.DatabaseConfiguration.ConnectionString);
string sql = "SELECT * FROM PlatformMap WHERE Id = @Id";
Dictionary<string, object> dbDict = new Dictionary<string, object>();
dbDict.Add("Id", Id);
DataTable data = db.ExecuteCMD(sql, dbDict);

if (data.Rows.Count > 0)
if (PlatformMapCache.ContainsKey(Id.ToString()))
{
PlatformMapItem platformMap = BuildPlatformMapItem(data.Rows[0]);

return platformMap;
return PlatformMapCache[Id.ToString()];
}
else
{
Exception exception = new Exception("Platform Map Id " + Id + " does not exist.");
Logging.Log(Logging.LogType.Critical, "Platform Map", "Platform Map Id " + Id + " does not exist.", exception);
throw exception;
Database db = new gaseous_tools.Database(Database.databaseType.MySql, Config.DatabaseConfiguration.ConnectionString);
string sql = "SELECT * FROM PlatformMap WHERE Id = @Id";
Dictionary<string, object> dbDict = new Dictionary<string, object>();
dbDict.Add("Id", Id);
DataTable data = db.ExecuteCMD(sql, dbDict);

if (data.Rows.Count > 0)
{
PlatformMapItem platformMap = BuildPlatformMapItem(data.Rows[0]);

return platformMap;
}
else
{
Exception exception = new Exception("Platform Map Id " + Id + " does not exist.");
Logging.Log(Logging.LogType.Critical, "Platform Map", "Platform Map Id " + Id + " does not exist.", exception);
throw exception;
}
}
}

Expand Down Expand Up @@ -218,6 +235,11 @@ public static void WritePlatformMap(PlatformMapItem item, bool Update, bool Allo
db.ExecuteCMD(sql, dbDict);
}
}

if (PlatformMapCache.ContainsKey(item.IGDBId.ToString()))
{
PlatformMapCache.Remove(item.IGDBId.ToString());
}
}

static PlatformMapItem BuildPlatformMapItem(DataRow row)
Expand Down Expand Up @@ -321,6 +343,15 @@ static PlatformMapItem BuildPlatformMapItem(DataRow row)
};
mapItem.Bios = bioss;

if (PlatformMapCache.ContainsKey(IGDBId.ToString()))
{
PlatformMapCache[IGDBId.ToString()] = mapItem;
}
else
{
PlatformMapCache.Add(IGDBId.ToString(), mapItem);
}

return mapItem;
}

Expand Down