DBQueryTask.cs 1.4 KB
Newer Older
T
tanghai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
using System;
using System.Threading.Tasks;
using MongoDB.Driver;

namespace Model
{
	[ObjectEvent]
	public class DBQueryTaskEvent : ObjectEvent<DBQueryTask>, IAwake<string, TaskCompletionSource<Entity>>
	{
		public void Awake(string collectionName, TaskCompletionSource<Entity> tcs)
		{
			DBQueryTask self = this.Get();
			self.CollectionName = collectionName;
			self.Tcs = tcs;
		}
	}

	public sealed class DBQueryTask : DBTask
	{
		public string CollectionName { get; set; }

		public TaskCompletionSource<Entity> Tcs { get; set; }

		public DBQueryTask(long id): base(id)
		{
		}

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