{"id":201566,"date":"2025-05-10T09:48:10","date_gmt":"2025-05-10T01:48:10","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201566\/"},"modified":"2025-05-10T09:48:10","modified_gmt":"2025-05-10T01:48:10","slug":"%e4%bd%bf%e7%94%a8%e6%95%b0%e6%8d%ae%e5%ba%93%e5%92%8credis%e5%bc%80%e5%8f%91%e5%90%84%e7%b3%bb%e7%bb%9f%e7%8b%ac%e7%ab%8b%e7%9a%84%e8%87%aa%e5%a2%9eid%e7%94%9f%e6%88%90%e5%99%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201566\/","title":{"rendered":"\u4f7f\u7528\u6570\u636e\u5e93\u548cRedis\u5f00\u53d1\u5404\u7cfb\u7edf\u72ec\u7acb\u7684\u81ea\u589eID\u751f\u6210\u5668"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u4f7f\u7528\u6570\u636e\u5e93\u548cRedis\u5f00\u53d1\u5404\u7cfb\u7edf\u72ec\u7acb\u7684\u81ea\u589eID\u751f\u6210\u5668<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762golang\u5b66\u4e60\u7f51\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2a\u6570\u636e\u5e93\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300a\u4f7f\u7528\u6570\u636e\u5e93\u548cRedis\u5f00\u53d1\u5404\u7cfb\u7edf\u72ec\u7acb\u7684\u81ea\u589eID\u751f\u6210\u5668\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01<\/p>\n<h2>&nbsp;\u80cc\u666f<\/h2>\n<p>\u4f7f\u7528\u4e1a\u52a1\u573a\u666f\uff1a<\/p>\n<p><strong>1.\u5229\u7528\u6570\u636e\u5e93\u81ea\u589e\u4e3b\u952e\u751f\u6210\u552f\u4e00ID\uff0c\u65e0\u6cd5\u6ee1\u8db3\u5404\u4e2a\u7cfb\u7edf\u72ec\u81ea\u751f\u6210\u81ea\u589e\u7684\u552f\u4e00ID\u9700\u6c42\u3002<\/strong><\/p>\n<p>\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\uff0c\u9700\u8981\u751f\u6210\u552f\u4e00ID\u7684\u7cfb\u7edf\u4e0d\u6b62\u4e00\u4e2a\uff0c\u8fd9\u4e9bID\u7684\u751f\u6210\u5728\u5404\u81ea\u4e1a\u52a1\u5185\u662f\u72ec\u7acb\u7684\uff0c\u540c\u65f6\u5305\u542b\u4e1a\u52a1\u89c4\u5219\uff0c\u5982\u679c\u8981\u5229\u7528\u6570\u636e\u5e93\u81ea\u589e\u4e3b\u952e\u751f\u6210\u552f\u4e00ID\uff0c\u9700\u8981\u6bcf\u4e2a\u4e1a\u52a1\u90e8\u95e8\u521b\u5efa\u4e00\u5f20\u8868\u6765\u4e3a\u552f\u4e00ID\u670d\u52a1\u3002<\/p>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230224\/167724273563f8b16fb2214.jpg\" style=\"max-width:100%\" class=\"aligncenter\"> <\/p>\n<p><strong>2.\u6570\u636e\u5e93\u81ea\u589e\u4e3b\u952e\u65e0\u6cd5\u6ee1\u8db3\u6027\u80fd\u8981\u6c42\u3002<\/strong><\/p>\n<p>\u800c\u56e0\u4e3aRedis\u7684\u9ad8\u6027\u80fd\u548c\u5355\u7ebf\u7a0b\u7279\u6027\uff0c\u4f7f\u5b83\u5728\u8fd9\u65b9\u9762\u6709\u72ec\u7279\u7684\u4f18\u52bf\u3002<\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u5404\u4e2a\u7cfb\u7edf\u72ec\u81ea\u751f\u6210\u81ea\u589e\u7684\u552f\u4e00ID\u7684\u9700\u6c42\uff0c\u800c\u53c8\u5b9e\u73b0\u7b80\u5355\u6709\u6548\uff0c\u505a\u4e86\u5982\u4e0b\u8bbe\u8ba1\u3002<\/p>\n<h2>\u5404\u7cfb\u7edf\u72ec\u7acb\u7684\u81ea\u589eID\u751f\u6210\u5668<\/h2>\n<p><strong>\u6570\u636e\u5e93\u8bbe\u8ba1(mysql)<\/strong><\/p>\n<p>DROP TABLE IF EXISTS sys_id_record;<br \/>CREATE TABLE sys_id_record(<br \/>id INT UNSIGNED AUTO_INCREMENT,<br \/>sys_id VARCHAR(40) NOT NULL COMMENT &#8216;\u5b50\u7cfb\u7edfID&#8217;,<br \/>sys_name VARCHAR(100) NOT NULL COMMENT &#8216;\u5b50\u7cfb\u7edf\u540d\u79f0&#8217;,<br \/>id_from INT NOT NULL DEFAULT 1 COMMENT &#8216;\u5b50\u7cfb\u7edfID\u8d77\u59cb\u503c&#8217;,<br \/>lenth SMALLINT NOT NULL DEFAULT 10 COMMENT &#8216;\u5b50\u7cfb\u7edfID\u957f\u5ea6&#8217;,<br \/>enabled VARCHAR(1) NOT NULL DEFAULT &#8216;Y&#8217;,<br \/>created_by VARCHAR(40) NOT NULL COMMENT &#8216;\u521b\u5efa\u4eba&#8217;,<br \/>created_date DATETIME COMMENT &#8216;\u521b\u5efa\u65f6\u95f4&#8217;,<br \/>updated_by VARCHAR(40) NOT NULL COMMENT &#8216;\u66f4\u65b0\u4eba&#8217;,<br \/>updated_date DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT &#8216;\u66f4\u65b0\u65f6\u95f4&#8217;,<br \/>PRIMARY KEY ( id )<br \/>)ENGINE=INNODB DEFAULT CHARSET=utf8;<\/p>\n<p><strong>\u4e3b\u8981\u4ee3\u7801<\/strong><\/p>\n<ul>\n<li>\u5b9a\u4e49\u670d\u52a1\u63a5\u53e3<\/li>\n<\/ul>\n<p>package com.davidwang456.test;<\/p>\n<p>public interface IDGenerateService {<br \/>String getIDGenerateBySys(String sysId);<br \/>}<\/p>\n<ul>\n<li>\u63a5\u53e3\u670d\u52a1\u5b9e\u73b0<\/li>\n<\/ul>\n<p>package com.davidwang456.test;<\/p>\n<p>import java.util.ArrayList;<br \/>import java.util.List;<\/p>\n<p>import org.springframework.beans.factory.annotation.Autowired;<br \/>import org.springframework.data.redis.core.RedisTemplate;<br \/>import org.springframework.stereotype.Service;<\/p>\n<p>@Service<br \/>public class IDGenerateServiceImpl implements IDGenerateService {<br \/>@Autowired<br \/>private SysIdRecordDao sysIdRecordDao;<\/p>\n<p>@Autowired<br \/>private RedisTemplate redisTemplate;<\/p>\n<p>@Override<br \/>public String getIDGenerateBySys(String sysId) {<br \/>init(sysId);<br \/>Object idSys=redisTemplate.opsForList().rightPop(sysId);<br \/>if(idSys==null) {<br \/>List records=sysIdRecordDao.getEnabledSysIdRecord(sysId); <br \/>if(records!=null&amp;&amp;records.isEmpty()) { <br \/>SysIdRecord record=records.get(0); <\/p>\n<p>List buf_ids=new ArrayList(); <br \/>for(int i=1;i buf_ids.add(record.getIdFrom+i); <br \/>} <br \/>redisTemplate.opsForList().leftPushAll(sysId, buf_ids); <br \/>record.setIdFrom(record.getIdFrom()+record.getLenth()); <br \/>sysIdRecordDao.save(record); <br \/>idSys=redisTemplate.opsForList().rightPop(sysId); <br \/>} <br \/>} <br \/>return sysId+&#8221;:&#8221;+idSys; <br \/>} <\/p>\n<p>private void init(String sysId) { <br \/>if(!redisTemplate.hasKey(sysId)) { <br \/>List records=sysIdRecordDao.getEnabledSysIdRecord(sysId); <br \/>if(records==null||records.isEmpty()) { <br \/>SysIdRecord record=new SysIdRecord(); <br \/>\/\/TODO <br \/>sysIdRecordDao.save(record); <br \/>} <br \/>} <\/p>\n<p>} <\/p>\n<p>} <\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u8fd9\u4e2a\u8bbe\u8ba1\u7684\u6838\u5fc3\u601d\u60f3\u662f\u6570\u636e\u5e93\u63a7\u5236\u5404\u4e2a\u5b50\u7cfb\u7edf\u7684\u8d77\u59cb\u503c\u548c\u6b65\u957f\uff0c\u7136\u540e\u5c06\u503c\u653e\u5230redis\u961f\u5217\u4e2d\uff0c\u4ece\u961f\u5217\u4e2d\u4f9d\u6b64\u53d6\u503c\u3002\u5f53\u4eceredis\u4e2d\u6d88\u8017\u6389\u540e\u518d\u6b21\u4ece\u6570\u636e\u5e93\u4e2d\u53d6\u4e00\u6279\u503c\uff0c\u4f9d\u6b64\u5faa\u73af\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u4f7f\u7528\u6570\u636e\u5e93\u548cRedis\u5f00\u53d1\u5404\u7cfb\u7edf\u72ec\u7acb\u7684\u81ea\u589eID\u751f\u6210\u5668\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728golang\u5b66\u4e60\u7f51\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528\u6570\u636e\u5e93\u548cRedis\u5f00\u53d1\u5404\u7cfb\u7edf&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201566","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201566","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201566"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201566\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201566"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201566"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}