internal static void SaveStatsToDatabase(BannerTotalStat[] bannerStatsToBeSaved, DateTime today){ foreach (BannerTotalStat stat in bannerStatsToBeSaved) { lock (stat) { Bobs.StoredProcedures.Bobs.BannerStat.Log.Execute(stat.BannerK, today, stat.Hits, stat.UniqueVisitors, stat.Clicks); } } }
public static void Log(int bannerK, Banner.Positions position, DateTime date, int hits, int uniqueHits, int clicks) { if (Common.Settings.BatchBannerStatUpdates) { BannerTotalStat stat = null; lock (bannerStatTemp) { if (bannerStatTemp.ContainsKey(bannerK)) { stat = bannerStatTemp[bannerK]; } else { stat = new BannerTotalStat() { BannerK = bannerK }; bannerStatTemp.Add(bannerK, stat); } System.Threading.Monitor.Enter(stat); } try { stat.Clicks += clicks; stat.Hits += hits; stat.UniqueVisitors += uniqueHits; } finally { System.Threading.Monitor.Exit(stat); } lock (lastSavedToDatabaseLock) { if (lastSavedToDatabase < Time.Now.Subtract(Time.Minutes(1))) { lock (bannerStatTemp) { BannerTotalStat[] bannerStats = new BannerTotalStat[bannerStatTemp.Count]; bannerStatTemp.Values.CopyTo(bannerStats, 0); bannerStatTemp.Clear(); System.Threading.Thread thread = Utilities.GetSafeThread(() => SaveStatsToDatabase(bannerStats, lastSavedToDatabase)); thread.Start(); lastSavedToDatabase = Time.Now; } } } } else { Bobs.StoredProcedures.Bobs.BannerStat.Log.Execute(bannerK, DateTime.Today, hits, uniqueHits, clicks); } }