diff --git a/Tools/BiblioTech/Rewards/RoleModifyContext.cs b/Tools/BiblioTech/Rewards/RoleModifyContext.cs index 3fbc5132..9b38dcea 100644 --- a/Tools/BiblioTech/Rewards/RoleModifyContext.cs +++ b/Tools/BiblioTech/Rewards/RoleModifyContext.cs @@ -10,6 +10,8 @@ namespace BiblioTech.Rewards { private Dictionary users = new(); private Dictionary roles = new(); + private DateTime lastLoad = DateTime.MinValue; + private readonly SocketGuild guild; private readonly UserRepo userRepo; private readonly ILog log; @@ -25,8 +27,14 @@ namespace BiblioTech.Rewards public async Task Initialize() { - this.users = await LoadAllUsers(guild); - this.roles = LoadAllRoles(guild); + var span = DateTime.UtcNow - lastLoad; + if (span > TimeSpan.FromMinutes(10)) + { + lastLoad = DateTime.UtcNow; + log.Log("Loading all users and roles..."); + this.users = await LoadAllUsers(guild); + this.roles = LoadAllRoles(guild); + } } public IGuildUser[] Users => users.Values.ToArray(); @@ -76,7 +84,6 @@ namespace BiblioTech.Rewards private async Task> LoadAllUsers(SocketGuild guild) { - log.Log("Loading all users.."); var result = new Dictionary(); var users = guild.GetUsersAsync(); await foreach (var ulist in users) diff --git a/Tools/BiblioTech/UserRepo.cs b/Tools/BiblioTech/UserRepo.cs index 07831dd6..701fdfe9 100644 --- a/Tools/BiblioTech/UserRepo.cs +++ b/Tools/BiblioTech/UserRepo.cs @@ -122,20 +122,17 @@ namespace BiblioTech { if (address == null) return null; - // If this becomes a performance problem, switch to in-memory cached list. - var files = Directory.GetFiles(Program.Config.UserDataPath); - foreach (var file in files) + var lower = address.Address.ToLowerInvariant(); + if (string.IsNullOrEmpty(lower)) return null; + + if (cache.Count == 0) LoadAllUserData(); + foreach (var item in cache.Values) { - try + if (item.CurrentAddress != null && + item.CurrentAddress.Address.ToLowerInvariant() == lower) { - var user = JsonConvert.DeserializeObject(File.ReadAllText(file))!; - if (user.CurrentAddress != null && - user.CurrentAddress.Address == address.Address) - { - return user; - } + return item; } - catch { } } return null;