using System; using System.Threading.Tasks; using MongoDB.Driver; namespace Model { [ObjectEvent] public class DBQueryTaskEvent : ObjectEvent, IAwake> { public void Awake(string collectionName, TaskCompletionSource tcs) { DBQueryTask self = this.Get(); self.CollectionName = collectionName; self.Tcs = tcs; } } public sealed class DBQueryTask : DBTask { public string CollectionName { get; set; } public TaskCompletionSource Tcs { get; set; } public DBQueryTask(long id): base(id) { } public override async Task Run() { DBCacheComponent dbCacheComponent = Game.Scene.GetComponent(); DBComponent dbComponent = Game.Scene.GetComponent(); // 执行查询前先看看cache中是否已经存在 Disposer disposer = dbCacheComponent.GetFromCache(this.CollectionName, this.Id); if (disposer != null) { this.Tcs.SetResult(disposer); return; } try { // 执行查询数据库任务 disposer = await dbComponent.GetCollection(this.CollectionName).FindAsync((s) => s.Id == this.Id).Result.FirstOrDefaultAsync(); if (disposer != null) { dbCacheComponent.AddToCache(disposer); } this.Tcs.SetResult(disposer); } catch (Exception e) { this.Tcs.SetException(new Exception($"查询数据库异常! {CollectionName} {Id}", e)); } } } }