Esempio n. 1
0
        public static SnowflakeId Default()
        {
            lock (SLock)
            {
                if (_snowflakeId != null)
                {
                    return(_snowflakeId);
                }

                var random = new Random();

                if (!int.TryParse(Environment.GetEnvironmentVariable("SNOWFLAKE_WORKERID", EnvironmentVariableTarget.Machine), out var workerId))
                {
                    workerId = random.Next((int)MaxWorkerId);
                }

                if (!int.TryParse(Environment.GetEnvironmentVariable("SNOWFLAKE_DATACENTERID", EnvironmentVariableTarget.Machine), out var datacenterId))
                {
                    datacenterId = random.Next((int)MaxDatacenterId);
                }

                return(_snowflakeId = new SnowflakeId(workerId, datacenterId));
            }
        }
Esempio n. 2
0
 public static TKey Generate <TKey>()
 {
     if (typeof(TKey) == typeof(Guid))
     {
         return((TKey)TypeDescriptor.GetConverter(typeof(TKey)).ConvertFromInvariantString(Guid.NewGuid().ToString()));
     }
     if (typeof(TKey) == typeof(string))
     {
         return((TKey)TypeDescriptor.GetConverter(typeof(TKey)).ConvertFromInvariantString(SnowflakeId.Create().ToString()));
     }
     if (typeof(TKey) == typeof(long))
     {
         return((TKey)TypeDescriptor.GetConverter(typeof(TKey)).ConvertFromInvariantString(SnowflakeId.Create().ToString()));
     }
     throw new NotSupportedException($"不支持此类型的id");
 }