예제 #1
0
파일: MessageQueue.cs 프로젝트: zgren/dp2
        public async Task <Message> GetAsync(bool remove_items,
                                             CancellationToken token = default)
        {
            using (var releaser = await _databaseLimit.EnterAsync())
            {
                using (var context = new QueueContext(_databaseFileName))
                {
                    List <QueueItem> items = new List <QueueItem>();

                    var first = context.Items.OrderBy(o => o.ID).FirstOrDefault();
                    if (first == null)
                    {
                        return(null);
                    }

                    items.Add(first);
                    if (string.IsNullOrEmpty(first.GroupID))
                    {
                        return new Message {
                                   Content = first.Content
                        }
                    }
                    ;
                    // 取出所有 GroupID 相同的事项,然后拼接起来
                    var         group_id = first.GroupID;
                    List <byte> bytes    = new List <byte>(first.Content);

                    int id = first.ID;
                    while (true)
                    {
                        var current = context.Items.Where(o => o.ID > id).OrderBy(o => o.ID).FirstOrDefault();

                        if (current == null)
                        {
                            break;
                        }
                        if (current.GroupID != group_id)
                        {
                            break;
                        }
                        bytes.AddRange(current.Content);
                        id = current.ID;

                        items.Add(current);
                    }

                    // 删除涉及到的事项
                    if (remove_items)
                    {
                        context.Items.RemoveRange(items);
                        await context.SaveChangesAsync(token);
                    }

                    return(new Message {
                        Content = bytes.ToArray()
                    });
                }
            }
        }
예제 #2
0
파일: MessageQueue.cs 프로젝트: zgren/dp2
 public async Task PushAsync(List <string> texts,
                             CancellationToken token = default)
 {
     using (var releaser = await _databaseLimit.EnterAsync())
     {
         using (var context = new QueueContext(_databaseFileName))
         {
             foreach (string text in texts)
             {
                 context.Items.AddRange(BuildItem(text));
             }
             await context.SaveChangesAsync(token).ConfigureAwait(false);
         }
     }
 }
예제 #3
0
파일: MessageQueue.cs 프로젝트: zgren/dp2
 public async Task PushAsync(List <byte[]> contents,
                             CancellationToken token = default)
 {
     using (var releaser = await _databaseLimit.EnterAsync())
     {
         using (var context = new QueueContext(_databaseFileName))
         {
             foreach (var content in contents)
             {
                 // 注意,这里每次 Add() 以后都要及时 SaveChanges()。否则 ID 的顺序会发生混乱
                 var items = BuildItem(content);
                 foreach (var item in items)
                 {
                     context.Items.Add(item);
                     await context.SaveChangesAsync(token);
                 }
             }
         }
     }
 }