首页 > 代码库 > 订单号生成办法
订单号生成办法
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链接 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("yyMMddHHmmssfff"); var orderIncr = db.StringIncrement(incrkey).ToString("0000000"); return orderDateTime + orderIncr; }
订单号生成办法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。