{"id":15479110,"url":"https://github.com/dekuan/dedid","last_synced_at":"2025-04-22T14:51:06.721Z","repository":{"id":56964989,"uuid":"99261735","full_name":"dekuan/dedid","owner":"dekuan","description":"An unique id generator for primary key of distributed database","archived":false,"fork":false,"pushed_at":"2017-12-25T07:54:44.000Z","size":21,"stargazers_count":22,"open_issues_count":1,"forks_count":8,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-29T16:11:14.444Z","etag":null,"topics":["composer","distributed-database","laravel","php","unique-id-generator"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dekuan.png","metadata":{"files":{"readme":"README.chs.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-08-03T18:09:33.000Z","updated_at":"2021-12-05T08:38:34.000Z","dependencies_parsed_at":"2022-08-21T05:40:27.869Z","dependency_job_id":null,"html_url":"https://github.com/dekuan/dedid","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dekuan%2Fdedid","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dekuan%2Fdedid/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dekuan%2Fdedid/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dekuan%2Fdedid/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dekuan","download_url":"https://codeload.github.com/dekuan/dedid/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250263376,"owners_count":21401848,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["composer","distributed-database","laravel","php","unique-id-generator"],"created_at":"2024-10-02T04:09:11.899Z","updated_at":"2025-04-22T14:51:06.705Z","avatar_url":"https://github.com/dekuan.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# dekuan/dedid\n一种为分布式数据库而设计的全局唯一 ID（主键）生成器。\n本算法的实现参考了 Twitter Snowflake，但是在最后的 12 位您不仅仅可以使用随机数字，也可以通过指定字符串来获取哈希值。\n\n\n* [Documentation in English](README.md)\n\n\n\n# 算法\n\n### 结构体概况\n本算法使用一个 64 位的 int 值作为 ID 的载体。\n\n~~~\n0 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx x xxxxxx xxxxxx xx xxxxxxxx\n~~~\n\n### 结构体说明\n\n位置\t| 长度 | 用途\t| 备注\n----------|----------|----------|----------\n0\t\t\t| 1 | 保留位 | 一直都是 0\n1~41\t\t\t| 41 | 以毫秒为单位的流淌时间 |0~69 年，即本 ID 算法最多可以使用 69 年\n42~46\t\t\t| 5 | 数据中心编号 | 编号范围 0~31，最多可容纳 32 个数据中心\n47~51\t\t\t| 5 | 数据中心下属数据节点编号 | 编号范围 0~31，每个数据中心最多可容纳 32 个数据节点。所以整个系统最多可容纳 1024 个数据节点\n52~63\t\t\t| 12 | 随机数/哈希值 | 取值范围 0~4095\n\n\n\n\n\n# 位排列\n\n### 数据中心编号\n~~~\n0 00000000 00000000 00000000 00000000 00000000 0 11111 00000 0000 00000000\n\n00000000 00000000 00000000 00000000 00000000 00111110 00000000 00000000\n\n00       00       00       00       00       3E       00       00\n~~~\n\n### 数据节点编号\n~~~\n0 00000000 00000000 00000000 00000000 00000000 0 00000 11111 0000 00000000\n\n00000000 00000000 00000000 00000000 00000000 00000001 11110000 00000000\n\n00       00       00       00       00       01       F0       00\n~~~\n\n\n### 流淌时间\n~~~\n0 11111111 11111111 11111111 11111111 11111111 1 00000 00000 0000 00000000\n\n01111111 11111111 11111111 11111111 11111111 11000000 00000000 00000000\n\n7F       FF       FF       FF       FF       C0       00       00\n~~~\n\n\n### 随机数/哈希值\n~~~\n0 00000000 00000000 00000000 00000000 00000000 0 00000 00000 1111 11111111\n\n00000000 00000000 00000000 00000000 00000000 00000000 00001111 11111111\n\n00       00       00       00       00       00       0F       FF\n~~~\n\n\n# 使用方法\n\n### 创建一个普通的随机数 ID\n\n~~~\n$cDId\t\t= CDId::getInstance();\n$nCenter\t= 0;\n$nNode\t\t= 1;\n\n$arrD\t\t= [];\n$nNewId\t= $cDId-\u003ecreateId( $nCenter, $nNode, null, $arrD );\n\necho \"new id = \" . $nNewId . \"\\r\\n\";\nprint_r( $arrD );\n\n~~~\n\n##### 输出\n\n~~~\nnew id = 114654484990270790\nArray\n(\n    [center] =\u003e 0\n    [node] =\u003e 1\n    [time] =\u003e 27335759399\n    [rand] =\u003e 3398\n)\n~~~\n\n\n### 指定字符串，创建一个带有哈希值的 ID\n\n~~~\n$cDId\t\t= CDId::getInstance();\n$nCenter\t= 0;\n$nNode\t\t= 15;\n\n$sSrc\t\t= \"dekuan\";\n$arrD\t\t= [];\n$nNewId\t= $cDId-\u003ecreateId( $nCenter, $nNode, $sSrc, $arrD );\n\necho \"new id = \" . $nNewId . \"\\r\\n\";\nprint_r( $arrD );\n\n~~~\n\n##### 输出\n\n~~~\nnew id = 114654631304370386\nArray\n(\n    [center] =\u003e 0\n    [node] =\u003e 1\n    [time] =\u003e 27335794283\n    [rand] =\u003e 2258\n)\n~~~\n\n\n\n\n### 解析 ID 获取详细信息\n\n~~~\n$cDId\t\t= CDId::getInstance();\n$arrId\t\t= $cDId-\u003eparseId( 114654631304370386 );\nprint_r( $arrId );\n\n~~~\n\n##### 输出\n\n~~~\nArray\n(\n    [center] =\u003e 0\n    [node] =\u003e 1\n    [time] =\u003e 27335794283\n    [rand] =\u003e 2258\n)\n~~~\n\n\n# 如何安装\n~~~\n# composer require dekuan/dedid\n~~~\n了解更多信息，敬请访问 [https://packagist.org/packages/dekuan/dedid](https://packagist.org/packages/dekuan/dedid)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdekuan%2Fdedid","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdekuan%2Fdedid","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdekuan%2Fdedid/lists"}