Home  >  Q&A  >  body text

数据库 - 求比较方便的基于自增id映射出一串唯一数字id的算法

rails默认用自增id作为pk,并且url上通过id找到相应的资源,不过我想像微博那样通过一串长数字去定位资源
如:

http://weibo.com/u/1857759250

这种形式的数字id。

该数字串最好能够有跟自增id的一定的对应关系,比如说是自增id经过一定的算法生成。
该数字串必须是唯一的,最好是不连续的,最好在10位以内。

谢谢

迷茫迷茫2712 days ago764

reply all(2)I'll reply

  • 阿神

    阿神2017-04-22 09:01:53

    A relatively simple method is to find a large number and xor it. The algorithm is simple and the number of digits is easy to control

    You can make a slight deformation to remove the regularity. For example, to find 16 large numbers, take the last 4 digits of the original number to find the corresponding large number, then the last 4 digits remain unchanged, and the other digits are xored with the large number. The last 4 digits are also included The pattern can be 1st, 3rd, 6th, 15th and so on

    reply
    0
  • 怪我咯

    怪我咯2017-04-22 09:01:53

    id + fixed value, md5, isn’t it possible?

    reply
    0
  • Cancelreply