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()); }
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(); }); }