namespace Supercell.Laser.Server.Database.Cache { using System.Collections.Generic; using Supercell.Laser.Server.Database.Models; public static class AccountCache { private static Dictionary CachedAccounts; private static Thread _thread; public static int Count { get { return CachedAccounts.Count; } } public static void Init() { CachedAccounts = new Dictionary(); _thread = new Thread(Update); _thread.Start(); } public static bool Started = true; private static void Update() { while (Started) { SaveAll(); Thread.Sleep(1000 * 45 * 10); } } public static void SaveAll() { try { foreach (var avatar in CachedAccounts.Values) { try { Accounts.Save(avatar); } catch (Exception ex) { Logger.Error( $"Unhandled exception while saving account {avatar.AccountId}: {ex.Message} trace: {ex.StackTrace}" ); } } } catch (Exception) { } } public static bool IsAccountCached(long id) { return CachedAccounts.ContainsKey(id); } public static Account GetAccount(long id) { if (CachedAccounts.ContainsKey(id)) { return CachedAccounts[id]; } return null; } public static Dictionary GetCachedAccounts() { return CachedAccounts; } public static void Cache(Account account) { if (!CachedAccounts.ContainsKey(account.AccountId)) { CachedAccounts.Add(account.AccountId, account); } else { CachedAccounts[account.AccountId] = account; } } } }