diff --git a/Domain/RaceDataManager.cs b/Domain/RaceDataManager.cs index 2725b2f..2f7e753 100644 --- a/Domain/RaceDataManager.cs +++ b/Domain/RaceDataManager.cs @@ -12,23 +12,32 @@ namespace HorseRacingAutoPurchaser.Domain { public class RaceDataManager { - private static HoldingInformation CachedHoldingInformation { get; set; } + private static volatile HoldingInformation CachedHoldingInformation; + private static readonly object _cacheLock = new object(); - public static void ResetCache() - { - CachedHoldingInformation = null; + public static void ResetCache() + { + lock (_cacheLock) + { + CachedHoldingInformation = null; + } } public static IEnumerable GetRaceDataOfDay(DateTime date, bool useHoldingInformationCache = false) { var holdingInformationRepository = new HoldingInformationRepository(); HoldingInformation currentHoldingInformation; - if (useHoldingInformationCache && CachedHoldingInformation != null) { - currentHoldingInformation = CachedHoldingInformation; - } - else { - currentHoldingInformation = holdingInformationRepository.ReadAll(); - CachedHoldingInformation = currentHoldingInformation; + lock (_cacheLock) + { + if (useHoldingInformationCache && CachedHoldingInformation != null) + { + currentHoldingInformation = CachedHoldingInformation; + } + else + { + currentHoldingInformation = holdingInformationRepository.ReadAll(); + CachedHoldingInformation = currentHoldingInformation; + } } var holdingData = new List(); @@ -58,8 +67,8 @@ public static IEnumerable GetRaceDataOfDay(DateTime date, bool useHold public static IEnumerable GetAndStoreRaceDataOfDay(DateTime date, Scraper scraper) { var holdingInformationRepository = new HoldingInformationRepository(); - var currentHoldingInformation = holdingInformationRepository.ReadAll() ?? new HoldingInformation(new List()); - + var currentHoldingInformation = holdingInformationRepository.ReadAll() ?? new HoldingInformation(new List()); + if (!currentHoldingInformation.HoldingData.Any(_ => _.HeldDate.Date == date.Date)) {