CNode

nodejs中想生成uid gid,使用什么模块?

问答
Ssnowdream发布于12 年前最后回复11 年前14 回复25488 浏览0 收藏

如题?

查看回复

回复 (14)

S
skipify#1·12 年前
S
snowdream#2·12 年前
引用 skipifyhttps://github.com/broofa/node uuid ?

@skipify 看起来太长了,有其他的选择吗

H
haoflynet#3·12 年前

我想问为什么要自己去生成呢,为什么不用数据库的自增呢,这是出于安全考虑吗

Y
yaochun#4·12 年前
引用 snowdream@skipify 看起来太长了,有其他的选择吗

@snowdream 太长?不是很理解,你其实要的是随机的类md5,但是可以指定长度?

A
alsotang#5·12 年前

场景是什么?

S
snowdream#6·12 年前
引用 alsotang场景是什么?

@alsotang 作为用户id,组id

S
snowdream#7·12 年前

我现在用passport.js方案,想在用户表中插入uid,gid字段

J
jiyinyiyong#8·12 年前

有个叫 shortid 的模块, 特点是短

S
snowdream#9·12 年前

感谢以上答复,准备使用以下模块》 https://github.com/mariodu/mongoose-id-autoinc

X
xuanye#10·12 年前
  function guidGenerator() {
    var S4 = function() {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    };
    return (S4() + S4() + S4() + S4() + S4() + S4() + S4() + S4());
}
V
vczero#11·12 年前

https://github.com/vczero/OurTimes/blob/master/server/util/guid.js /**

  • guid模块提供生成唯一值ID,用于token生成等。如果加上objID或者用户唯一标识更好
  • @module guid */ module.exports = { create: function() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); }).toUpperCase(); } };
V
vczero#12·12 年前
module.exports = {
  create: function() {
	  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
		  var r = Math.random() * 16 | 0,
			  v = c == 'x' ? r : (r & 0x3 | 0x8);
		  return v.toString(16);
	  }).toUpperCase();
  }
  };
S
snowdream#13·12 年前

感谢.选择了递增整数来做uid。

P
pangguoming#14·11 年前

Redis 键值对里想做确保唯一性,用UUID,还是用Redis里面用 incr 叠加生成个ID ?

参与回复
登录后即可参与回复。登录