/// <summary> /// /// </summary> /// <param name="storageName">保存的名称</param> /// <param name="primaryPropertyName">主键属性的名称,必须是long、int、string类型</param> /// <param name="asyncSaveFile">是否异步同步到文件当中,true写入性能较快,但数据存在丢失风险</param> /// <param name="checkRepeatPrimaryKey">是否检查主键重复,如果为true,会对写入性能有影响</param> /// <param name="logger"></param> public StorageContext(string storageName, string primaryPropertyName, bool asyncSaveFile = true, bool checkRepeatPrimaryKey = false, ILogger logger = null) { var filepath = "./Jack.Storage.MemoryObjects.datas/" + storageName + ".db"; _checkRepeatPrimaryKey = checkRepeatPrimaryKey; if (string.IsNullOrEmpty(filepath)) { throw new Exception("filepath is empty"); } if (string.IsNullOrEmpty(primaryPropertyName)) { throw new Exception("primaryPropertyName is empty"); } _propertyInfo = typeof(T).GetProperty(primaryPropertyName, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); if (_propertyInfo == null) { throw new Exception($"{typeof(T).FullName} can not find property: {primaryPropertyName}"); } if (_propertyInfo.PropertyType != typeof(string) && _propertyInfo.PropertyType != typeof(int) && _propertyInfo.PropertyType != typeof(long) ) { throw new Exception("主键属性必须是long、int、string类型"); } this.StorageName = storageName; _db = new StorageDB <T>(filepath, _propertyInfo, logger); _db.ReadData <T>((item) => { _dataList.Add(new DataItem <T>(item)); }); if (asyncSaveFile) { _storageQueue = new Local.AsyncStorageQueue <T>(_dataList, filepath, _propertyInfo, logger); } else { _storageQueue = new Local.SyncStorageQueue <T>(_dataList, filepath, _propertyInfo, logger); } }
/// <summary> /// /// </summary> /// <param name="storageName">保存的名称</param> /// <param name="primaryPropertyName">主键属性的名称,必须是long、int、string类型</param> /// <param name="checkRepeatPrimaryKey">是否检查主键重复,如果为true,会对写入性能有影响</param> public StorageContext(string storageName, string primaryPropertyName, bool checkRepeatPrimaryKey = false) { var filepath = "./Jack.Storage.MemoryObjects.datas/" + storageName + ".db"; _checkRepeatPrimaryKey = checkRepeatPrimaryKey; if (string.IsNullOrEmpty(filepath)) { throw new Exception("filepath is empty"); } if (string.IsNullOrEmpty(primaryPropertyName)) { throw new Exception("primaryPropertyName is empty"); } _propertyInfo = typeof(T).GetProperty(primaryPropertyName, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); if (_propertyInfo == null) { throw new Exception($"{typeof(T).FullName} can not find property: {primaryPropertyName}"); } if (_propertyInfo.PropertyType != typeof(string) && _propertyInfo.PropertyType != typeof(int) && _propertyInfo.PropertyType != typeof(long) ) { throw new Exception("主键属性必须是long、int、string类型"); } this.StorageName = storageName; _db = new StorageDB(filepath, _propertyInfo); _db.ReadData <T>((item) => { _datas.TryAdd(item, true); }); new Thread(backupRunning).Start(); }