Ejemplo n.º 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));
            }
        }
Ejemplo n.º 2
0
        public override async Task Run()
        {
            DBComponent dbComponent = Game.Scene.GetComponent <DBComponent>();

            foreach (Entity entity in this.Entitys)
            {
                if (entity == null)
                {
                    continue;
                }

                try
                {
                    // 执行保存数据库任务
                    await dbComponent.GetCollection(this.CollectionName).ReplaceOneAsync(s => s.Id == entity.Id, entity, new UpdateOptions {
                        IsUpsert = true
                    });
                }
                catch (Exception e)
                {
                    Log.Debug($"{entity.GetType().Name} {entity.ToJson()}" + e.ToString());
                    this.Tcs.SetException(new Exception($"保存数据失败! {CollectionName} {this.Entitys.ListToString()}", e));
                }
            }
            this.Tcs.SetResult(true);
        }
Ejemplo n.º 3
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));
            }
        }
Ejemplo n.º 4
0
        public override async Task Run()
        {
            DBComponent dbComponent = Game.Scene.GetComponent <DBComponent>();

            try
            {
                // 执行查询数据库任务
                FilterDefinition <Disposer> filterDefinition = new JsonFilterDefinition <Disposer>(this.Json);
                List <Disposer>             disposers        = await dbComponent.GetCollection(this.CollectionName).FindAsync(filterDefinition).Result.ToListAsync();

                this.Tcs.SetResult(disposers);
            }
            catch (Exception e)
            {
                this.Tcs.SetException(new Exception($"查询数据库异常! {CollectionName} {this.Json}", e));
            }
        }
Ejemplo n.º 5
0
        public override async Task Run()
        {
            DBComponent dbComponent = Game.Scene.GetComponent <DBComponent>();

            try
            {
                // 执行保存数据库任务
                await dbComponent.GetCollection(this.CollectionName).ReplaceOneAsync(s => s.Id == this.Disposer.Id, this.Disposer, new UpdateOptions {
                    IsUpsert = true
                });

                this.Tcs.SetResult(true);
            }
            catch (Exception e)
            {
                this.Tcs.SetException(new Exception($"保存数据失败!  {CollectionName} {Id}", e));
            }
        }