Skip to content
Open
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
33 changes: 21 additions & 12 deletions Domain/RaceDataManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RaceData> 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<HoldingDatum>();
Expand Down Expand Up @@ -58,8 +67,8 @@ public static IEnumerable<RaceData> GetRaceDataOfDay(DateTime date, bool useHold
public static IEnumerable<RaceData> GetAndStoreRaceDataOfDay(DateTime date, Scraper scraper)
{
var holdingInformationRepository = new HoldingInformationRepository();
var currentHoldingInformation = holdingInformationRepository.ReadAll() ?? new HoldingInformation(new List<HoldingDatum>());
var currentHoldingInformation = holdingInformationRepository.ReadAll() ?? new HoldingInformation(new List<HoldingDatum>());


if (!currentHoldingInformation.HoldingData.Any(_ => _.HeldDate.Date == date.Date))
{
Expand Down