{"id":717,"date":"2013-08-17T03:58:24","date_gmt":"2013-08-17T03:58:24","guid":{"rendered":"http:\/\/blog.zhukunqian.com\/?p=717"},"modified":"2015-03-09T07:59:23","modified_gmt":"2015-03-09T07:59:23","slug":"flickr%e4%b8%bb%e9%94%ae%e7%94%9f%e6%88%90%e7%ad%96%e7%95%a5","status":"publish","type":"post","link":"http:\/\/blog.zhukunqian.com\/?p=717","title":{"rendered":"flickr\u4e3b\u952e\u751f\u6210\u7b56\u7565"},"content":{"rendered":"<blockquote class=\"wp-embedded-content\" data-secret=\"dyZNACnZaw\"><p><a href=\"https:\/\/code.flickr.net\/2010\/02\/08\/ticket-servers-distributed-unique-primary-keys-on-the-cheap\/\">Ticket Servers: Distributed Unique Primary Keys on the Cheap<\/a><\/p><\/blockquote>\n<p><iframe class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Ticket Servers: Distributed Unique Primary Keys on the Cheap&#8221; &#8212; code.flickr.com\" src=\"https:\/\/code.flickr.net\/2010\/02\/08\/ticket-servers-distributed-unique-primary-keys-on-the-cheap\/embed\/#?secret=PyfUVL8psK#?secret=dyZNACnZaw\" data-secret=\"dyZNACnZaw\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<p>&nbsp;<\/p>\n<p>\u6d4b\u8bd5\u4e00\u4e0b\u4e3b\u952e\u751f\u6210\u7684\u6027\u80fd\uff0c\u901a\u8fc7\u4ee3\u7801\u8c03\u7528\u751f\u621010\u4e07\u6b21\u4e3b\u952e\u4e3a\u4f8b\u3002<\/p>\n<p>\u6d4b\u8bd5\u7684\u673a\u5668\uff1a8G\u5185\u5b58\uff0c\u81ea\u8eab\u8dd1\u77407\u4e2atomcat\uff0c\u6bcf\u4e2a\u5206\u914d512M\u5185\u5b58\u3002mysql\u4e5f\u5b89\u88c5\u5728\u6b64\u673a\u5668\u4e0a\u3002<\/p>\n<p>1\u3001innodb\u65e0\u4e8b\u52a1(never)<\/p>\n<p>\u5355\u7ebf\u7a0b\uff1a746\u79d2\uff0c\u6bcf\u4e2a\u8bf7\u6c427.4\u79d2<\/p>\n<p>10\u7ebf\u7a0b\uff1a540\u79d2\uff0c\u6bcf\u4e2a\u8bf7\u6c425.4\u79d2<\/p>\n<p>50\u7ebf\u7a0b\uff1a\u8017\u65f6\u592a\u957f\uff0c2449,\u6bcf\u4e2a\u8bf7\u6c4224\u79d2\uff0c\u7ecf\u8fc7\u67e5\u627e\uff0c\u53d1\u73b0\u662f\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u6700\u5927\u6570\u91cf\u9650\u5236\u4e3a20\u5f71\u54cd\u4e86\u6027\u80fd\u3002\u628a\u6700\u5927\u8fde\u63a5\u6570\u6539\u4e3a100\uff0c\u66f4\u6076\u6027\u7684\u4e8b\u4ef6\u4ea7\u751f\u4e86\uff0cDB\u4e2d\u7684\u7ebf\u7a0b\u7adf\u7136\u5f00\u59cb\u4e92\u76f8\u7ade\u4e89\u8d77\u6765\uff0c\u5bfc\u81f4\u4e25\u91cd\u5f71\u54cd\u6027\u80fd\u3002<\/p>\n<p>2\u3001innodb\u4e8b\u52a1<\/p>\n<p>3\u3001MYISAM\u65e0\u4e8b\u52a1<\/p>\n<p>\u5355\u7ebf\u7a0b\uff1a476\u79d2,\u5e73\u5747\u6bcf\u6b21\u8bf7\u6c424.7ms<\/p>\n<p>10\u7ebf\u7a0b: 244\u79d2,\u5e73\u5747\u6bcf\u6b21\u8bf7\u6c422.4ms<\/p>\n<p>50\u7ebf\u7a0b:237\u79d2\uff0c\u5e73\u5747\u6bcf\u79d22.3ms<\/p>\n<p>100\u7ebf\u7a0b:235\u79d2,\u5e73\u5747\u6bcf\u79d22.3ms<\/p>\n<p>4\u3001MYISAM\u6709\u4e8b\u52a1(MYISAM\u672c\u8eab\u4e0d\u652f\u6301\u4e8b\u52a1\uff0c\u53ea\u662f\u6d4b\u8bd5\u4e0bspring aop\u7684\u5f71\u54cd)<\/p>\n<p>\u5355\u7ebf\u7a0b\uff1a\u5728aop\u4e8b\u52a1\u4e2d,503\u79d2\uff0c\u6bcf\u4e2a\u8bf7\u6c425ms,\u4e0d\u5728aop\u4e8b\u52a1\u4e2d,446\u79d2\uff0c\u6bcf\u79d2\u8bf7\u6c424.4<\/p>\n<p>10\u7ebf\u7a0b\uff1a\u5728aop\u4e8b\u52a1\u4e2d,238\u79d2\uff0c\u6bcf\u4e2a\u8bf7\u6c422.3\u79d2\uff0c\u4e0d\u5728aop\u4e8b\u52a1\u4e2d,234\u79d2\uff0c\u6bcf\u4e2a\u8bf7\u6c422.3\u79d2<\/p>\n<p>\u7ed3\u8bba\uff1aInnoDB\u662f\u5e76\u53d1\u6570\u8fbe\u5230\u4e00\u5b9a\u6570\u636e\u540e\uff0cDB\u4e2d\u9501\u7684\u7ade\u4e89\u5389\u5bb3\u3002\u4f46\u662fmyisam\u8868\u5012\u6ca1\u6709\u8fd9\u79cd\u95ee\u9898\uff0c\u5e76\u53d1\u6570\u5728100\u65f6\u4e5f\u6ca1\u6709\u9047\u5230\u95ee\u9898\u3002<\/p>\n<p>\u5728aop\u4e8b\u52a1\u4e2d\u591a\u5c11\u4f1a\u6709\u4e00\u70b9\u5f71\u54cd\uff0c\u4f46\u672c\u8eab\u662fmyisam\u8868\uff0c\u5f71\u54cd\u5728\u53ef\u63a5\u53d7\u8303\u56f4\u5185\u3002<\/p>\n<p>\u53ef\u6539\u8fdb\u7684\u5730\u65b9\uff1a\u8fd9\u6b21\u6d4b\u8bd5\u53ea\u662f\u4f7f\u7528\u4e00\u4e2a\u8868\uff0c\u5728flicker\u4e2d\uff0c\u4e5f\u4ecb\u7ecd\u4e86\u5982\u4f55\u5206\u8868\u6765\u5206\u62c5\u8d1f\u8f7d\u3002\u4f46\u5bf9\u8fd9\u4e2a\u9879\u76ee\u6765\u8bf4\uff0c\u5df2\u7ecf\u8db3\u591f\u6ee1\u8db3\u9700\u6c42\u4e86\u3002\u6682\u4e0d\u8003\u8651\u5206\u8868\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>2015.03.09<\/p>\n<p>\u9644\u4e0b\u9879\u76ee\u4e2d\u4f7f\u7528\u7684\u4ee3\u7801:<\/p>\n<pre class=\"brush: java; gutter: true\">package cn.joylab.game.service.impl;\r\n\r\nimport java.util.concurrent.atomic.AtomicInteger;\r\nimport java.util.concurrent.atomic.AtomicLong;\r\n\r\nimport org.apache.commons.logging.Log;\r\nimport org.apache.commons.logging.LogFactory;\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.stereotype.Component;\r\n\r\nimport cn.joylab.game.dao2.Ticket32Dao;\r\nimport cn.joylab.game.dao2.Ticket64Dao;\r\nimport cn.joylab.game.model.Ticket32;\r\nimport cn.joylab.game.model.Ticket64;\r\nimport cn.joylab.game.service.TicketManager;\r\n\r\n@Component(&quot;ticketManager&quot;)\r\npublic class TicketManagerImpl implements TicketManager {\r\n\r\n\tprivate static final int NUM = 1000;\r\n\r\n\tprivate AtomicInteger a32 = new AtomicInteger();\r\n\tprivate AtomicInteger a32_ = new AtomicInteger();\r\n\tprivate AtomicInteger b32 = new AtomicInteger();\r\n\tprivate AtomicInteger b32_ = new AtomicInteger();\r\n\tprivate AtomicInteger c32 = new AtomicInteger();\r\n\tprivate AtomicInteger c32_ = new AtomicInteger();\r\n\tprivate AtomicInteger d32 = new AtomicInteger();\r\n\tprivate AtomicInteger d32_ = new AtomicInteger();\r\n\tprivate AtomicInteger e32 = new AtomicInteger();\r\n\tprivate AtomicInteger e32_ = new AtomicInteger();\r\n\tprivate AtomicInteger f32 = new AtomicInteger();\r\n\tprivate AtomicInteger f32_ = new AtomicInteger();\r\n\tprivate AtomicInteger g32 = new AtomicInteger();\r\n\tprivate AtomicInteger g32_ = new AtomicInteger();\r\n\tprivate AtomicInteger h32 = new AtomicInteger();\r\n\tprivate AtomicInteger h32_ = new AtomicInteger();\r\n\r\n\tprivate AtomicLong a64 = new AtomicLong();\r\n\tprivate AtomicLong a64_ = new AtomicLong();\r\n\tprivate AtomicLong b64 = new AtomicLong();\r\n\tprivate AtomicLong b64_ = new AtomicLong();\r\n\tprivate AtomicLong c64 = new AtomicLong();\r\n\tprivate AtomicLong c64_ = new AtomicLong();\r\n\tprivate AtomicLong d64 = new AtomicLong();\r\n\tprivate AtomicLong d64_ = new AtomicLong();\r\n\tprivate AtomicLong e64 = new AtomicLong();\r\n\tprivate AtomicLong e64_ = new AtomicLong();\r\n\tprivate AtomicLong f64 = new AtomicLong();\r\n\tprivate AtomicLong f64_ = new AtomicLong();\r\n\tprivate AtomicLong g64 = new AtomicLong();\r\n\tprivate AtomicLong g64_ = new AtomicLong();\r\n\tprivate AtomicLong h64 = new AtomicLong();\r\n\tprivate AtomicLong h64_ = new AtomicLong();\r\n\r\n\tpublic int getNewId32(String stub) {\r\n\t\tif (&quot;a&quot;.equals(stub)) {\r\n\t\t\treturn getNewId32_1(stub, a32, a32_);\r\n\t\t} else if (&quot;b&quot;.equals(stub)) {\r\n\t\t\treturn getNewId32_1(stub, b32, b32_);\r\n\t\t} else if (&quot;c&quot;.equals(stub)) {\r\n\t\t\treturn getNewId32_1(stub, c32, c32_);\r\n\t\t} else if (&quot;d&quot;.equals(stub)) {\r\n\t\t\treturn getNewId32_1(stub, d32, d32_);\r\n\t\t} else if (&quot;e&quot;.equals(stub)) {\r\n\t\t\treturn getNewId32_1(stub, e32, e32_);\r\n\t\t} else if (&quot;f&quot;.equals(stub)) {\r\n\t\t\treturn getNewId32_1(stub, f32, f32_);\r\n\t\t} else if (&quot;g&quot;.equals(stub)) {\r\n\t\t\treturn getNewId32_1(stub, g32, g32_);\r\n\t\t} else if (&quot;h&quot;.equals(stub)) {\r\n\t\t\treturn getNewId32_1(stub, h32, h32_);\r\n\t\t} else {\r\n\t\t\tlog.error(&quot;\u672a\u89e3\u6790\u7684\u5b57\u7b26\u4e32:&quot; + stub);\r\n\t\t}\r\n\t\treturn -1;\r\n\t}\r\n\r\n\tprivate int getNewId32_1(String stub, AtomicInteger a, AtomicInteger b) {\r\n\t\tif (a.get() == 0) {\r\n\t\t\tint id = getNewId32_2(stub);\r\n\t\t\ta.set(id);\r\n\t\t\tb.set(NUM);\r\n\t\t\treturn id * NUM;\r\n\t\t}\r\n\t\tint i = b.decrementAndGet();\r\n\t\tif (i &gt; 0) {\r\n\t\t\treturn a.get() * NUM + (NUM - i);\r\n\t\t} else {\r\n\t\t\tint id = getNewId32_2(stub);\r\n\t\t\ta.set(id);\r\n\t\t\tb.set(NUM);\r\n\t\t\treturn id * NUM;\r\n\t\t}\r\n\t}\r\n\r\n\tprivate int getNewId32_2(String stub) {\r\n\t\tTicket32 ticket32 = new Ticket32();\r\n\t\tticket32.setStub(stub);\r\n\t\tif (&quot;a&quot;.equals(stub)) {\r\n\t\t\tticket32Dao.getNewId_a(ticket32);\r\n\t\t} else if (&quot;b&quot;.equals(stub)) {\r\n\t\t\tticket32Dao.getNewId_b(ticket32);\r\n\t\t} else if (&quot;c&quot;.equals(stub)) {\r\n\t\t\tticket32Dao.getNewId_c(ticket32);\r\n\t\t} else if (&quot;d&quot;.equals(stub)) {\r\n\t\t\tticket32Dao.getNewId_d(ticket32);\r\n\t\t} else if (&quot;e&quot;.equals(stub)) {\r\n\t\t\tticket32Dao.getNewId_e(ticket32);\r\n\t\t} else if (&quot;f&quot;.equals(stub)) {\r\n\t\t\tticket32Dao.getNewId_f(ticket32);\r\n\t\t} else if (&quot;g&quot;.equals(stub)) {\r\n\t\t\tticket32Dao.getNewId_g(ticket32);\r\n\t\t} else if (&quot;h&quot;.equals(stub)) {\r\n\t\t\tticket32Dao.getNewId_h(ticket32);\r\n\t\t} else {\r\n\t\t\tlog.error(&quot;\u672a\u89e3\u6790\u7684\u5b57\u7b26\u4e32:&quot; + stub);\r\n\t\t}\r\n\t\treturn ticket32.getId();\r\n\t}\r\n\r\n\tpublic long getNewId64(String stub) {\r\n\t\tif (&quot;a&quot;.equals(stub)) {\r\n\t\t\treturn getNewId64_1(stub, a64, a64_);\r\n\t\t} else if (&quot;b&quot;.equals(stub)) {\r\n\t\t\treturn getNewId64_1(stub, b64, b64_);\r\n\t\t} else if (&quot;c&quot;.equals(stub)) {\r\n\t\t\treturn getNewId64_1(stub, c64, c64_);\r\n\t\t} else if (&quot;d&quot;.equals(stub)) {\r\n\t\t\treturn getNewId64_1(stub, d64, d64_);\r\n\t\t} else if (&quot;e&quot;.equals(stub)) {\r\n\t\t\treturn getNewId64_1(stub, e64, e64_);\r\n\t\t} else if (&quot;f&quot;.equals(stub)) {\r\n\t\t\treturn getNewId64_1(stub, f64, f64_);\r\n\t\t} else if (&quot;g&quot;.equals(stub)) {\r\n\t\t\treturn getNewId64_1(stub, g64, g64_);\r\n\t\t} else if (&quot;h&quot;.equals(stub)) {\r\n\t\t\treturn getNewId64_1(stub, h64, h64_);\r\n\t\t} else {\r\n\t\t\tlog.error(&quot;\u672a\u89e3\u6790\u7684\u5b57\u7b26\u4e32:&quot; + stub);\r\n\t\t}\r\n\t\treturn -1;\r\n\t}\r\n\r\n\tprivate long getNewId64_1(String stub, AtomicLong a, AtomicLong b) {\r\n\t\tif (a.get() == 0) {\r\n\t\t\tlong id = getNewId64_2(stub);\r\n\t\t\ta.set(id);\r\n\t\t\tb.set(NUM);\r\n\t\t\treturn id * NUM;\r\n\t\t}\r\n\t\tlong i = b.decrementAndGet();\r\n\t\tif (i &gt; 0) {\r\n\t\t\treturn a.get() * NUM + (NUM - i);\r\n\t\t} else {\r\n\t\t\tlong id = getNewId64_2(stub);\r\n\t\t\ta.set(id);\r\n\t\t\tb.set(NUM);\r\n\t\t\treturn id * NUM;\r\n\t\t}\r\n\t}\r\n\r\n\tprivate long getNewId64_2(String stub) {\r\n\t\tTicket64 ticket64 = new Ticket64();\r\n\t\tticket64.setStub(stub);\r\n\t\tif (&quot;a&quot;.equals(stub)) {\r\n\t\t\tticket64Dao.getNewId_a(ticket64);\r\n\t\t} else if (&quot;b&quot;.equals(stub)) {\r\n\t\t\tticket64Dao.getNewId_b(ticket64);\r\n\t\t} else if (&quot;c&quot;.equals(stub)) {\r\n\t\t\tticket64Dao.getNewId_c(ticket64);\r\n\t\t} else if (&quot;d&quot;.equals(stub)) {\r\n\t\t\tticket64Dao.getNewId_d(ticket64);\r\n\t\t} else if (&quot;e&quot;.equals(stub)) {\r\n\t\t\tticket64Dao.getNewId_e(ticket64);\r\n\t\t} else if (&quot;f&quot;.equals(stub)) {\r\n\t\t\tticket64Dao.getNewId_f(ticket64);\r\n\t\t} else if (&quot;g&quot;.equals(stub)) {\r\n\t\t\tticket64Dao.getNewId_g(ticket64);\r\n\t\t} else if (&quot;h&quot;.equals(stub)) {\r\n\t\t\tticket64Dao.getNewId_h(ticket64);\r\n\t\t} else {\r\n\t\t\tlog.error(&quot;\u672a\u89e3\u6790\u7684\u5b57\u7b26\u4e32:&quot; + stub);\r\n\t\t}\r\n\t\treturn ticket64.getId();\r\n\t}\r\n\r\n\t@Autowired\r\n\tprivate Ticket32Dao ticket32Dao;\r\n\t@Autowired\r\n\tprivate Ticket64Dao ticket64Dao;\r\n\tprivate static final Log log = LogFactory.getLog(TicketManagerImpl.class);\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Ticket Servers: Distributed Unique Prima &hellip;<\/p>\n<p class=\"read-more\"><a href=\"http:\/\/blog.zhukunqian.com\/?p=717\">\u7ee7\u7eed\u9605\u8bfb &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"_links":{"self":[{"href":"http:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/posts\/717"}],"collection":[{"href":"http:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.zhukunqian.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=717"}],"version-history":[{"count":5,"href":"http:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/posts\/717\/revisions"}],"predecessor-version":[{"id":1157,"href":"http:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/posts\/717\/revisions\/1157"}],"wp:attachment":[{"href":"http:\/\/blog.zhukunqian.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=717"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.zhukunqian.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=717"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.zhukunqian.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=717"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}