Example #1
0
        private async Task GetOrAddOneCooperateItem(CancellationToken cancellationToken, string oneId)
        {
            using (var cooperateContext = new CooperateContext(_options))
            {
                try
                {
                    var findRes = await cooperateContext.CooperateEntities.FindAsync(oneId);

                    if (findRes != null)
                    {
                        var item = _cooperateItems[oneId];
                        item.ExpireKeyValuePair   = new KeyValuePair <bool, DateTime>(false, findRes.ExpireDateTime);
                        _cooperateEntities[oneId] = findRes;
                    }
                    else
                    {
                        var now    = DateTime.UtcNow;
                        var entity = new CooperateEntity
                        {
                            Id = oneId,
                            LastModifyDateTime = now,
                            ExpireDateTime     = now + _cooperateRequest.AliveTimeSpan
                        };
                        var res = await cooperateContext.CooperateEntities.AddAsync(entity, cancellationToken);

                        await cooperateContext.SaveChangesAsync(cancellationToken);

                        _cooperateEntities[oneId] = entity;
                        var item = _cooperateItems[oneId];
                        item.ExpireKeyValuePair = new KeyValuePair <bool, DateTime>(true, entity.ExpireDateTime);
                    }
                }
                catch (DbUpdateException)
                {
                    var findRes = await cooperateContext.CooperateEntities.FindAsync(oneId);

                    if (findRes != null)
                    {
                        var item = _cooperateItems[oneId];
                        item.ExpireKeyValuePair   = new KeyValuePair <bool, DateTime>(false, findRes.ExpireDateTime);
                        _cooperateEntities[oneId] = findRes;
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
Example #2
0
        private async Task UpdateCooperateEntity(CancellationToken cancellationToken, string oneId, DateTime now)
        {
            using (var cooperateContext = new CooperateContext(_options))
            {
                try
                {
                    var entity     = _cooperateEntities[oneId];
                    var backEntity = new CooperateEntity
                    {
                        Id = entity.Id,
                        LastModifyDateTime = entity.LastModifyDateTime,
                        ExpireDateTime     = entity.ExpireDateTime
                    };

                    var nowEntity = await cooperateContext.CooperateEntities.FindAsync(oneId);

                    if (nowEntity.LastModifyDateTime > backEntity.LastModifyDateTime)
                    {
                        _cooperateEntities[oneId] = nowEntity;
                        var item = _cooperateItems[oneId];
                        item.ExpireKeyValuePair = new KeyValuePair <bool, DateTime>(false, nowEntity.ExpireDateTime);
                        return;
                    }
                    else
                    {
                        nowEntity.LastModifyDateTime = now;
                        nowEntity.ExpireDateTime     = now + _cooperateRequest.AliveTimeSpan;

                        cooperateContext.Update(nowEntity);
                        await cooperateContext.SaveChangesAsync(cancellationToken);

                        _cooperateEntities[oneId] = nowEntity;
                        var item = _cooperateItems[oneId];
                        item.ExpireKeyValuePair = new KeyValuePair <bool, DateTime>(true, nowEntity.ExpireDateTime);
                    }
                }
                catch (DbUpdateConcurrencyException)
                {
                    var entity = await cooperateContext.CooperateEntities.FindAsync(oneId);

                    _cooperateEntities[oneId] = entity;
                    var item = _cooperateItems[oneId];
                    item.ExpireKeyValuePair = new KeyValuePair <bool, DateTime>(false, entity.ExpireDateTime);
                }
            }
        }