Move ApplicationContext to use ConcurrentDictionary
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
using SPTarkov.Common.Annotations;
|
||||
using System.Collections.Concurrent;
|
||||
using SPTarkov.Common.Annotations;
|
||||
using SPTarkov.Server.Core.Models.Utils;
|
||||
|
||||
namespace SPTarkov.Server.Core.Context;
|
||||
|
||||
@@ -6,17 +8,21 @@ namespace SPTarkov.Server.Core.Context;
|
||||
public class ApplicationContext
|
||||
{
|
||||
protected const short MaxSavedValues = 10;
|
||||
protected readonly Dictionary<ContextVariableType, LinkedList<ContextVariable>> variables = new();
|
||||
private readonly Lock _lockObject = new();
|
||||
protected readonly ConcurrentDictionary<ContextVariableType, LinkedList<ContextVariable>> variables = new();
|
||||
|
||||
private static ApplicationContext? _applicationContext;
|
||||
private readonly ISptLogger<ApplicationContext> _logger;
|
||||
|
||||
/// <summary>
|
||||
/// When ApplicationContext gets created by the DI container we store the singleton reference so we can provide it
|
||||
/// statically for harmony patches!
|
||||
/// </summary>
|
||||
public ApplicationContext()
|
||||
public ApplicationContext
|
||||
(
|
||||
ISptLogger<ApplicationContext> logger
|
||||
)
|
||||
{
|
||||
_logger = logger;
|
||||
_applicationContext = this;
|
||||
}
|
||||
|
||||
@@ -27,12 +33,9 @@ public class ApplicationContext
|
||||
|
||||
public ContextVariable? GetLatestValue(ContextVariableType type)
|
||||
{
|
||||
lock (_lockObject)
|
||||
if (variables.TryGetValue(type, out var savedValues))
|
||||
{
|
||||
if (variables.TryGetValue(type, out var savedValues))
|
||||
{
|
||||
return savedValues.Last!.Value;
|
||||
}
|
||||
return savedValues.Last!.Value;
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -40,42 +43,39 @@ public class ApplicationContext
|
||||
|
||||
public ICollection<ContextVariable> GetValues(ContextVariableType type)
|
||||
{
|
||||
lock (_lockObject)
|
||||
var values = new List<ContextVariable>();
|
||||
if (variables.TryGetValue(type, out var savedValues))
|
||||
{
|
||||
var values = new List<ContextVariable>();
|
||||
if (variables.TryGetValue(type, out var savedValues))
|
||||
{
|
||||
values.AddRange(savedValues);
|
||||
}
|
||||
|
||||
return values;
|
||||
values.AddRange(savedValues);
|
||||
}
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
public void AddValue(ContextVariableType type, object value)
|
||||
{
|
||||
lock (_lockObject)
|
||||
if (!variables.TryGetValue(type, out var savedValues))
|
||||
{
|
||||
if (!variables.TryGetValue(type, out var savedValues))
|
||||
savedValues = [];
|
||||
if (!variables.TryAdd(type, savedValues))
|
||||
{
|
||||
savedValues = [];
|
||||
variables.Add(type, savedValues);
|
||||
_logger.Error($"Unable to add context variable type: {type}");
|
||||
}
|
||||
|
||||
if (savedValues.Count >= MaxSavedValues)
|
||||
{
|
||||
savedValues.RemoveFirst();
|
||||
}
|
||||
|
||||
savedValues.AddLast(new ContextVariable(value, type));
|
||||
}
|
||||
|
||||
if (savedValues.Count >= MaxSavedValues)
|
||||
{
|
||||
savedValues.RemoveFirst();
|
||||
}
|
||||
|
||||
savedValues.AddLast(new ContextVariable(value, type));
|
||||
}
|
||||
|
||||
public void ClearValues(ContextVariableType type)
|
||||
{
|
||||
lock (_lockObject)
|
||||
if (!variables.Remove(type, out _))
|
||||
{
|
||||
variables.Remove(type);
|
||||
_logger.Error($"Unable to clear context variable type: {type}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user