public override async Task Run() { DBCacheComponent dbCacheComponent = Game.Scene.GetComponent <DBCacheComponent>(); DBComponent dbComponent = Game.Scene.GetComponent <DBComponent>(); List <ComponentWithId> result = new List <ComponentWithId>(); try { // 执行查询数据库任务 foreach (long id in IdList) { ComponentWithId component = dbCacheComponent.GetFromCache(this.CollectionName, id); if (component == null) { component = await dbComponent.GetCollection(this.CollectionName).FindAsync((s) => s.Id == id).Result.FirstOrDefaultAsync(); } if (component == null) { continue; } result.Add(component); } this.Tcs.SetResult(result); } catch (Exception e) { this.Tcs.SetException(new Exception($"查询数据库异常! {this.CollectionName} {IdList.ListToString()}", e)); } }
public override async Task Run() { DBCacheComponent dbCacheComponent = Game.Scene.GetComponent <DBCacheComponent>(); DBComponent dbComponent = Game.Scene.GetComponent <DBComponent>(); // 执行查询前先看看cache中是否已经存在 ComponentWithId component = dbCacheComponent.GetFromCache(this.CollectionName, this.Id); if (component != null) { this.Tcs.SetResult(component); return; } try { // 执行查询数据库任务 component = await dbComponent.GetCollection(this.CollectionName).FindAsync((s) => s.Id == this.Id).Result.FirstOrDefaultAsync(); if (component != null) { dbCacheComponent.AddToCache(component); } this.Tcs.SetResult(component); } catch (Exception e) { this.Tcs.SetException(new Exception($"查询数据库异常! {CollectionName} {Id}", e)); } }