zoukankan      html  css  js  c++  java
  • 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");

  • 相关阅读:
    .Net控件创建热键
    .Net字符串处理
    C++ 中通过函数名字的字符串调用函数
    将.Net应用程序依赖的库文件部署到其他目录下
    VS2012生成事件
    visual studio导出项目模板
    .Net Program Unable to copy a file from objDebug to binDebug
    VBA引用.Net类库
    .Net 自定义事件
    VB.Net 解决winForm界面卡死
  • 原文地址:https://www.cnblogs.com/liuqiyun/p/9503753.html
Copyright © 2011-2022 走看看