Пример #1
0
 public LocalMessageSet(string dbFileFullName)
 {
     if (!string.IsNullOrEmpty(dbFileFullName))
     {
         _connectionString = $"filename={dbFileFullName};journal=false";
     }
     VirtualRoot.AddCmdPath <AddLocalMessageCommand>(action: message => {
         if (string.IsNullOrEmpty(_connectionString))
         {
             return;
         }
         InitOnece();
         var data = LocalMessageData.Create(message.Input);
         // TODO:批量持久化,异步持久化
         List <ILocalMessage> removes = new List <ILocalMessage>();
         lock (_locker) {
             _records.AddFirst(data);
             while (_records.Count > NTKeyword.LocalMessageSetCapacity)
             {
                 var toRemove = _records.Last;
                 removes.Add(toRemove.Value);
                 _records.RemoveLast();
                 using (LiteDatabase db = new LiteDatabase(_connectionString)) {
                     var col = db.GetCollection <LocalMessageData>();
                     col.Delete(toRemove.Value.Id);
                 }
             }
         }
         using (LiteDatabase db = new LiteDatabase(_connectionString)) {
             var col = db.GetCollection <LocalMessageData>();
             col.Insert(data);
         }
         VirtualRoot.RaiseEvent(new LocalMessageAddedEvent(message.Id, data, removes));
     }, location: this.GetType());
     VirtualRoot.AddCmdPath <ClearLocalMessageSetCommand>(action: message => {
         if (string.IsNullOrEmpty(_connectionString))
         {
             return;
         }
         lock (_locker) {
             _records.Clear();
         }
         using (LiteDatabase db = new LiteDatabase(_connectionString)) {
             db.DropCollection(nameof(LocalMessageData));
         }
         VirtualRoot.RaiseEvent(new LocalMessageSetClearedEvent());
     }, location: this.GetType());
 }
Пример #2
0
 public LocalMessageSet()
 {
     if (ClientAppType.IsMinerClient)
     {
         LocalMessageDtoSet = new LocalMessageDtoSet();
     }
     VirtualRoot.BuildCmdPath <AddLocalMessageCommand>(location: this.GetType(), LogEnum.DevConsole, path: message => {
         InitOnece();
         var data = LocalMessageData.Create(message.Input);
         List <ILocalMessage> removeds = new List <ILocalMessage>();
         lock (_dbToInserts) {
             _records.AddFirst(data);
             _dbToInserts.Add(data);
             while (_records.Count > NTKeyword.LocalMessageSetCapacity)
             {
                 var toRemove = _records.Last.Value;
                 removeds.Add(toRemove);
                 _records.RemoveLast();
             }
             _dbToRemoveIds.AddRange(removeds.Select(a => a.Id));
         }
         LocalMessageDtoSet.Add(data.ToDto());
         VirtualRoot.RaiseEvent(new LocalMessageAddedEvent(message.MessageId, data, removeds));
     });
     VirtualRoot.BuildCmdPath <ClearLocalMessageSetCommand>(location: this.GetType(), LogEnum.DevConsole, path: message => {
         lock (_dbToInserts) {
             _records.Clear();
             _dbToRemoveIds.Clear();
             _dbToInserts.Clear();
         }
         try {
             using (LiteDatabase db = new LiteDatabase(ConnString)) {
                 db.DropCollection(nameof(LocalMessageData));
             }
         }
         catch (Exception e) {
             Logger.ErrorDebugLine(e);
         }
         VirtualRoot.RaiseEvent(new LocalMessageSetClearedEvent());
     });
     VirtualRoot.BuildEventPath <Per1MinuteEvent>("周期保存LocalMessage到数据库", LogEnum.DevConsole, this.GetType(), PathPriority.Normal, path: message => {
         SaveToDb();
     });
     VirtualRoot.BuildEventPath <AppExitEvent>("程序退出时保存LocalMessage到数据库", LogEnum.DevConsole, this.GetType(), PathPriority.Normal, path: message => {
         SaveToDb();
     });
 }
Пример #3
0
        public void Add(string channel, string provider, string messageType, string content)
        {
            if (string.IsNullOrEmpty(_connectionString))
            {
                return;
            }
            InitOnece();
            var data = new LocalMessageData {
                Id          = Guid.NewGuid(),
                Channel     = channel,
                Provider    = provider,
                MessageType = messageType,
                Content     = content,
                Timestamp   = DateTime.Now
            };
            // TODO:批量持久化,异步持久化
            List <ILocalMessage> removes = new List <ILocalMessage>();

            lock (_locker) {
                _records.AddFirst(data);
                while (_records.Count > NTKeyword.LocalMessageSetCapacity)
                {
                    var toRemove = _records.Last;
                    removes.Add(toRemove.Value);
                    _records.RemoveLast();
                    using (LiteDatabase db = new LiteDatabase(_connectionString)) {
                        var col = db.GetCollection <LocalMessageData>();
                        col.Delete(toRemove.Value.Id);
                    }
                }
            }
            using (LiteDatabase db = new LiteDatabase(_connectionString)) {
                var col = db.GetCollection <LocalMessageData>();
                col.Insert(data);
            }
            VirtualRoot.RaiseEvent(new LocalMessageAddedEvent(data, removes));
        }