Exemplo n.º 1
0
        /// <summary>
        /// Generates a new TimeGuid value and resets the incrementer back to zero.  This is the only way that this class can get a new TimeGuid value if its _constantTimeGUID flag is
        /// set to True.  If the generated TimeGuid is the same as the previous it will sleep for up to 1 second and then try again.  Therefore, this function should not be called
        /// rapidly in sequence or else performance of calling app will suffer.
        /// </summary>
        /// <param name="prefix"></param>
        /// <returns></returns>
        public string RefreshKey(string uniqueIdentifier = "Key")
        {
            bool keepTrying = true;

            while (keepTrying)
            {
                TimeGuid newGuid = new TimeGuid(DateTime.Now);

                if (newGuid == _timeGuid)
                {
                    int millisec  = DateTimeOffset.Now.Millisecond;
                    int sleepTime = 1000 - millisec;

                    Thread.Sleep(sleepTime);
                }
                else
                {
                    _timeGuid  = newGuid;
                    keepTrying = false;
                }
            }

            _keyIncrementer = 0;
            return(FormatKey(uniqueIdentifier));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Generates the random part of the key.  Depending on the setting for _constantTimeGUID it may or may not generate a new TimeGuid.
        /// If the new TimeGuid is the same as the old (because they were both generated within the same physical second, then it will increment
        /// the numeric incrementer by 1.  If _constantTimeGUID is true then it will always increment the numeric part by 1.
        /// </summary>
        /// <returns></returns>
        private string IncrementKey(string identifier)
        {
            string val;

            lock ( keyIncrLock ) {
                // If the TimeGuid needs to change, then see what new TimeGuid is.  If same as existing then we increment the _keyIncrementer
                // otherwise we reset KeyIncrementer to zero.
                if (!_constantTimeGuid)
                {
                    TimeGuid newGuid = new TimeGuid(DateTime.Now);
                    if (newGuid == _timeGuid)
                    {
                        _keyIncrementer++;
                        val = FormatKey(identifier);
                    }
                    else
                    {
                        _keyIncrementer = 0;
                        _timeGuid       = newGuid;
                        val             = FormatKey(identifier);
                    }
                }
                else
                {
                    _keyIncrementer++;
                    val = FormatKey(identifier);
                }
            } // End Lock

            return(val);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates an object that will generate UniqueKeys.  Keys are guaranteed to be unique across this single instance of the object.  If you trully need
        /// unique keys then use a full Guid.
        /// </summary>
        /// <param name="keySeparator">The separator to be used to separate the custom identifier and the unique part of the key.  Set to empty string for no separator.</param>
        /// <param name="timeGuidSeparator">The separator to be used to separate the numerically increasing number at the end of the unique key.</param>
        /// <param name="keyIdentifierFirst">Set to True to have the key start with the key identifier.  If false the key will end with this identifier.</param>
        /// <param name="constantTimeGuid">Set to True to have the same TimeGuid portion of the key.  The only unique part will be the number increasing at the end.</param>
        public UniqueKeys(string keySeparator = ":", string timeGuidSeparator = "_", bool keyIdentifierFirst = true, bool constantTimeGuid = false)
        {
            _keyIdentifierFirst = keyIdentifierFirst;
            _constantTimeGuid   = constantTimeGuid;
            _keySeparator       = keySeparator;
            _tgSeparator        = timeGuidSeparator;


            // If user wants a constant time guid object then go compute it now.
            if (_constantTimeGuid)
            {
                _timeGuid = new TimeGuid(DateTime.Now);
            }
        }