Example #1
0
        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));
            }
        }
Example #2
0
        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));
            }
        }