귀하는 로그인되어 있지 않습니다. 이대로 편집하면 귀하의 IP 주소가 편집 기록에 남게 됩니다.스팸 방지 검사입니다. 이것을 입력하지 마세요!=== 프로그래밍 언어 === ==== Python ==== Python 3.14에서 UUID 버전 6, 7, 8이 표준 라이브러리에 공식 추가되었다. <syntaxhighlight lang="python"> import uuid # 버전 4 (가장 일반적) uuid4 = uuid.uuid4() # 버전 1 uuid1 = uuid.uuid1() # 버전 7 (Python 3.14+) uuid7 = uuid.uuid7() # 버전 8 (Python 3.14+) uuid8 = uuid.uuid8() # Python 3.13 이하에서는 외부 라이브러리 사용 # pip install uuid-utils 또는 uuid-extension </syntaxhighlight> ==== JavaScript/Node.js ==== <syntaxhighlight lang="javascript"> // crypto 모듈 사용 (버전 4만 지원) const { randomUUID } = require('crypto'); const uuid = randomUUID(); // uuid 패키지 사용 (모든 버전 지원) const { v4: uuidv4, v7: uuidv7 } = require('uuid'); const id4 = uuidv4(); const id7 = uuidv7(); </syntaxhighlight> ==== Java ==== Java OpenJDK에서 RFC 9562에 정의된 UUID 버전 7(UUIDv7) 지원 추가를 위한 작업이 진행 중이다(JDK-8334015). 현재는 외부 라이브러리를 통해 사용 가능하다. <syntaxhighlight lang="java"> // 표준 라이브러리 (버전 4만 지원) UUID uuid = UUID.randomUUID(); // 버전 7은 외부 라이브러리 필요 // uuid-creator 라이브러리 사용 예시 import com.github.f4b6a3.uuid.UuidCreator; UUID uuid7 = UuidCreator.getTimeOrderedEpoch(); </syntaxhighlight> ==== .NET/C# ==== .NET 9 (2024년 11월 정식 출시)에서 <code>Guid.CreateVersion7()</code> 메서드가 추가되어 네이티브로 UUID v7을 지원한다. <syntaxhighlight lang="csharp"> // .NET 9.0 이상 var guid = Guid.CreateVersion7(); // 특정 타임스탬프로 생성 var guidWithTimestamp = Guid.CreateVersion7(DateTimeOffset.UtcNow); // TimeProvider를 활용한 테스트 가능한 코드 var uuid = Guid.CreateVersion7(timeProvider.GetUtcNow()); // GUID 버전 확인 var version = guid.Version; // 7 // .NET 8 이하에서는 외부 라이브러리 필요 </syntaxhighlight> ==== Go ==== <syntaxhighlight lang="go"> // google/uuid 패키지 import "github.com/google/uuid" // 버전 4 uuid := uuid.New() // 버전 7 (v1.6.0+) uuid7, err := uuid.NewV7() </syntaxhighlight> ==== PHP ==== PHP에서는 네이티브 UUID 지원이 제한적이므로 라이브러리를 사용하거나 직접 구현하는 방식을 사용한다. '''라이브러리 사용 (ramsey/uuid)''' ramsey/uuid는 가장 널리 사용되는 PHP UUID 라이브러리로, RFC 9562의 모든 버전(1-8)을 지원한다. <syntaxhighlight lang="php"> <?php // Composer로 설치 // composer require ramsey/uuid use Ramsey\Uuid\Uuid; // 버전 4 (무작위) $uuid4 = Uuid::uuid4(); echo $uuid4->toString(); // e.g. 25769c6c-d34d-4bfe-ba98-e0ee856f3e7a // 버전 7 (시간순 정렬) $uuid7 = Uuid::uuid7(); echo $uuid7->toString(); // e.g. 01921e85-f198-7490-9b89-7dd0d468543b // 날짜/시간 정보 추출 printf("UUID: %s\n", $uuid7->toString()); printf("Version: %d\n", $uuid7->getFields()->getVersion()); printf("Date: %s\n", $uuid7->getDateTime()->format('Y-m-d H:i:s')); // 특정 날짜로 버전 7 생성 $dateTime = new DateTimeImmutable('2025-01-01 00:00:00'); $uuid7WithDate = Uuid::uuid7($dateTime); // 버전 1, 3, 5, 6, 8도 지원 $uuid1 = Uuid::uuid1(); $uuid5 = Uuid::uuid5(Uuid::NAMESPACE_DNS, 'example.com'); $uuid6 = Uuid::uuid6(); </syntaxhighlight> '''직접 구현 (버전 4)''' 외부 라이브러리 없이 간단한 UUID v4를 직접 구현할 수 있다. <syntaxhighlight lang="php"> <?php function uuid4(): string { // PHP 7.0+ 필요 $data = random_bytes(16); // 버전 비트 설정 (0100) $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // 변형 비트 설정 (10xx) $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // UUID 문자열 포맷 return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)); } echo uuid4(); // e.g. 550e8400-e29b-41d4-a716-446655440000 </syntaxhighlight> '''직접 구현 (버전 7)''' UUID v7도 비교적 간단하게 직접 구현 가능하다. <syntaxhighlight lang="php"> <?php function uuid7(): string { static $last_timestamp = 0; // 현재 타임스탬프 (밀리초) $unixts_ms = intval(microtime(true) * 1000); // 모노토닉 증가 보장 if ($last_timestamp >= $unixts_ms) { $unixts_ms = $last_timestamp + 1; } $last_timestamp = $unixts_ms; // 10바이트 랜덤 데이터 생성 $data = random_bytes(10); // 버전 비트 설정 (0111) $data[0] = chr((ord($data[0]) & 0x0f) | 0x70); // 변형 비트 설정 (10xx) $data[2] = chr((ord($data[2]) & 0x3f) | 0x80); // 타임스탬프 + 랜덤 데이터 결합 return vsprintf( '%s%s-%s-%s-%s-%s%s%s', str_split( str_pad(dechex($unixts_ms), 12, '0', STR_PAD_LEFT) . bin2hex($data), 4 ) ); } echo uuid7(); // e.g. 01921e85-f198-7490-9b89-7dd0d468543b </syntaxhighlight> '''Laravel에서 사용''' Laravel 11+에서는 Eloquent 모델에서 UUID를 쉽게 사용할 수 있다. <syntaxhighlight lang="php"> <?php use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Concerns\HasUuids; use Illuminate\Database\Eloquent\Concerns\HasVersion7Uuids; // 버전 4 UUID (기본) class Product extends Model { use HasUuids; // 자동으로 'id' 필드에 UUID v4 생성 } // 버전 7 UUID (권장) class Order extends Model { use HasVersion7Uuids; // 자동으로 'id' 필드에 UUID v7 생성 // 시간순 정렬 가능하여 데이터베이스 성능 향상 } // 직접 생성 use Illuminate\Support\Str; $uuid4 = Str::uuid(); // 버전 4 $uuid7 = Str::orderedUuid(); // 시간순 UUID (버전 4 기반) </syntaxhighlight> '''Doctrine ORM 통합''' <syntaxhighlight lang="php"> <?php use Doctrine\ORM\Mapping as ORM; use Ramsey\Uuid\Doctrine\UuidV7Generator; use Ramsey\Uuid\UuidInterface; #[ORM\Entity] #[ORM\Table(name: 'products')] class Product { #[ORM\Id] #[ORM\Column(type: 'uuid_binary', unique: true)] #[ORM\GeneratedValue(strategy: 'CUSTOM')] #[ORM\CustomIdGenerator(class: UuidV7Generator::class)] private UuidInterface $id; public function getId(): UuidInterface { return $this->id; } } </syntaxhighlight> '''경량 대안 라이브러리''' 대규모 의존성이 부담스러운 경우 경량 라이브러리 사용을 고려할 수 있다. <syntaxhighlight lang="php"> <?php // oittaa/uuid-php (경량 라이브러리) // composer require oittaa/uuid-php use UUID\UUID; $uuid4 = UUID::uuid4(); $uuid7 = UUID::uuid7(); $uuid8 = UUID::uuid8(); // 검증 $isValid = UUID::isValid('01921e85-f198-7490-9b89-7dd0d468543b'); </syntaxhighlight> 편집 요약 가온 위키에서의 모든 기여는 크리에이티브 커먼즈 저작자표시-동일조건변경허락 라이선스로 배포된다는 점을 유의해 주세요(자세한 내용에 대해서는 가온 위키:저작권 문서를 읽어주세요). 만약 여기에 동의하지 않는다면 문서를 저장하지 말아 주세요. 또한, 직접 작성했거나 퍼블릭 도메인과 같은 자유 문서에서 가져왔다는 것을 보증해야 합니다. 저작권이 있는 내용을 허가 없이 저장하지 마세요! 취소 편집 도움말 (새 창에서 열림)