public override async Task Run() { DBCacheComponent dbCacheComponent = Game.Scene.GetComponent <DBCacheComponent>(); DBComponent dbComponent = Game.Scene.GetComponent <DBComponent>(); List <Entity> result = new List <Entity>(); try { // 执行查询数据库任务 foreach (long id in IdList) { Entity entity = dbCacheComponent.GetFromCache(this.CollectionName, id); if (entity == null) { entity = await dbComponent.GetCollection(this.CollectionName).FindAsync((s) => s.Id == id).Result.FirstOrDefaultAsync(); dbCacheComponent.AddToCache(entity); } if (entity == null) { continue; } result.Add(entity); } this.Tcs.SetResult(result); } catch (Exception e) { this.Tcs.SetException(new Exception($"查询数据库异常! {this.CollectionName} {IdList.ListToString()}", e)); } }
public override async ETTask Run() { DBComponent dbComponent = Game.Scene.GetComponent <DBComponent>(); List <ComponentWithId> result = new List <ComponentWithId>(); try { // 执行查询数据库任务 foreach (long id in IdList) { IAsyncCursor <ComponentWithId> cursor = await dbComponent.GetCollection(this.CollectionName).FindAsync((s) => s.Id == id); ComponentWithId component = await cursor.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 ETTask Run() { DBComponent dbComponent = Game.Scene.GetComponent <DBComponent>(); long deleteCount = 0; try { // 执行删除数据库任务 foreach (long id in IdList) { DeleteResult deleteResult = await dbComponent.GetCollection(this.CollectionName).DeleteManyAsync((s) => s.Id == id); deleteCount += deleteResult.DeletedCount; } this.Tcs.SetResult(deleteCount); } catch (Exception e) { this.Tcs.SetException(new Exception($"删除数据库异常! {this.CollectionName} {IdList.ListToString()}", e)); } }