1. Go to this page and download the library: Download highperapp/uuid library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
highperapp / uuid example snippets
// Test FFI with absolute path
$ffi = FFI::cdef("", __DIR__ . "/rust/target/release/libuuid_ffi.so");
use HighPerApp\HighPer\Uuid\Core\Configuration;
use HighPerApp\HighPer\Uuid\Core\UuidFactory;
$config = Configuration::highPerformance();
UuidFactory::configure($config);
// Now all UUID generation uses Rust FFI when available
$uuid = UuidFactory::v4(); // Uses Rust acceleration
use HighPerApp\HighPer\Uuid\Core\ThreadSafeUuidFactory;
// Safe for concurrent environments
$factory = ThreadSafeUuidFactory::getInstance();
$uuid = $factory->v4();
// Or use static convenience methods
$uuid = ThreadSafeUuidFactory::createV4();
$uuid = ThreadSafeUuidFactory::createV7();
use HighPerApp\HighPer\Uuid\Async\AsyncUuidFactory;
// Check if async is available
if (AsyncUuidFactory::isAsyncAvailable()) {
// Single async UUID
$uuid = AsyncUuidFactory::v4Async()->await();
$uuid = AsyncUuidFactory::v7Async()->await();
// Batch generation (non-blocking)
$uuids = AsyncUuidFactory::generateBatch(1000, 4)->await();
// Concurrent batch with worker processes
$uuids = AsyncUuidFactory::generateConcurrentBatch(10000, 4, 8)->await();
} else {
// Fallback to synchronous
$uuids = AsyncUuidFactory::generateBatchLegacy(1000, 4);
}
use HighPerApp\HighPer\Uuid\Async\TrueConcurrentGenerator;
// True parallel processing (generateConcurrent(100000, 4);
// Generates 100,000 UUIDs across 8 actual parallel processes
use HighPerApp\HighPer\Uuid\Async\AsyncUuidFactory;
// Stream-based generation with non-blocking yields
foreach (AsyncUuidFactory::generatePipeline(10000, 7, 1000) as $uuid) {
// Process each UUID as it's generated
// Automatically yields control between batches
echo $uuid->toString() . "\n";
}
use HighPerApp\HighPer\Uuid\Configuration\AsyncConfiguration;
// Configure for high-throughput
$config = AsyncConfiguration::highThroughput();
AsyncUuidFactory::configure($config);
// Or custom configuration
$config = new AsyncConfiguration([
'defaultWorkerCount' => 8,
'batchSize' => 5000,
'enableEntropyPool' => true,
'enableParallel' => true,
]);
// Get optimal settings for your system
$optimal = $config->getOptimalWorkerCount(); // Auto-detects CPU cores
$batchSize = $config->getOptimalBatchSize(100000);
use HighPerApp\HighPer\Uuid\RandomProvider\RandomProviderFactory;
// Available providers
$providers = RandomProviderFactory::getAvailableProviders();
// ['secure', 'openssl', 'rust', 'pseudo']
// Create specific provider
$provider = RandomProviderFactory::create('rust');
$uuid = UuidFactory::v4($provider);
// Benchmark providers
$results = RandomProviderFactory::benchmark('rust', 1000);
// In a Laravel service provider
use HighPerApp\HighPer\Uuid\Core\ThreadSafeUuidFactory;
use HighPerApp\HighPer\Uuid\Async\AsyncUuidFactory;
use HighPerApp\HighPer\Uuid\Configuration\AsyncConfiguration;
public function register()
{
// Register thread-safe factory
$this->app->singleton(ThreadSafeUuidFactory::class, function() {
return ThreadSafeUuidFactory::getInstance();
});
}
public function boot()
{
// Configure async capabilities if available
if (AsyncUuidFactory::isAsyncAvailable()) {
AsyncUuidFactory::configure(AsyncConfiguration::highThroughput());
}
}
// In your models
protected $keyType = 'string';
public $incrementing = false;
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
if (empty($model->id)) {
$factory = app(ThreadSafeUuidFactory::class);
$model->id = $factory->v7()->toString();
}
});
}
// In queue jobs for bulk operations
class BulkUuidGenerationJob
{
public function handle()
{
if (AsyncUuidFactory::isAsyncAvailable()) {
$uuids = AsyncUuidFactory::generateConcurrentBatch(50000, 7, 4)->await();
} else {
$uuids = AsyncUuidFactory::generateBatchLegacy(50000, 7);
}
// Process the UUIDs
}
}
// In a service or controller
use HighPerApp\HighPer\Uuid\Core\ThreadSafeUuidFactory;
use HighPerApp\HighPer\Uuid\Async\AsyncUuidFactory;
class UuidService
{
public function __construct(
private ThreadSafeUuidFactory $uuidFactory
) {}
public function generateSingle(): string
{
return $this->uuidFactory->v7()->toString();
}
public async function generateBatch(int $count): array
{
if (AsyncUuidFactory::isAsyncAvailable()) {
return AsyncUuidFactory::generateBatch($count, 7)->await();
}
return AsyncUuidFactory::generateBatchLegacy($count, 7);
}
}
use Swoole\Http\Server;
use HighPerApp\HighPer\Uuid\Core\ThreadSafeUuidFactory;
use HighPerApp\HighPer\Uuid\Async\AsyncUuidFactory;
$server = new Server("127.0.0.1", 9501);
$server->on('WorkerStart', function($server, $workerId) {
// Reset static state for each worker
ThreadSafeUuidFactory::resetInstance();
AsyncUuidFactory::reset();
});
$server->on('Request', function($request, $response) {
// Thread-safe UUID generation
$factory = ThreadSafeUuidFactory::getInstance();
$uuid = $factory->v7()->toString();
$response->end("Generated UUID: {$uuid}");
});
$server->start();
use React\EventLoop\Loop;
use React\Http\HttpServer;
use HighPerApp\HighPer\Uuid\Core\ThreadSafeUuidFactory;
use HighPerApp\HighPer\Uuid\Async\AsyncUuidFactory;
$loop = Loop::get();
$server = new HttpServer(function($request) {
return new Promise(function($resolve) {
if (AsyncUuidFactory::isAsyncAvailable()) {
// Non-blocking UUID generation
AsyncUuidFactory::generateBatch(1000, 7)->then(function($uuids) use ($resolve) {
$resolve(new Response(200, [], json_encode($uuids)));
});
} else {
// Fallback to thread-safe synchronous
$factory = ThreadSafeUuidFactory::getInstance();
$uuids = [];
for ($i = 0; $i < 1000; $i++) {
$uuids[] = $factory->v7()->toString();
}
$resolve(new Response(200, [], json_encode($uuids)));
}
});
});
$socket = new SocketServer('127.0.0.1:8080', [], $loop);
$server->listen($socket);
$loop->run();