• 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");

  • 相关阅读:
    Python判断文件和文件夹是否存在的方法
    机器学习之深度学习
    机器学习之深度学习
    以云计算与大数据为代表的信息技术是电力行业未来发展核心
    以云计算与大数据为代表的信息技术是电力行业未来发展核心
    机器学习之决策树(ID3)算法与Python实现
    机器学习之决策树(ID3)算法与Python实现
    R语言基础画图/绘图/作图
    R语言基础画图/绘图/作图
    rabbitmq 入门基础(一)
  • 原文地址:https://www.cnblogs.com/liuqiyun/p/9503753.html
Copyright © 2020-2023  润新知