[震惊]PHP用整数生成绝对不重复的唯一ID类库——Hashids》》》》》》》出现了重复!!!!咋回事?

需求生成一个6位数的自增不重复的短链接,于是
在网上看到《PHP用整数生成绝对不重复的唯一ID类库——Hashids》,试了一下,无论怎么改salt,每次执行到200条左右就出现重复值,这是咋回事

composer require vinkla/hashids
  • 迁移文件
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateHashidsTestsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('hashids_tests', function (Blueprint $table) {
            $table->id();
            $table->string('hashids')->unique('hashids')->default(0);
            $table->integer('ids_text')->default(0);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('hashids_tests');
    }
}
  • 执行
    set_time_limit(0);
   for ($i = 1;$i<1000000;$i++){
       $hashids = Hashids::encode($i);
        HashidsTest::query()->create([
            'hashids' => $hashids,
            'ids_text' => $i,
        ]);
   }

PHP用整数生成绝对不重复的唯一ID类库——Hashids》》》》》》》出现了重复!!!!咋回事?

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
最佳答案

大小写问题,你可以全部大写或者全部小写

1年前 评论
Ranger9527 1年前
my38778570 (楼主) 1年前
讨论数量: 5

大小写问题,你可以全部大写或者全部小写

1年前 评论
Ranger9527 1年前
my38778570 (楼主) 1年前

用 _bin 类的字符 collation,大小写敏感

1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!