zoukankan      html  css  js  c++  java
  • 订单号生成办法

    1、Redis Incr

        public class RedisConnection
        {
            private static readonly ConfigurationOptions ConfigurationOptions = ConfigurationOptions.Parse("127.0.0.1" + ":" + "6379");
            private static ConnectionMultiplexer _redisConn;
            private static readonly object Locker = new object();
            private RedisConnection() { }  
    
            /// <summary>
            /// 单例获取Redis链接
            /// </summary>
            public static ConnectionMultiplexer GetRedisConn
            {
                get
                {
                    if (_redisConn == null)
                    {
                        lock (Locker)
                        {
                            if (_redisConn == null || !_redisConn.IsConnected)
                            {
                                _redisConn = ConnectionMultiplexer.Connect(ConfigurationOptions);
                            }
                        }
                    }
                    return _redisConn;
                }
            }
        }

      

            /// <summary>
            /// 生成规则:时间+redis自增
            /// 缺点:可以根据redis自增知道订单量
            /// </summary>
            public static string CreateByRedis()
            {
                //Redis服务器链接失败会报异常
                try
                {
                    //单例获取Redis链接
                    var redis = RedisConnection.GetRedisConn;
                    var db = redis.GetDatabase();
                    //根据当前日期获取Redis订单号自增的key
                    var incrkey = "Incr_" + DateTime.Now.ToString("yyyyMMdd");
                    if (!db.KeyExists(incrkey))
                    {
                        db.StringSet(incrkey, 1, new TimeSpan(24, 0, 0));
                    }
    
                    var orderDateTime = DateTime.Now.ToString("yyMMdd"); //因为我们的Redis Incr是按照天数来的 所以原则上用天的区分就好了
                    var orderIncr = db.StringIncrement(incrkey).ToString("0000000");
                    return orderDateTime + orderIncr;
                }
                catch (Exception)
                {
                    //TODO 这里可以写个日志
                    return "";
                } 
            }

      

  • 相关阅读:
    linux修改键盘按键
    linux添加一个已经存在用户到一个用户组
    centos-6更新yum源(163)
    Fedora 19安装以后的优化
    centos永久性修改系统时间显示格式
    smb.conf文件详解
    Centos上部署文件共享
    centos上mysql开启远程访问
    centos安装mysql后默认密码修改
    centos上mysql的一种安装方式
  • 原文地址:https://www.cnblogs.com/ideacore/p/6877678.html
Copyright © 2011-2022 走看看