public IdGenerator(DatabaseType type, string connectionString, string serverId = "101", int cacheCount = 100, int formatNum = 8) { this.IsDisposed = true; if (string.IsNullOrEmpty(connectionString)) { throw new ArgumentNullException(nameof(connectionString)); } if (string.IsNullOrEmpty(serverId)) { throw new ArgumentNullException(nameof(connectionString)); } if (0 >= cacheCount) { throw new ArgumentException($"{nameof(cacheCount)} is error!", nameof(cacheCount)); } if (0 > formatNum) { throw new ArgumentException($"{nameof(formatNum)} is error!", nameof(formatNum)); } this.IsDisposed = false; this.dicLock = new object(); this.dic = new Dictionary <string, IdInfoModel>(StringComparer.OrdinalIgnoreCase); this.DatabaseType = type; this.ConnectionString = connectionString; this.ServerId = serverId; this.CacheCount = cacheCount; this.FormatNum = formatNum; this.sq = new IdInfoModel() { Key = "*", StratValue = 0, EndValue = 0 }; switch (type) { case DatabaseType.MSSQLServer: updateSQL = "UPDATE [SysSequence] SET [Value] = [Value] + @count, [UpdateTime] = @now WHERE [Name] = @name AND [Key] = @key"; selectUpSQL = "SELECT [Value] FROM [SysSequence] WHERE [Name] = @name AND [Key] = @key"; insertSQL = "INSERT INTO [SysSequence]([Name], [Key], [Value], [UpdateTime], [CreateTime]) VALUES(@Name, @Key, @Value, @UpdateTime, @CreateTime)"; break; case DatabaseType.MySQL: updateSQL = "UPDATE `SysSequence` SET `Value` = `Value` + @count, `UpdateTime` = @now WHERE `Name` = @name AND `Key` = @key"; selectUpSQL = "SELECT `Value` FROM `SysSequence` WHERE `Name` = @name AND `Key` = @key"; insertSQL = "INSERT INTO `SysSequence`(`Name`, `Key`, `Value`, `UpdateTime`, `CreateTime`) VALUES(@Name, @Key, @Value, @UpdateTime, @CreateTime)"; break; default: throw new Exception($"不支持 {this.DatabaseType} 数据库!"); } }
public void Dispose() { if (!this.IsDisposed) { this.IsDisposed = true; this.ConnectionString = null; this.ServerId = null; if (this.dic != null) { this.dic.Clear(); } this.dic = null; this.dicLock = null; this.sq = null; } }
private List <string> GetValue(string name, string key, int count, int?cacheCount, int?formatNum) { if (!cacheCount.HasValue || cacheCount <= 0) { cacheCount = this.CacheCount; } List <string> vlist = new List <string>(count); IdInfoModel vm = null; lock (this.dicLock) { dic.TryGetValue(name, out vm); } if (vm == null) { lock (this.dicLock) { dic.TryGetValue(name, out vm); if (vm == null) { vm = new IdInfoModel() { Key = string.Empty, StratValue = 0, EndValue = 0 }; dic.Add(name, vm); } } } lock (vm.LockObj) { if (string.Equals(vm.Key, key, StringComparison.OrdinalIgnoreCase)) { int c = vm.EndValue - vm.StratValue; if (c < count) { while (vm.StratValue < vm.EndValue) { vm.StratValue = vm.StratValue + 1; string v = FormatId(key, vm.StratValue, formatNum); vlist.Add(v); } count = count - c; vm.EndValue = GetDbValue(name, key, cacheCount.Value + count); vm.StratValue = vm.EndValue - cacheCount.Value - count; } } else { vm.EndValue = GetDbValue(name, key, cacheCount.Value + count); vm.StratValue = vm.EndValue - cacheCount.Value - count; vm.Key = key; } while (count > 0) { vm.StratValue = vm.StratValue + 1; string v = FormatId(key, vm.StratValue, formatNum); vlist.Add(v); count--; } } return(vlist); }