[震惊]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,
]);
}