コード例 #1
0
        /// <summary>
        /// Tries to load data from the population report cache
        /// </summary>
        /// <returns>True if data is loaded; otherwise - false</returns>
        private bool TryLoadCachedData()
        {
            if (populationData != null)
            {
                return(true);
            }

            lock (syncLock)
            {
                if (populationData != null)
                {
                    return(true);
                }

                var reportFileInfo = new FileInfo(ReportFile);

                if (reportFileInfo.Exists)
                {
                    resetOnRead = reportFileInfo.Length > MaxReportFileSize;

                    try
                    {
                        using (var fs = new FileStream(ReportFile, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
                        {
                            populationData = Serializer.Deserialize <PopulationReportCache>(fs);
                            LoadPlayerTypes();
                            return(true);
                        }
                    }
                    catch (Exception ex)
                    {
                        LogProvider.Log.Error(this, $"Population report could not be loaded from '{ReportFile}'", ex);
                    }
                }

                populationData = new PopulationReportCache
                {
                    Report = new List <PopulationReportIndicators>()
                };
            }

            return(false);
        }
コード例 #2
0
        /// <summary>
        /// Gets population data
        /// </summary>
        public IEnumerable <ReportIndicators> GetReport(bool forceRefresh, CancellationToken cancellationToken)
        {
            try
            {
                if (!TryLoadCachedData())
                {
                    ValidateCache();
                    BuildReportData(cancellationToken);
                }
                else if (!ValidateCache() || forceRefresh)
                {
                    BuildReportData(cancellationToken);
                }

                return(populationData.Report.ToList());
            }
            finally
            {
                if (resetOnRead)
                {
                    populationData = null;
                }
            }
        }