using SPTarkov.DI.Annotations; using SPTarkov.Server.Core.Utils.Cloners; namespace SPTarkov.Server.Core.Services; [Injectable(InjectionType.Singleton)] public class InMemoryCacheService(ICloner cloner) { protected readonly Dictionary _cacheData = new(); /// /// Store data into an in-memory object /// /// Key to store data against /// Data to store in cache public void StoreByKey(string key, T dataToCache) { _cacheData[key] = cloner.Clone(dataToCache); } /// /// Retrieve data stored by a key /// /// key /// Stored data public T? GetDataByKey(string key) { if (_cacheData.TryGetValue(key, out var value)) { return (T)value; } return default; } /// /// Does data exist against the provided key /// /// Key to check for data against /// True if exists public bool HasStoredDataByKey(string key) { return _cacheData.ContainsKey(key); } /// /// Remove data stored against key /// /// Key to remove data against public void ClearDataStoredByKey(string key) { _cacheData.Remove(key); } /// /// Remove all data stored /// public void ClearCache() { _cacheData.Clear(); } }