打赏

redisTools-IdGenerator

public class IdGenerator : RedisToolBase
    {
        //redis客户端对象
        private static readonly NedisClient client = new NedisClient(GetRedisConfig(redisConfigKey));

        //redis客户端对象配置存放在Consul服务端的key
        private static readonly string redisConfigKey = "redis/common/idgeneratorconfig";

        private static readonly object objMaxIdsLock = new object();
        // 存放BusinessIdKey,MaxId
        private static readonly Dictionary<string, long> maxIds = new Dictionary<string, long>();

        private static readonly object objNextIdsLock = new object();
        // 存放BusinessIdKey,NextId
        private static readonly Dictionary<string, long> nextIds = new Dictionary<string, long>();

        private static readonly object objIncrementsLock = new object();
        // 存放BusinessIdKey,id增量
        private static readonly Dictionary<string, long> increments = new Dictionary<string, long>();

        /// <summary>
        /// 计算主键时的增量
        /// </summary>
        private static readonly uint persistenceIncrement = 10;

        /// <summary>
        /// 业务IdKey
        /// </summary>
        private string busnessIdKey = string.Empty;

        /// <summary>
        /// 使用业务ID的key,ID增量初始化
        /// </summary>
        /// <param name="BusnessIdKey">业务IdKey</param>
        /// <param name="Increment">id增量,默认为1,不能大于10</param>
        public IdGenerator(string BusinessIdKey, uint Increment=1)
        {
            Init(BusinessIdKey, Increment);
        }

        /// <summary>
        /// 初始化increments,maxIds,nextIds字典
        /// </summary>
        /// <param name="BusinessIdKey"></param>
        /// <param name="Increment"></param>
        private void Init(string BusinessIdKey,long Increment)
        {
            if (!increments.ContainsKey(BusinessIdKey))
            {
                lock (objIncrementsLock)
                {
                    if (!increments.ContainsKey(BusinessIdKey))
                    {
                        increments.Add(BusinessIdKey, Increment);
                    }
                }
            }
            if (!maxIds.ContainsKey(BusinessIdKey))
            {
                lock (objMaxIdsLock)
                {
                    if (!maxIds.ContainsKey(BusinessIdKey))
                    {
                        maxIds.Add(BusinessIdKey, client.Increment(BusinessIdKey, persistenceIncrement));
                    }
                }
            }
            if (!nextIds.ContainsKey(BusinessIdKey))
            {
                lock (objNextIdsLock)
                {
                    if (!nextIds.ContainsKey(BusinessIdKey))
                    {
                        nextIds.Add(BusinessIdKey, maxIds[BusinessIdKey] - persistenceIncrement);
                    }
                }
            }
        }

        /// <summary>
        /// 重新设置MaxID
        /// </summary>
        /// <returns></returns>
        private static void ResetMaxID(string BusinessIdKey)
        {
            maxIds[BusinessIdKey] = client.Increment(BusinessIdKey, persistenceIncrement);
            nextIds[BusinessIdKey] = maxIds[BusinessIdKey] - persistenceIncrement;
        }
        
        // 获取下一个ID的锁
        private static readonly object nextIDLocker = new object();

        /// <summary>
        /// 根据业务Id键获取下一个主键ID
        /// </summary>
        /// <returns></returns>
        public Int64 GetNextID(string BusinessIdKey)
        {
            lock (nextIDLocker)
            {
                nextIds[BusinessIdKey] = nextIds[BusinessIdKey] + 1;

                // 如果自增后的ID大于已经持久的ID,则先持久ID,再返回
                if (nextIds[BusinessIdKey] >= maxIds[BusinessIdKey])
                {
                    ResetMaxID(BusinessIdKey);
                }

                return nextIds[BusinessIdKey];
            }
        }
    }

  

1.IdGenerator使用说明:
var primaryKey = new IdGenerator("blog_id", 1);
var id = primaryKey.GetNextID("blog_id");

posted @ 2018-08-20 09:20  刘奇云  阅读(822)  评论(0编辑  收藏  举报