{"id":13676367,"url":"https://github.com/osgochina/donkeyid","last_synced_at":"2026-01-17T14:35:27.130Z","repository":{"id":45864644,"uuid":"55904198","full_name":"osgochina/donkeyid","owner":"osgochina","description":"php扩展，64位自增id生成器","archived":false,"fork":false,"pushed_at":"2020-11-13T09:45:03.000Z","size":175,"stargazers_count":378,"open_issues_count":5,"forks_count":83,"subscribers_count":32,"default_branch":"master","last_synced_at":"2024-11-11T18:41:09.265Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/osgochina.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-04-10T14:39:06.000Z","updated_at":"2023-12-19T03:02:02.000Z","dependencies_parsed_at":"2022-09-14T05:00:33.955Z","dependency_job_id":null,"html_url":"https://github.com/osgochina/donkeyid","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osgochina%2Fdonkeyid","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osgochina%2Fdonkeyid/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osgochina%2Fdonkeyid/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/osgochina%2Fdonkeyid/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/osgochina","download_url":"https://codeload.github.com/osgochina/donkeyid/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251450656,"owners_count":21591407,"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":[],"created_at":"2024-08-02T13:00:24.028Z","updated_at":"2026-01-17T14:35:27.120Z","avatar_url":"https://github.com/osgochina.png","language":"C","funding_links":[],"categories":["其他","php扩展"],"sub_categories":[],"readme":"# DonkeyID---php扩展-64位自增ID生成器\n[0.7版本请访问](https://github.com/osgochina/donkeyid/tree/donkeyid-0.7)\n## 原理\n\t参考Twitter-Snowflake 算法,扩展了其中的细节。具体组成如下图：\n\t\n![bits.jpg](https://github.com/osgochina/donkeyid/blob/master/doc/bits.png?raw=true)\n\n\u003e 如图所示，64bits 咱们分成了4个部分。\n\n\u003e 1. 毫秒级的时间戳,有42个bit.能够使用139年，从1970年开始计算，能使用到2109年，当然这些是可以扩展的,可以通知指定起始时间来延长这个日期长度。\n\u003e 2. 自定义节点id,防止多进程运行产生重复id,占位12个bit,能够支持4096个节点。部署的时候可以配置好服务器id;\n\u003e 4. ~~进程workerid，占位5bit，能够生成32个进程id。根据pid运算获得。(已经取消)~~\n\u003e 4. 进程内毫秒时间自增序号。占位10bit,一毫秒能产生1024个id。也就是说并发1秒能产生1024000个id。\n\n### 唯一性保证\n\u003e 100%唯一性保证,根据nodeid的不一样保证多服务器的唯一性，使用共享内存+自旋锁保证单节点多进程的唯一性\n\u003e 同一毫秒内自增变量保证并发的唯一性。\n\n\n\n## 使用\n### 安装\n\n\u003e 下载代码到本地，进入项目文件夹，执行\n\n```Bash\ncd ./donkeyid\n/path/to/phpize\n./configure --with-php-config=/path/to/php-config\nmake\nmake install\n```\n\n```Bssh\necho \"extension=donkeyid.so\" \u003e\u003e /path/to/php.ini\n```\n### 模式介绍\n\n\u003e DonkeyId 有三种id生成模式：\n\n\u003e 1. 默认模式,以上的介绍都是基于默认模式,(mysql字段请使用bigint(20)). dk_get_next_id()。\n\u003e 2. 第二种模式是10进制模式 生成最多20位数字,(mysql字段请使用varchar(20)).从右开始算第十位以后的数字是时间戳的秒，\n\u003e    第7位到第9位 是节点id。三位数字，最多到999.从第2位到第6位是秒内的自增id,\n\u003e     最后一位是留给业务方的自定义位数。2016053010150316300120001.dk_get_ts_id().\n\u003e 3. 第三种模式是字符串模式，生成一个25位的字符串,前17位是年月日时分秒毫秒,第18位到21位是节点id，第22-25位是毫秒内自增id,(mysql字段请使用varchar(25))。dk_get_dt_id().\n\n### 运行\n\n* 注意，该扩展不支持php的线程安全模式,请在非线程安全模式下使用。\n\n#### 配置\n\n\u003e 在php.ini 中配置节点id\n\n```\n[DonkeyId]\n;0-4095\ndonkeyid.node_id=0\n;0-当前时间戳\ndonkeyid.epoch=0\n\n```\n\n#### api接口\n\n* dk_get_next_id()\n\n\u003e 获取基于Snowflake算法的id\n\n* dk_get_next_ids($num,$time=0)\n\n\u003e 获取基于Snowflake算法的id列表.$num:生成id的数量,$time:需要生成指定时间的id.$time 默认为0 生成当前时间指定数量的id\n\n* dk_parse_id($id)\n\n\u003e 解析基于Snowflake算法的id元数据,返回值包括:time id生成时间，node_id 节点id，sequence 自增数\n\n* dk_get_ts_id()\n\n\u003e 获取10进制的时间戳类型的id\n\n* dk_get_ts_ids($num,$time=0)\n\n\u003e 获取10进制的时间戳类型的id列表.$num:生成id的数量,$time:需要生成指定时间的id.$time 默认为0 生成当前时间指定数量的id\n\n* dk_parse_ts_id($tsid)\n\n\u003e 解析10进制的时间戳类型的id元数据,返回值包括:time id生成时间，node_id 节点id，sequence 自增数\n\n* dk_get_dt_id()\n\n\u003e 获取字符串类型的id，显式包含日期时间属性\n\n#### 测试代码\n\n```php\n\n    $nextid =  dk_get_next_id();\n    echo \"nextid:\".$nextid.\"\\n\";\n    print_r(dk_parse_id($nextid));\n    \n    $tsid =  dk_get_ts_id();\n    echo \"tsid:\".$tsid.\"\\n\";\n    print_r(dk_parse_ts_id($tsid));\n    \n    $dtid = dk_get_dt_id();\n    echo \"dtid:\".$dtid.\"\\n\";\n    \n    echo \"nextids:\\n\";\n    print_r(dk_get_next_ids(100,1470298401));\n    echo \"nextids2:\\n\";\n    print_r(dk_get_next_ids(100));\n    \n    echo \"tsids:\\n\";\n    print_r(dk_get_ts_ids(100,1470298401));\n    echo \"tsids2:\\n\";\n    print_r(dk_get_ts_ids(100));\n   \n```\n#### 支持版本\n\u003e 支持 php5.3+ ,支持php 7 - 7.4\n\u003e 支持php 8\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fosgochina%2Fdonkeyid","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fosgochina%2Fdonkeyid","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fosgochina%2Fdonkeyid/lists"}