{"id":1860,"date":"2010-09-17T09:46:00","date_gmt":"2010-09-17T09:46:00","guid":{"rendered":"http:\/\/www.icocean.com\/blog\/?p=1860"},"modified":"1970-01-01T07:00:00","modified_gmt":"1970-01-01T07:00:00","slug":"memcached%E6%B7%B1%E5%BA%A6%E5%88%86%E6%9E%90","status":"publish","type":"post","link":"https:\/\/www.icocean.com\/blog\/?p=1860","title":{"rendered":"Memcached\u6df1\u5ea6\u5206\u6790"},"content":{"rendered":"<p>Memcached\u662fdanga.com\uff08\u8fd0\u8425LiveJournal\u7684\u6280\u672f\u56e2\u961f\uff09\u5f00\u53d1\u7684\u4e00\u5957\u5206\u5e03\u5f0f\u5185\u5b58\u5bf9\u8c61\u7f13\u5b58\u7cfb\u7edf\uff0c\u7528\u4e8e\u5728\u52a8\u6001\u7cfb\u7edf\u4e2d\u51cf\u5c11\u6570\u636e\u5e93\u8d1f\u8f7d\uff0c\u63d0\u5347\u6027\u80fd\u3002\u5173\u4e8e\u8fd9\u4e2a\u4e1c\u897f\uff0c\u76f8\u4fe1\u5f88\u591a\u4eba\u90fd\u7528\u8fc7\uff0c\u672c\u6587\u610f\u5728\u901a\u8fc7\u5bf9memcached\u7684\u5b9e\u73b0\u53ca\u4ee3\u7801\u5206\u6790\uff0c\u83b7\u5f97\u5bf9\u8fd9\u4e2a\u51fa\u8272\u7684\u5f00\u6e90\u8f6f\u4ef6\u66f4\u6df1\u5165\u7684\u4e86\u89e3\uff0c\u5e76\u53ef\u4ee5\u6839\u636e\u6211\u4eec\u7684\u9700\u8981\u5bf9\u5176\u8fdb\u884c\u66f4\u8fdb\u4e00\u6b65\u7684\u4f18\u5316\u3002\u672b\u4e86\u5c06\u901a\u8fc7\u5bf9BSM_Memcache\u6269\u5c55\u7684\u5206\u6790\uff0c\u52a0\u6df1\u5bf9memcached\u7684\u4f7f\u7528\u65b9\u5f0f\u7406\u89e3\u3002<\/p>\n<p>\u672c\u6587\u7684\u90e8\u5206\u5185\u5bb9\u53ef\u80fd\u9700\u8981\u6bd4\u8f83\u597d\u7684\u6570\u5b66\u57fa\u7840\u4f5c\u4e3a\u8f85\u52a9\u3002<\/p>\n<p>\u25ceMemcached\u662f\u4ec0\u4e48<\/p>\n<p>\u5728\u9610\u8ff0\u8fd9\u4e2a\u95ee\u9898\u4e4b\u524d\uff0c\u6211\u4eec\u9996\u5148\u8981\u6e05\u695a\u5b83\u201c\u4e0d\u662f\u4ec0\u4e48\u201d\u3002\u5f88\u591a\u4eba\u628a\u5b83\u5f53\u4f5c\u548cSharedMemory\u90a3\u79cd\u5f62\u5f0f\u7684\u5b58\u50a8\u8f7d\u4f53\u6765\u4f7f\u7528\uff0c\u867d\u7136memcached\u4f7f\u7528\u4e86\u540c\u6837\u7684\u201cKey=>Value\u201d\u65b9\u5f0f\u7ec4\u7ec7\u6570\u636e\uff0c\u4f46\u662f\u5b83\u548c\u5171\u4eab\u5185\u5b58\u3001APC\u7b49\u672c\u5730\u7f13\u5b58\u6709\u975e\u5e38\u5927\u7684\u533a\u522b\u3002Memcached\u662f\u5206\u5e03\u5f0f\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\u5b83\u4e0d\u662f\u672c\u5730\u7684\u3002\u5b83\u57fa\u4e8e\u7f51\u7edc\u8fde\u63a5\uff08\u5f53\u7136\u5b83\u4e5f\u53ef\u4ee5\u4f7f\u7528localhost\uff09\u65b9\u5f0f\u5b8c\u6210\u670d\u52a1\uff0c\u672c\u8eab\u5b83\u662f\u4e00\u4e2a\u72ec\u7acb\u4e8e\u5e94\u7528\u7684\u7a0b\u5e8f\u6216\u5b88\u62a4\u8fdb\u7a0b\uff08Daemon\u65b9\u5f0f\uff09\u3002<\/p>\n<p>Memcached \u4f7f\u7528libevent\u5e93\u5b9e\u73b0\u7f51\u7edc\u8fde\u63a5\u670d\u52a1\uff0c\u7406\u8bba\u4e0a\u53ef\u4ee5\u5904\u7406\u65e0\u9650\u591a\u7684\u8fde\u63a5\uff0c\u4f46\u662f\u5b83\u548cApache\u4e0d\u540c\uff0c\u5b83\u66f4\u591a\u7684\u65f6\u5019\u662f\u9762\u5411\u7a33\u5b9a\u7684\u6301\u7eed\u8fde\u63a5\u7684\uff0c\u6240\u4ee5\u5b83\u5b9e\u9645\u7684\u5e76\u53d1\u80fd\u529b\u662f\u6709\u9650\u5236\u7684\u3002\u5728\u4fdd\u5b88\u60c5\u51b5\u4e0bmemcached\u7684\u6700\u5927\u540c\u65f6\u8fde\u63a5\u6570\u4e3a200\uff0c\u8fd9\u548cLinux\u7ebf\u7a0b\u80fd\u529b\u6709\u5173\u7cfb\uff0c\u8fd9\u4e2a\u6570\u503c\u662f\u53ef\u4ee5\u8c03\u6574\u7684\u3002\u5173\u4e8e libevent\u53ef\u4ee5\u53c2\u8003\u76f8\u5173\u6587\u6863\u3002 Memcached\u5185\u5b58\u4f7f\u7528\u65b9\u5f0f\u4e5f\u548cAPC\u4e0d\u540c\u3002APC\u662f\u57fa\u4e8e\u5171\u4eab\u5185\u5b58\u548cMMAP\u7684\uff0cmemcachd\u6709\u81ea\u5df1\u7684\u5185\u5b58\u5206\u914d\u7b97\u6cd5\u548c\u7ba1\u7406\u65b9\u5f0f\uff0c\u5b83\u548c\u5171\u4eab\u5185\u5b58\u6ca1\u6709\u5173\u7cfb\uff0c\u4e5f\u6ca1\u6709\u5171\u4eab\u5185\u5b58\u7684\u9650\u5236\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6bcf\u4e2amemcached\u8fdb\u7a0b\u53ef\u4ee5\u7ba1\u74062GB\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u5982\u679c\u9700\u8981\u66f4\u591a\u7684\u7a7a\u95f4\uff0c\u53ef\u4ee5\u589e\u52a0\u8fdb\u7a0b\u6570\u3002<\/p>\n<p>\u25ceMemcached\u9002\u5408\u4ec0\u4e48\u573a\u5408<\/p>\n<p>\u5728\u5f88\u591a\u65f6\u5019\uff0cmemcached\u90fd\u88ab\u6ee5\u7528\u4e86\uff0c\u8fd9\u5f53\u7136\u5c11\u4e0d\u4e86\u5bf9\u5b83\u7684\u62b1\u6028\u3002\u6211\u7ecf\u5e38\u5728\u8bba\u575b\u4e0a\u770b\u89c1\u6709\u4eba\u53d1\u8d34\uff0c\u7c7b\u4f3c\u4e8e\u201c\u5982\u4f55\u63d0\u9ad8\u6548\u7387\u201d\uff0c\u56de\u590d\u662f\u201c\u7528memcached\u201d\uff0c\u81f3\u4e8e\u600e\u4e48\u7528\uff0c\u7528\u5728\u54ea\u91cc\uff0c\u7528\u6765\u5e72\u4ec0\u4e48\u4e00\u53e5\u6ca1\u6709\u3002memcached\u4e0d\u662f\u4e07\u80fd\u7684\uff0c\u5b83\u4e5f\u4e0d\u662f\u9002\u7528\u5728\u6240\u6709\u573a\u5408\u3002<\/p>\n<p>Memcached \u662f\u201c\u5206\u5e03\u5f0f\u201d\u7684\u5185\u5b58\u5bf9\u8c61\u7f13\u5b58\u7cfb\u7edf\uff0c\u90a3\u4e48\u5c31\u662f\u8bf4\uff0c\u90a3\u4e9b\u4e0d\u9700\u8981\u201c\u5206\u5e03\u201d\u7684\uff0c\u4e0d\u9700\u8981\u5171\u4eab\u7684\uff0c\u6216\u8005\u5e72\u8106\u89c4\u6a21\u5c0f\u5230\u53ea\u6709\u4e00\u53f0\u670d\u52a1\u5668\u7684\u5e94\u7528\uff0cmemcached\u4e0d\u4f1a\u5e26\u6765\u4efb\u4f55\u597d\u5904\uff0c\u76f8\u53cd\u8fd8\u4f1a\u62d6\u6162\u7cfb\u7edf\u6548\u7387\uff0c\u56e0\u4e3a\u7f51\u7edc\u8fde\u63a5\u540c\u6837\u9700\u8981\u8d44\u6e90\uff0c\u5373\u4f7f\u662fUNIX\u672c\u5730\u8fde\u63a5\u4e5f\u4e00\u6837\u3002 \u5728\u6211\u4e4b\u524d\u7684\u6d4b\u8bd5\u6570\u636e\u4e2d\u663e\u793a\uff0cmemcached\u672c\u5730\u8bfb\u5199\u901f\u5ea6\u8981\u6bd4\u76f4\u63a5PHP\u5185\u5b58\u6570\u7ec4\u6162\u51e0\u5341\u500d\uff0c\u800cAPC\u3001\u5171\u4eab\u5185\u5b58\u65b9\u5f0f\u90fd\u548c\u76f4\u63a5\u6570\u7ec4\u5dee\u4e0d\u591a\u3002\u53ef\u89c1\uff0c\u5982\u679c\u53ea\u662f\u672c\u5730\u7ea7\u7f13\u5b58\uff0c\u4f7f\u7528memcached\u662f\u975e\u5e38\u4e0d\u5212\u7b97\u7684\u3002<!--more--><\/p>\n<p>Memcached\u5728\u5f88\u591a\u65f6\u5019\u90fd\u662f\u4f5c\u4e3a\u6570\u636e\u5e93\u524d\u7aefcache\u4f7f\u7528\u7684\u3002\u56e0\u4e3a\u5b83\u6bd4\u6570\u636e\u5e93\u5c11\u4e86\u5f88\u591aSQL\u89e3\u6790\u3001\u78c1\u76d8\u64cd\u4f5c\u7b49\u5f00\u9500\uff0c\u800c\u4e14\u5b83\u662f\u4f7f\u7528\u5185\u5b58\u6765\u7ba1\u7406\u6570\u636e\u7684\uff0c\u6240\u4ee5\u5b83\u53ef\u4ee5\u63d0\u4f9b\u6bd4\u76f4\u63a5\u8bfb\u53d6\u6570\u636e\u5e93\u66f4\u597d\u7684\u6027\u80fd\uff0c\u5728\u5927\u578b\u7cfb\u7edf\u4e2d\uff0c\u8bbf\u95ee\u540c\u6837\u7684\u6570\u636e\u662f\u5f88\u9891\u7e41\u7684\uff0cmemcached\u53ef\u4ee5\u5927\u5927\u964d\u4f4e\u6570\u636e\u5e93\u538b\u529b\uff0c\u4f7f\u7cfb\u7edf\u6267\u884c\u6548\u7387\u63d0\u5347\u3002\u53e6\u5916\uff0cmemcached\u4e5f\u7ecf\u5e38\u4f5c\u4e3a\u670d\u52a1\u4e4b\u95f4\u6570\u636e\u5171\u4eab\u7684\u5b58\u50a8\u5a92\u4ecb\uff0c\u4f8b\u5982\u5728SSO\u7cfb\u7edf\u4e2d\u4fdd\u5b58\u7cfb\u7edf\u5355\u70b9\u767b\u9646\u72b6\u6001\u7684\u6570\u636e\u5c31\u53ef\u4ee5\u4fdd\u5b58\u5728memcached\u4e2d\uff0c\u88ab\u591a\u4e2a\u5e94\u7528\u5171\u4eab\u3002<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cmemcached\u4f7f\u7528\u5185\u5b58\u7ba1\u7406\u6570\u636e\uff0c\u6240\u4ee5\u5b83\u662f\u6613\u5931\u7684\uff0c\u5f53\u670d\u52a1\u5668\u91cd\u542f\uff0c\u6216\u8005memcached\u8fdb\u7a0b\u4e2d\u6b62\uff0c\u6570\u636e\u4fbf\u4f1a\u4e22\u5931\uff0c\u6240\u4ee5memcached\u4e0d\u80fd\u7528\u6765\u6301\u4e45\u4fdd\u5b58\u6570\u636e\u3002\u5f88\u591a\u4eba\u7684\u9519\u8bef\u7406\u89e3\uff0cmemcached\u7684\u6027\u80fd\u975e\u5e38\u597d\uff0c\u597d\u5230\u4e86\u5185\u5b58\u548c\u786c\u76d8\u7684\u5bf9\u6bd4\u7a0b\u5ea6\uff0c\u5176\u5b9ememcached\u4f7f\u7528\u5185\u5b58\u5e76\u4e0d\u4f1a\u5f97\u5230\u6210\u767e\u4e0a\u5343\u7684\u8bfb\u5199\u901f\u5ea6\u63d0\u9ad8\uff0c\u5b83\u7684\u5b9e\u9645\u74f6\u9888\u5728\u4e8e\u7f51\u7edc\u8fde\u63a5\uff0c\u5b83\u548c\u4f7f\u7528\u78c1\u76d8\u7684\u6570\u636e\u5e93\u7cfb\u7edf\u76f8\u6bd4\uff0c\u597d\u5904\u5728\u4e8e\u5b83\u672c\u8eab\u975e\u5e38\u201c\u8f7b\u201d\uff0c\u56e0\u4e3a\u6ca1\u6709\u8fc7\u591a\u7684\u5f00\u9500\u548c\u76f4\u63a5\u7684\u8bfb\u5199\u65b9\u5f0f\uff0c\u5b83\u53ef\u4ee5\u8f7b\u677e\u5e94\u4ed8\u975e\u5e38\u5927\u7684\u6570\u636e\u4ea4\u6362\u91cf\uff0c\u6240\u4ee5\u7ecf\u5e38\u4f1a\u51fa\u73b0\u4e24\u6761\u5343\u5146\u7f51\u7edc\u5e26\u5bbd\u90fd\u6ee1\u8d1f\u8377\u4e86\uff0cmemcached\u8fdb\u7a0b\u672c\u8eab\u5e76\u4e0d\u5360\u7528\u591a\u5c11CPU\u8d44\u6e90\u7684\u60c5\u51b5\u3002<\/p>\n<p>\u25ceMemcached\u7684\u5de5\u4f5c\u65b9\u5f0f<\/p>\n<p>\u4ee5\u4e0b\u7684\u90e8\u5206\u4e2d\uff0c\u8bfb\u8005\u6700\u597d\u80fd\u51c6\u5907\u4e00\u4efdmemcached\u7684\u6e90\u4ee3\u7801\u3002<\/p>\n<p>Memcached\u662f\u4f20\u7edf\u7684\u7f51\u7edc\u670d\u52a1\u7a0b\u5e8f\uff0c\u5982\u679c\u542f\u52a8\u7684\u65f6\u5019\u4f7f\u7528\u4e86-d\u53c2\u6570\uff0c\u5b83\u4f1a\u4ee5\u5b88\u62a4\u8fdb\u7a0b\u7684\u65b9\u5f0f\u6267\u884c\u3002\u521b\u5efa\u5b88\u62a4\u8fdb\u7a0b\u7531daemon.c\u5b8c\u6210\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u53ea\u6709\u4e00\u4e2adaemon\u51fd\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u5f88\u7b80\u5355\uff08\u5982\u65e0\u7279\u6b8a\u8bf4\u660e\uff0c\u4ee3\u7801\u4ee51.2.1\u4e3a\u51c6\uff09\uff1a<\/p>\n<p>CODE:<br \/>#include <fcntl.h><br \/>#include <stdlib.h><br \/>#include <unistd.h><\/p>\n<p>int<br \/>daemon(nochdir, noclose)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;int nochdir, noclose;<br \/>{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;int fd;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;switch (fork()) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;case -1:<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return (-1);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;case 0:<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;&nbsp;&nbsp;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;default:<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_exit(0);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;if (setsid() == -1)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return (-1);<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;if (!nochdir)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(void)chdir(&#8220;\/&#8221;);<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;if (!noclose &#038;&#038; (fd = open(&#8220;\/dev\/null&#8221;, O_RDWR, 0)) != -1) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(void)dup2(fd, STDIN_FILENO);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(void)dup2(fd, STDOUT_FILENO);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(void)dup2(fd, STDERR_FILENO);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (fd > STDERR_FILENO)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(void)close(fd);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;return (0);<br \/>}<br \/>\u8fd9\u4e2a\u51fd\u6570 fork \u4e86\u6574\u4e2a\u8fdb\u7a0b\u4e4b\u540e\uff0c\u7236\u8fdb\u7a0b\u5c31\u9000\u51fa\uff0c\u63a5\u7740\u91cd\u65b0\u5b9a\u4f4d STDIN \u3001 STDOUT \u3001 STDERR \u5230\u7a7a\u8bbe\u5907\uff0c daemon \u5c31\u5efa\u7acb\u6210\u529f\u4e86\u3002<\/p>\n<p>Memcached \u672c\u8eab\u7684\u542f\u52a8\u8fc7\u7a0b\uff0c\u5728 memcached.c \u7684 main \u51fd\u6570\u4e2d\u987a\u5e8f\u5982\u4e0b\uff1a<\/p>\n<p>1 \u3001\u8c03\u7528 settings_init() \u8bbe\u5b9a\u521d\u59cb\u5316\u53c2\u6570<br \/>2 \u3001\u4ece\u542f\u52a8\u547d\u4ee4\u4e2d\u8bfb\u53d6\u53c2\u6570\u6765\u8bbe\u7f6e setting \u503c<br \/>3 \u3001\u8bbe\u5b9a LIMIT \u53c2\u6570<br \/>4 \u3001\u5f00\u59cb\u7f51\u7edc socket \u76d1\u542c\uff08\u5982\u679c\u975e socketpath \u5b58\u5728\uff09\uff08 1.2 \u4e4b\u540e\u652f\u6301 UDP \u65b9\u5f0f\uff09<br \/>5 \u3001\u68c0\u67e5\u7528\u6237\u8eab\u4efd\uff08 Memcached \u4e0d\u5141\u8bb8 root \u8eab\u4efd\u542f\u52a8\uff09<br \/>6 \u3001\u5982\u679c\u6709 socketpath \u5b58\u5728\uff0c\u5f00\u542f UNIX \u672c\u5730\u8fde\u63a5\uff08Sock \u7ba1\u9053\uff09<br \/>7 \u3001\u5982\u679c\u4ee5 -d \u65b9\u5f0f\u542f\u52a8\uff0c\u521b\u5efa\u5b88\u62a4\u8fdb\u7a0b\uff08\u5982\u4e0a\u8c03\u7528 daemon \u51fd\u6570\uff09<br \/>8 \u3001\u521d\u59cb\u5316 item \u3001 event \u3001\u72b6\u6001\u4fe1\u606f\u3001 hash \u3001\u8fde\u63a5\u3001 slab<br \/>9 \u3001\u5982\u8bbe\u7f6e\u4e2d managed \u751f\u6548\uff0c\u521b\u5efa bucket \u6570\u7ec4<br \/>10 \u3001\u68c0\u67e5\u662f\u5426\u9700\u8981\u9501\u5b9a\u5185\u5b58\u9875<br \/>11 \u3001\u521d\u59cb\u5316\u4fe1\u53f7\u3001\u8fde\u63a5\u3001\u5220\u9664\u961f\u5217<br \/>12 \u3001\u5982\u679c daemon \u65b9\u5f0f\uff0c\u5904\u7406\u8fdb\u7a0b ID<br \/>13 \u3001event \u5f00\u59cb\uff0c\u542f\u52a8\u8fc7\u7a0b\u7ed3\u675f\uff0c main \u51fd\u6570\u8fdb\u5165\u5faa\u73af\u3002<\/p>\n<p>\u5728 daemon \u65b9\u5f0f\u4e2d\uff0c\u56e0\u4e3a stderr \u5df2\u7ecf\u88ab\u5b9a\u5411\u5230\u9ed1\u6d1e\uff0c\u6240\u4ee5\u4e0d\u4f1a\u53cd\u9988\u6267\u884c\u4e2d\u7684\u53ef\u89c1\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<p>memcached.c \u7684\u4e3b\u5faa\u73af\u51fd\u6570\u662f drive_machine \uff0c\u4f20\u5165\u53c2\u6570\u662f\u6307\u5411\u5f53\u524d\u7684\u8fde\u63a5\u7684\u7ed3\u6784\u6307\u9488\uff0c\u6839\u636e state \u6210\u5458\u7684\u72b6\u6001\u6765\u5b9a\u52a8\u4f5c\u3002<\/p>\n<p>Memcached \u4f7f\u7528\u4e00\u5957\u81ea\u5b9a\u4e49\u7684\u534f\u8bae\u5b8c\u6210\u6570\u636e\u4ea4\u6362\uff0c\u5b83\u7684 protocol \u6587\u6863\u53ef\u4ee5\u53c2\u8003\uff1a http:\/\/code.sixapart.com\/svn\/memcached\/trunk\/server\/doc\/protocol.txt<\/p>\n<p>\u5728API\u4e2d\uff0c\u6362\u884c\u7b26\u53f7\u7edf\u4e00\u4e3a&#92;r&#92;n<\/p>\n<p>\u25ceMemcached\u7684\u5185\u5b58\u7ba1\u7406\u65b9\u5f0f<\/p>\n<p>Memcached\u6709\u4e00\u4e2a\u5f88\u6709\u7279\u8272\u7684\u5185\u5b58\u7ba1\u7406\u65b9\u5f0f\uff0c\u4e3a\u4e86\u63d0\u9ad8\u6548\u7387\uff0c\u5b83\u4f7f\u7528\u9884\u7533\u8bf7\u548c\u5206\u7ec4\u7684\u65b9\u5f0f\u7ba1\u7406\u5185\u5b58\u7a7a\u95f4\uff0c\u800c\u5e76\u4e0d\u662f\u6bcf\u6b21\u9700\u8981\u5199\u5165\u6570\u636e\u7684\u65f6\u5019\u53bbmalloc\uff0c\u5220\u9664\u6570\u636e\u7684\u65f6\u5019free\u4e00\u4e2a\u6307\u9488\u3002Memcached\u4f7f\u7528slab->chunk\u7684\u7ec4\u7ec7\u65b9\u5f0f\u7ba1\u7406\u5185\u5b58\u3002<\/p>\n<p>1.1\u548c1.2\u7684slabs.c\u4e2d\u7684slab\u7a7a\u95f4\u5212\u5206\u7b97\u6cd5\u6709\u4e00\u4e9b\u4e0d\u540c\uff0c\u540e\u9762\u4f1a\u5206\u522b\u4ecb\u7ecd\u3002<\/p>\n<p>Slab \u53ef\u4ee5\u7406\u89e3\u4e3a\u4e00\u4e2a\u5185\u5b58\u5757\uff0c\u4e00\u4e2aslab\u662fmemcached\u4e00\u6b21\u7533\u8bf7\u5185\u5b58\u7684\u6700\u5c0f\u5355\u4f4d\uff0c\u5728memcached\u4e2d\uff0c\u4e00\u4e2aslab\u7684\u5927\u5c0f\u9ed8\u8ba4\u4e3a1048576\u5b57\u8282\uff081MB\uff09\uff0c\u6240\u4ee5memcached\u90fd\u662f\u6574MB\u7684\u4f7f\u7528\u5185\u5b58\u3002\u6bcf\u4e00\u4e2aslab\u88ab\u5212\u5206\u4e3a\u82e5\u5e72\u4e2achunk\uff0c\u6bcf\u4e2achunk\u91cc\u4fdd\u5b58\u4e00\u4e2aitem\uff0c\u6bcf\u4e2aitem\u540c\u65f6\u5305\u542b\u4e86item\u7ed3\u6784\u4f53\u3001key\u548cvalue\uff08\u6ce8\u610f\u5728memcached\u4e2d\u7684value\u662f\u53ea\u6709\u5b57\u7b26\u4e32\u7684\uff09\u3002slab\u6309\u7167\u81ea\u5df1\u7684id\u5206\u522b\u7ec4\u6210\u94fe\u8868\uff0c\u8fd9\u4e9b\u94fe\u8868\u53c8\u6309id\u6302\u5728\u4e00\u4e2aslabclass\u6570\u7ec4\u4e0a\uff0c\u6574\u4e2a\u7ed3\u6784\u770b\u8d77\u6765\u6709\u70b9\u50cf\u4e8c\u7ef4\u6570\u7ec4\u3002slabclass\u7684\u957f\u5ea6\u57281.1\u4e2d\u662f21\uff0c\u57281.2\u4e2d\u662f200\u3002<\/p>\n<p>slab\u6709\u4e00\u4e2a\u521d\u59cbchunk\u5927\u5c0f\uff0c1.1\u4e2d\u662f1\u5b57\u8282\uff0c1.2\u4e2d\u662f80\u5b57\u8282\uff0c1.2\u4e2d\u6709\u4e00\u4e2afactor\u503c\uff0c\u9ed8\u8ba4\u4e3a1.25<\/p>\n<p>\u5728 1.1\u4e2d\uff0cchunk\u5927\u5c0f\u8868\u793a\u4e3a\u521d\u59cb\u5927\u5c0f*2^n\uff0cn\u4e3aclassid\uff0c\u5373\uff1aid\u4e3a0\u7684slab\uff0c\u6bcfchunk\u5927\u5c0f1\u5b57\u8282\uff0cid\u4e3a1\u7684slab\uff0c\u6bcf chunk\u5927\u5c0f2\u5b57\u8282\uff0cid\u4e3a2\u7684slab\uff0c\u6bcfchunk\u5927\u5c0f4\u5b57\u8282\u2026\u2026id\u4e3a20\u7684slab\uff0c\u6bcfchunk\u5927\u5c0f\u4e3a1MB\uff0c\u5c31\u662f\u8bf4id\u4e3a20\u7684slab\u91cc\u53ea\u6709\u4e00\u4e2achunk\uff1a<\/p>\n<p>CODE:<br \/>void slabs_init(size_t limit) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;int i;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;int size=1;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;mem_limit = limit;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;for(i=0; i<=POWER_LARGEST; i++, size*=2) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;slabclass<i>.size = size;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;slabclass<i>.perslab = POWER_BLOCK \/ size;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;slabclass<i>.slots = 0;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;slabclass<i>.sl_curr = slabclass<i>.sl_total = slabclass<i>.slabs = 0;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;slabclass<i>.end_page_ptr = 0;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;slabclass<i>.end_page_free = 0;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;slabclass<i>.slab_list = 0;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;slabclass<i>.list_size = 0;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;slabclass<i>.killing = 0;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\/* for the test suite:&nbsp;&nbsp;faking of how much we&#39;ve already malloc&#39;d *\/<br \/>&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char *t_initial_malloc = getenv(&#8220;T_MEMD_INITIAL_MALLOC&#8221;);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (t_initial_malloc) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mem_malloced = atol(getenv(&#8220;T_MEMD_INITIAL_MALLOC&#8221;));<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\/* pre-allocate slabs by default, unless the environment variable<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for testing is set to something non-zero *\/<br \/>&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char *pre_alloc = getenv(&#8220;T_MEMD_SLABS_ALLOC&#8221;);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!pre_alloc &#124;&#124; atoi(pre_alloc)) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;slabs_preallocate(limit \/ POWER_BLOCK);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>}<br \/>\u57281.2\u4e2d\uff0cchunk\u5927\u5c0f\u8868\u793a\u4e3a\u521d\u59cb\u5927\u5c0f*f^n\uff0cf\u4e3afactor\uff0c\u5728memcached.c\u4e2d\u5b9a\u4e49\uff0cn\u4e3aclassid\uff0c\u540c\u65f6\uff0c201\u4e2a\u5934\u4e0d\u662f\u5168\u90e8\u90fd\u8981\u521d\u59cb\u5316\u7684\uff0c\u56e0\u4e3afactor\u53ef\uff0c\u521d\u59cb\u5316\u53ea\u5faa\u73af\u5230\u8ba1\u7b97\u51fa\u7684\u5927\u5c0f\u8fbe\u5230slab\u5927\u5c0f\u7684\u4e00\u534a\u4e3a\u6b62\uff0c\u800c\u4e14\u5b83\u662f\u4eceid1\u5f00\u59cb\u7684\uff0c\u5373\uff1aid\u4e3a1\u7684slab\uff0c\u6bcf chunk\u5927\u5c0f80\u5b57\u8282\uff0cid\u4e3a2\u7684slab\uff0c\u6bcfchunk\u5927\u5c0f80*f\uff0cid\u4e3a3\u7684slab\uff0c\u6bcfchunk\u5927\u5c0f80*f^2\uff0c\u521d\u59cb\u5316\u5927\u5c0f\u6709\u4e00\u4e2a\u4fee\u6b63\u503c CHUNK_ALIGN_BYTES\uff0c\u7528\u6765\u4fdd\u8bc1n-byte\u6392\u5217 \uff08\u4fdd\u8bc1\u7ed3\u679c\u662fCHUNK_ALIGN_BYTES\u7684\u6574\u500d\u6570\uff09\u3002\u8fd9\u6837\uff0c\u5728\u6807\u51c6\u60c5\u51b5\u4e0b\uff0cmemcached1.2\u4f1a\u521d\u59cb\u5316\u5230id40\uff0c\u8fd9\u4e2aslab\u4e2d\u6bcf\u4e2a chunk\u5927\u5c0f\u4e3a504692\uff0c\u6bcf\u4e2aslab\u4e2d\u6709\u4e24\u4e2achunk\u3002\u6700\u540e\uff0cslab_init\u51fd\u6570\u4f1a\u5728\u6700\u540e\u8865\u8db3\u4e00\u4e2aid41\uff0c\u5b83\u662f\u6574\u5757\u7684\uff0c\u4e5f\u5c31\u662f\u8fd9\u4e2a slab\u4e2d\u53ea\u6709\u4e00\u4e2a1MB\u5927\u7684chunk\uff1a<\/p>\n<p>CODE:<br \/>void slabs_init(size_t limit, double factor) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;int i = POWER_SMALLEST &#8211; 1;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;unsigned int size = sizeof(item) + settings.chunk_size;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\/* Factor of 2.0 means use the default memcached behavior *\/<br \/>&nbsp;&nbsp;&nbsp;&nbsp;if (factor == 2.0 &#038;&#038; size < 128)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;size = 128;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;mem_limit = limit;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;memset(slabclass, 0, sizeof(slabclass));<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;while (++i < POWER_LARGEST &#038;&#038; size <= POWER_BLOCK \/ 2) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/* Make sure items are always n-byte aligned *\/<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (size % CHUNK_ALIGN_BYTES)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;size += CHUNK_ALIGN_BYTES &#8211; (size % CHUNK_ALIGN_BYTES);<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;slabclass<i>.size = size;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;slabclass<i>.perslab = POWER_BLOCK \/ slabclass<i>.size;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;size *= factor;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (settings.verbose > 1) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr, &#8220;slab class %3d: chunk size %6d perslab %5d&#92;n&#8221;,<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i, slabclass<i>.size, slabclass<i>.perslab);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;power_largest = i;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;slabclass[power_largest].size = POWER_BLOCK;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;slabclass[power_largest].perslab = 1;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\/* for the test suite:&nbsp;&nbsp;faking of how much we&#39;ve already malloc&#39;d *\/<br \/>&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char *t_initial_malloc = getenv(&#8220;T_MEMD_INITIAL_MALLOC&#8221;);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (t_initial_malloc) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mem_malloced = atol(getenv(&#8220;T_MEMD_INITIAL_MALLOC&#8221;));<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n<p>#ifndef DONT_PREALLOC_SLABS<br \/>&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char *pre_alloc = getenv(&#8220;T_MEMD_SLABS_ALLOC&#8221;);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!pre_alloc &#124;&#124; atoi(pre_alloc)) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;slabs_preallocate(limit \/ POWER_BLOCK);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>#endif<br \/>}<br \/>\u7531\u4e0a\u53ef\u4ee5\u770b\u51fa\uff0cmemcached\u7684\u5185\u5b58\u5206\u914d\u662f\u6709\u5197\u4f59\u7684\uff0c\u5f53\u4e00\u4e2aslab\u4e0d\u80fd\u88ab\u5b83\u6240\u62e5\u6709\u7684chunk\u5927\u5c0f\u6574\u9664\u65f6\uff0cslab\u5c3e\u90e8\u5269\u4f59\u7684\u7a7a\u95f4\u5c31\u88ab\u4e22\u5f03\u4e86\uff0c\u5982id40\u4e2d\uff0c\u4e24\u4e2achunk\u5360\u7528\u4e861009384\u5b57\u8282\uff0c\u8fd9\u4e2aslab\u4e00\u5171\u67091MB\uff0c\u90a3\u4e48\u5c31\u670939192\u5b57\u8282\u88ab\u6d6a\u8d39\u4e86\u3002<\/p>\n<p>Memcached \u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u6765\u5206\u914d\u5185\u5b58\uff0c\u662f\u4e3a\u4e86\u53ef\u4ee5\u5feb\u901f\u7684\u901a\u8fc7item\u957f\u5ea6\u5b9a\u4f4d\u51faslab\u7684classid\uff0c\u6709\u4e00\u70b9\u7c7b\u4f3chash\uff0c\u56e0\u4e3aitem\u7684\u957f\u5ea6\u662f\u53ef\u4ee5\u8ba1\u7b97\u7684\uff0c\u6bd4\u5982\u4e00\u4e2aitem\u7684\u957f\u5ea6\u662f300\u5b57\u8282\uff0c\u57281.2\u4e2d\u5c31\u53ef\u4ee5\u5f97\u5230\u5b83\u5e94\u8be5\u4fdd\u5b58\u5728id7\u7684slab\u4e2d\uff0c\u56e0\u4e3a\u6309\u7167\u4e0a\u9762\u7684\u8ba1\u7b97\u65b9\u6cd5\uff0cid6\u7684chunk\u5927\u5c0f\u662f252\u5b57\u8282\uff0cid7\u7684chunk\u5927\u5c0f\u662f316\u5b57\u8282\uff0cid8\u7684chunk\u5927\u5c0f\u662f396\u5b57\u8282\uff0c\u8868\u793a\u6240\u6709252\u5230316\u5b57\u8282\u7684item\u90fd\u5e94\u8be5\u4fdd\u5b58\u5728id7\u4e2d\u3002\u540c\u7406\uff0c\u5728 1.1\u4e2d\uff0c\u4e5f\u53ef\u4ee5\u8ba1\u7b97\u5f97\u5230\u5b83\u51fa\u4e8e256\u548c512\u4e4b\u95f4\uff0c\u5e94\u8be5\u653e\u5728chunk_size\u4e3a512\u7684id9\u4e2d(32\u4f4d\u7cfb\u7edf\uff09\u3002<\/p>\n<p>Memcached \u521d\u59cb\u5316\u7684\u65f6\u5019\uff0c\u4f1a\u521d\u59cb\u5316slab\uff08\u524d\u9762\u53ef\u4ee5\u770b\u5230\uff0c\u5728main\u51fd\u6570\u4e2d\u8c03\u7528\u4e86slabs_init()\uff09\u3002\u5b83\u4f1a\u5728slabs_init()\u4e2d\u68c0\u67e5\u4e00\u4e2a\u5e38\u91cf DONT_PREALLOC_SLABS\uff0c\u5982\u679c\u8fd9\u4e2a\u6ca1\u6709\u88ab\u5b9a\u4e49\uff0c\u8bf4\u660e\u4f7f\u7528\u9884\u5206\u914d\u5185\u5b58\u65b9\u5f0f\u521d\u59cb\u5316slab\uff0c\u8fd9\u6837\u5728\u6240\u6709\u5df2\u7ecf\u5b9a\u4e49\u8fc7\u7684slabclass\u4e2d\uff0c\u6bcf\u4e00\u4e2aid\u521b\u5efa\u4e00\u4e2aslab\u3002\u8fd9\u6837\u5c31\u8868\u793a\uff0c1.2\u5728\u9ed8\u8ba4\u7684\u73af\u5883\u4e2d\u542f\u52a8\u8fdb\u7a0b\u540e\u8981\u5206\u914d41MB\u7684slab\u7a7a\u95f4\uff0c\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u91cc\uff0cmemcached\u7684\u7b2c\u4e8c\u4e2a\u5185\u5b58\u5197\u4f59\u53d1\u751f\u4e86\uff0c\u56e0\u4e3a\u6709\u53ef\u80fd\u4e00\u4e2aid\u6839\u672c\u6ca1\u6709\u88ab\u4f7f\u7528\u8fc7\uff0c\u4f46\u662f\u5b83\u4e5f\u9ed8\u8ba4\u7533\u8bf7\u4e86\u4e00\u4e2aslab\uff0c\u6bcf\u4e2aslab\u4f1a\u7528\u63891MB\u5185\u5b58<\/p>\n<p>\u5f53\u4e00\u4e2aslab\u7528\u5149\u540e\uff0c\u53c8\u6709\u65b0\u7684item\u8981\u63d2\u5165\u8fd9\u4e2aid\uff0c\u90a3\u4e48\u5b83\u5c31\u4f1a\u91cd\u65b0\u7533\u8bf7\u65b0\u7684slab\uff0c\u7533\u8bf7\u65b0\u7684slab\u65f6\uff0c\u5bf9\u5e94id\u7684slab\u94fe\u8868\u5c31\u8981\u589e\u957f\uff0c\u8fd9\u4e2a\u94fe\u8868\u662f\u6210\u500d\u589e\u957f\u7684\uff0c\u5728\u51fd\u6570grow_slab_list\u51fd\u6570\u4e2d\uff0c\u8fd9\u4e2a\u94fe\u7684\u957f\u5ea6\u4ece1\u53d8\u62102\uff0c\u4ece2\u53d8\u62104\uff0c\u4ece4\u53d8\u62108\u2026\u2026\uff1a<\/p>\n<p>CODE:<br \/>static int grow_slab_list (unsigned int id) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;slabclass_t *p = &#038;slabclass[id];<br \/>&nbsp;&nbsp;&nbsp;&nbsp;if (p->slabs == p->list_size) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;size_t new_size =&nbsp;&nbsp;p->list_size ? p->list_size * 2 : 16;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;void *new_list = realloc(p->slab_list, new_size*sizeof(void*));<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (new_list == 0) return 0;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p->list_size = new_size;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p->slab_list = new_list;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;return 1;<br \/>}<br \/>\u5728\u5b9a\u4f4ditem\u65f6\uff0c\u90fd\u662f\u4f7f\u7528slabs_clsid\u51fd\u6570\uff0c\u4f20\u5165\u53c2\u6570\u4e3aitem\u5927\u5c0f\uff0c\u8fd4\u56de\u503c\u4e3aclassid\uff0c\u7531\u8fd9\u4e2a\u8fc7\u7a0b\u53ef\u4ee5\u770b\u51fa\uff0cmemcached\u7684\u7b2c\u4e09\u4e2a\u5185\u5b58\u5197\u4f59\u53d1\u751f\u5728\u4fdd\u5b58item\u7684\u8fc7\u7a0b\u4e2d\uff0citem\u603b\u662f\u5c0f\u4e8e\u6216\u7b49\u4e8echunk\u5927\u5c0f\u7684\uff0c\u5f53item\u5c0f\u4e8echunk\u5927\u5c0f\u65f6\uff0c\u5c31\u53c8\u53d1\u751f\u4e86\u7a7a\u95f4\u6d6a\u8d39\u3002<\/p>\n<p>\u25ceMemcached\u7684NewHash\u7b97\u6cd5<\/p>\n<p>Memcached \u7684item\u4fdd\u5b58\u57fa\u4e8e\u4e00\u4e2a\u5927\u7684hash\u8868\uff0c\u5b83\u7684\u5b9e\u9645\u5730\u5740\u5c31\u662fslab\u4e2d\u7684chunk\u504f\u79fb\uff0c\u4f46\u662f\u5b83\u7684\u5b9a\u4f4d\u662f\u4f9d\u9760\u5bf9key\u505ahash\u7684\u7ed3\u679c\uff0c\u5728 primary_hashtable\u4e2d\u627e\u5230\u7684\u3002\u5728assoc.c\u548citems.c\u4e2d\u5b9a\u4e49\u4e86\u6240\u6709\u7684hash\u548citem\u64cd\u4f5c\u3002<\/p>\n<p>Memcached\u4f7f\u7528\u4e86\u4e00\u4e2a\u53eb\u505aNewHash\u7684\u7b97\u6cd5\uff0c\u5b83\u7684\u6548\u679c\u5f88\u597d\uff0c\u6548\u7387\u4e5f\u5f88\u9ad8\u30021.1\u548c1.2\u7684NewHash\u6709\u4e00\u4e9b\u4e0d\u540c\uff0c\u4e3b\u8981\u7684\u5b9e\u73b0\u65b9\u5f0f\u8fd8\u662f\u4e00\u6837\u7684\uff0c1.2\u7684hash\u51fd\u6570\u662f\u7ecf\u8fc7\u6574\u7406\u4f18\u5316\u7684\uff0c\u9002\u5e94\u6027\u66f4\u597d\u4e00\u4e9b\u3002<\/p>\n<p>NewHash\u7684\u539f\u578b\u53c2\u8003\uff1ahttp:\/\/burtleburtle.net\/bob\/hash\/evahash.html\u3002\u6570\u5b66\u5bb6\u603b\u662f\u6709\u70b9\u5947\u602a\uff0c\u5475\u5475\uff5e<\/p>\n<p>\u4e3a\u4e86\u53d8\u6362\u65b9\u4fbf\uff0c\u5b9a\u4e49\u4e86u4\u548cu1\u4e24\u79cd\u6570\u636e\u7c7b\u578b\uff0cu4\u5c31\u662f\u65e0\u7b26\u53f7\u7684\u957f\u6574\u5f62\uff0cu1\u5c31\u662f\u65e0\u7b26\u53f7char(0-255)\u3002<\/p>\n<p>\u5177\u4f53\u4ee3\u7801\u53ef\u4ee5\u53c2\u80031.1\u548c1.2\u6e90\u7801\u5305\u3002<\/p>\n<p>\u6ce8\u610f\u8fd9\u91cc\u7684hashtable\u957f\u5ea6\uff0c1.1\u548c1.2\u4e5f\u662f\u6709\u533a\u522b\u7684\uff0c1.1\u4e2d\u5b9a\u4e49\u4e86HASHPOWER\u5e38\u91cf\u4e3a20\uff0chashtable\u8868\u957f\u4e3a hashsize(HASHPOWER)\uff0c\u5c31\u662f4MB\uff08hashsize\u662f\u4e00\u4e2a\u5b8f\uff0c\u8868\u793a1\u53f3\u79fbn\u4f4d\uff09\uff0c1.2\u4e2d\u662f\u53d8\u91cf16\uff0c\u5373hashtable\u8868\u957f 65536\uff1a<\/p>\n<p>CODE:<br \/>typedef&nbsp;&nbsp;unsigned long&nbsp;&nbsp;int&nbsp;&nbsp;ub4;&nbsp;&nbsp; \/* unsigned 4-byte quantities *\/<br \/>typedef&nbsp;&nbsp;unsigned&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char ub1;&nbsp;&nbsp; \/* unsigned 1-byte quantities *\/<\/p>\n<p>#define hashsize(n) ((ub4)1<<(n))<br \/>#define hashmask(n) (hashsize(n)-1)<br \/>\u5728assoc_init()\u4e2d\uff0c\u4f1a\u5bf9primary_hashtable\u505a\u521d\u59cb\u5316\uff0c\u5bf9\u5e94\u7684hash\u64cd\u4f5c\u5305\u62ec\uff1aassoc_find()\u3001 assoc_expand()\u3001assoc_move_next_bucket()\u3001assoc_insert()\u3001assoc_delete()\uff0c\u5bf9\u5e94\u4e8eitem\u7684\u8bfb\u5199\u64cd\u4f5c\u3002\u5176\u4e2dassoc_find()\u662f\u6839\u636ekey\u548ckey\u957f\u5bfb\u627e\u5bf9\u5e94\u7684item\u5730\u5740\u7684\u51fd\u6570\uff08\u6ce8\u610f\u5728C\u4e2d\uff0c\u5f88\u591a\u65f6\u5019\u90fd\u662f\u540c\u65f6\u76f4\u63a5\u4f20\u5165\u5b57\u7b26\u4e32\u548c\u5b57\u7b26\u4e32\u957f\u5ea6\uff0c\u800c\u4e0d\u662f\u5728\u51fd\u6570\u5185\u90e8\u505astrlen\uff09\uff0c\u8fd4\u56de\u7684\u662fitem\u7ed3\u6784\u6307\u9488\uff0c\u5b83\u7684\u6570\u636e\u5730\u5740\u5728slab\u4e2d\u7684\u67d0\u4e2achunk\u4e0a\u3002<\/p>\n<p>items.c\u662f\u6570\u636e\u9879\u7684\u64cd\u4f5c\u7a0b\u5e8f\uff0c\u6bcf\u4e00\u4e2a\u5b8c\u6574\u7684item\u5305\u62ec\u51e0\u4e2a\u90e8\u5206\uff0c\u5728item_make_header()\u4e2d\u5b9a\u4e49\u4e3a\uff1a<\/p>\n<p>key\uff1a\u952e<br \/>nkey\uff1a\u952e\u957f<br \/>flags\uff1a\u7528\u6237\u5b9a\u4e49\u7684flag\uff08\u5176\u5b9e\u8fd9\u4e2aflag\u5728memcached\u4e2d\u6ca1\u6709\u542f\u7528\uff09<br \/>nbytes\uff1a\u503c\u957f\uff08\u5305\u62ec\u6362\u884c\u7b26\u53f7&#92;r&#92;n\uff09<br \/>suffix\uff1a\u540e\u7f00Buffer<br \/>nsuffix\uff1a\u540e\u7f00\u957f<\/p>\n<p>\u4e00\u4e2a\u5b8c\u6574\u7684item\u957f\u5ea6\u662f\u952e\u957f\uff0b\u503c\u957f\uff0b\u540e\u7f00\u957f\uff0bitem\u7ed3\u6784\u5927\u5c0f\uff0832\u5b57\u8282\uff09\uff0citem\u64cd\u4f5c\u5c31\u662f\u6839\u636e\u8fd9\u4e2a\u957f\u5ea6\u6765\u8ba1\u7b97slab\u7684classid\u7684\u3002<\/p>\n<p>hashtable \u4e2d\u7684\u6bcf\u4e00\u4e2a\u6876\u4e0a\u6302\u7740\u4e00\u4e2a\u53cc\u94fe\u8868\uff0citem_init()\u7684\u65f6\u5019\u5df2\u7ecf\u521d\u59cb\u5316\u4e86heads\u3001tails\u3001sizes\u4e09\u4e2a\u6570\u7ec4\u4e3a0\uff0c\u8fd9\u4e09\u4e2a\u6570\u7ec4\u7684\u5927\u5c0f\u90fd\u4e3a\u5e38\u91cf LARGEST_ID\uff08\u9ed8\u8ba4\u4e3a255\uff0c\u8fd9\u4e2a\u503c\u9700\u8981\u914d\u5408factor\u6765\u4fee\u6539\uff09\uff0c\u5728\u6bcf\u6b21item_assoc()\u7684\u65f6\u5019\uff0c\u5b83\u4f1a\u9996\u5148\u5c1d\u8bd5\u4eceslab\u4e2d\u83b7\u53d6\u4e00\u5757\u7a7a\u95f2\u7684chunk\uff0c\u5982\u679c\u6ca1\u6709\u53ef\u7528\u7684chunk\uff0c\u4f1a\u5728\u94fe\u8868\u4e2d\u626b\u63cf50\u6b21\uff0c\u4ee5\u5f97\u5230\u4e00\u4e2a\u88abLRU\u8e22\u6389\u7684item\uff0c\u5c06\u5b83unlink\uff0c\u7136\u540e\u5c06\u9700\u8981\u63d2\u5165\u7684item\u63d2\u5165\u94fe\u8868\u4e2d\u3002<\/p>\n<p>\u6ce8\u610fitem\u7684refcount\u6210\u5458\u3002item\u88abunlink\u4e4b\u540e\u53ea\u662f\u4ece\u94fe\u8868\u4e0a\u6458\u6389\uff0c\u4e0d\u662f\u7acb\u523b\u5c31\u88abfree\u7684\uff0c\u53ea\u662f\u5c06\u5b83\u653e\u5230\u5220\u9664\u961f\u5217\u4e2d\uff08item_unlink_q()\u51fd\u6570\uff09\u3002<\/p>\n<p>item\u5bf9\u5e94\u4e00\u4e9b\u8bfb\u5199\u64cd\u4f5c\uff0c\u5305\u62ecremove\u3001update\u3001replace\uff0c\u5f53\u7136\u6700\u91cd\u8981\u7684\u5c31\u662falloc\u64cd\u4f5c\u3002<\/p>\n<p>item \u8fd8\u6709\u4e00\u4e2a\u7279\u6027\u5c31\u662f\u5b83\u6709\u8fc7\u671f\u65f6\u95f4\uff0c\u8fd9\u662fmemcached\u7684\u4e00\u4e2a\u5f88\u6709\u7528\u7684\u7279\u6027\uff0c\u5f88\u591a\u5e94\u7528\u90fd\u662f\u4f9d\u8d56\u4e8ememcached\u7684item\u8fc7\u671f\uff0c\u6bd4\u5982session\u5b58\u50a8\u3001\u64cd\u4f5c\u9501\u7b49\u3002item_flush_expired()\u51fd\u6570\u5c31\u662f\u626b\u63cf\u8868\u4e2d\u7684item\uff0c\u5bf9\u8fc7\u671f\u7684item\u6267\u884cunlink\u64cd\u4f5c\uff0c\u5f53\u7136\u8fd9\u53ea\u662f\u4e00\u4e2a\u56de\u6536\u52a8\u4f5c\uff0c\u5b9e\u9645\u4e0a\u5728get\u7684\u65f6\u5019\u8fd8\u8981\u8fdb\u884c\u65f6\u95f4\u5224\u65ad\uff1a<\/p>\n<p>CODE:<br \/>\/* expires items that are more recent than the oldest_live setting. *\/<br \/>void item_flush_expired() {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;int i;&nbsp;&nbsp;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;item *iter, *next;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;if (! settings.oldest_live)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;for (i = 0; i < LARGEST_ID; i++) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/* The LRU is sorted in decreasing time order, and an item&#39;s timestamp<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * is never newer than its last access time, so we only need to walk<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * back until we hit an item older than the oldest_live time.<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * The oldest_live checking will auto-expire the remaining items.<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *\/<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (iter = heads<i>; iter != NULL; iter = next) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (iter->time >= settings.oldest_live) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next = iter->next;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ((iter->it_flags &#038; ITEM_SLABBED) == 0) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item_unlink(iter);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/* We&#39;ve hit the first old item. Continue to the next queue. *\/<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;&nbsp;&nbsp;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>}<\/p>\n<p>CODE:<br \/>\/* wrapper around assoc_find which does the lazy expiration\/deletion logic *\/<br \/>item *get_item_notedeleted(char *key, size_t nkey, int *delete_locked) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;item *it = assoc_find(key, nkey);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;if (delete_locked) *delete_locked = 0;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;if (it &#038;&#038; (it->it_flags &#038; ITEM_DELETED)) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/* it&#39;s flagged as delete-locked.&nbsp;&nbsp;let&#39;s see if that condition<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; is past due, and the 5-second delete_timer just hasn&#39;t<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gotten to it yet&#8230; *\/<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (! item_delete_lock_over(it)) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (delete_locked) *delete_locked = 1;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;it = 0;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;if (it &#038;&#038; settings.oldest_live &#038;&#038; settings.oldest_live <= current_time &#038;&#038;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;it->time <= settings.oldest_live) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item_unlink(it);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;it = 0;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;if (it &#038;&#038; it->exptime &#038;&#038; it->exptime <= current_time) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;item_unlink(it);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;it = 0;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;return it;<br \/>}<br \/>Memcached\u7684\u5185\u5b58\u7ba1\u7406\u65b9\u5f0f\u662f\u975e\u5e38\u7cbe\u5de7\u548c\u9ad8\u6548\u7684\uff0c\u5b83\u5f88\u5927\u7a0b\u5ea6\u4e0a\u51cf\u5c11\u4e86\u76f4\u63a5alloc\u7cfb\u7edf\u5185\u5b58\u7684\u6b21\u6570\uff0c\u964d\u4f4e\u51fd\u6570\u5f00\u9500\u548c\u5185\u5b58\u788e\u7247\u4ea7\u751f\u51e0\u7387\uff0c\u867d\u7136\u8fd9\u79cd\u65b9\u5f0f\u4f1a\u9020\u6210\u4e00\u4e9b\u5197\u4f59\u6d6a\u8d39\uff0c\u4f46\u662f\u8fd9\u79cd\u6d6a\u8d39\u5728\u5927\u578b\u7cfb\u7edf\u5e94\u7528\u4e2d\u662f\u5fae\u4e0d\u8db3\u9053\u7684\u3002<\/p>\n<p>\u25ceMemcached\u7684\u7406\u8bba\u53c2\u6570\u8ba1\u7b97\u65b9\u5f0f<\/p>\n<p>\u5f71\u54cd memcached \u5de5\u4f5c\u7684\u51e0\u4e2a\u53c2\u6570\u6709\uff1a<\/p>\n<p>\u5e38\u91cfREALTIME_MAXDELTA 60*60*24*30<br \/>\u6700\u592730\u5929\u7684\u8fc7\u671f\u65f6\u95f4<\/p>\n<p>conn_init()\u4e2d\u7684freetotal\uff08=200\uff09<br \/>\u6700\u5927\u540c\u65f6\u8fde\u63a5\u6570<\/p>\n<p>\u5e38\u91cfKEY_MAX_LENGTH 250<br \/>\u6700\u5927\u952e\u957f<\/p>\n<p>settings.factor\uff08=1.25\uff09<br \/>factor\u5c06\u5f71\u54cdchunk\u7684\u6b65\u8fdb\u5927\u5c0f<\/p>\n<p>settings.maxconns\uff08=1024\uff09<br \/>\u6700\u5927\u8f6f\u8fde\u63a5<\/p>\n<p>settings.chunk_size\uff08=48\uff09<br \/>\u4e00\u4e2a\u4fdd\u5b88\u4f30\u8ba1\u7684key+value\u957f\u5ea6\uff0c\u7528\u6765\u751f\u6210id1\u4e2d\u7684chunk\u957f\u5ea6\uff081.2\uff09\u3002id1\u7684chunk\u957f\u5ea6\u7b49\u4e8e\u8fd9\u4e2a\u6570\u503c\u52a0\u4e0aitem\u7ed3\u6784\u4f53\u7684\u957f\u5ea6\uff0832\uff09\uff0c\u5373\u9ed8\u8ba4\u768480\u5b57\u8282\u3002<\/p>\n<p>\u5e38\u91cfPOWER_SMALLEST 1<br \/>\u6700\u5c0fclassid\uff081.2\uff09<\/p>\n<p>\u5e38\u91cfPOWER_LARGEST 200<br \/>\u6700\u5927classid\uff081.2\uff09<\/p>\n<p>\u5e38\u91cfPOWER_BLOCK 1048576<br \/>\u9ed8\u8ba4slab\u5927\u5c0f<\/p>\n<p>\u5e38\u91cfCHUNK_ALIGN_BYTES (sizeof(void *))<br \/>\u4fdd\u8bc1chunk\u5927\u5c0f\u662f\u8fd9\u4e2a\u6570\u503c\u7684\u6574\u6570\u500d\uff0c\u9632\u6b62\u8d8a\u754c\uff08void *\u7684\u957f\u5ea6\u5728\u4e0d\u540c\u7cfb\u7edf\u4e0a\u4e0d\u4e00\u6837\uff0c\u5728\u6807\u51c632\u4f4d\u7cfb\u7edf\u4e0a\u662f4\uff09<\/p>\n<p>\u5e38\u91cfITEM_UPDATE_INTERVAL 60<br \/>\u961f\u5217\u5237\u65b0\u95f4\u9694<\/p>\n<p>\u5e38\u91cfLARGEST_ID 255<br \/>\u6700\u5927item\u94fe\u8868\u6570\uff08\u8fd9\u4e2a\u503c\u4e0d\u80fd\u6bd4\u6700\u5927\u7684classid\u5c0f\uff09<\/p>\n<p>\u53d8\u91cfhashpower\uff08\u57281.1\u4e2d\u662f\u5e38\u91cfHASHPOWER\uff09<br \/>\u51b3\u5b9ahashtable\u7684\u5927\u5c0f<\/p>\n<p>\u6839\u636e\u4e0a\u9762\u4ecb\u7ecd\u7684\u5185\u5bb9\u53ca\u53c2\u6570\u8bbe\u5b9a\uff0c\u53ef\u4ee5\u8ba1\u7b97\u51fa\u7684\u4e00\u4e9b\u7ed3\u679c\uff1a<\/p>\n<p>1\u3001\u5728memcached\u4e2d\u53ef\u4ee5\u4fdd\u5b58\u7684item\u4e2a\u6570\u662f\u6ca1\u6709\u8f6f\u4ef6\u4e0a\u9650\u7684\uff0c\u4e4b\u524d\u6211\u7684100\u4e07\u7684\u8bf4\u6cd5\u662f\u9519\u8bef\u7684\u3002<br \/>2\u3001\u5047\u8bbeNewHash\u7b97\u6cd5\u78b0\u649e\u5747\u5300\uff0c\u67e5\u627eitem\u7684\u5faa\u73af\u6b21\u6570\u662fitem\u603b\u6570\u9664\u4ee5hashtable\u5927\u5c0f\uff08\u7531hashpower\u51b3\u5b9a\uff09\uff0c\u662f\u7ebf\u6027\u7684\u3002<br \/>3\u3001Memcached\u9650\u5236\u4e86\u53ef\u4ee5\u63a5\u53d7\u7684\u6700\u5927item\u662f1MB\uff0c\u5927\u4e8e1MB\u7684\u6570\u636e\u4e0d\u4e88\u7406\u4f1a\u3002<br \/>4\u3001Memcached\u7684\u7a7a\u95f4\u5229\u7528\u7387\u548c\u6570\u636e\u7279\u6027\u6709\u5f88\u5927\u7684\u5173\u7cfb\uff0c\u53c8\u4e0eDONT_PREALLOC_SLABS\u5e38\u91cf\u6709\u5173\u3002 \u5728\u6700\u5dee\u60c5\u51b5\u4e0b\uff0c\u6709198\u4e2aslab\u4f1a\u88ab\u6d6a\u8d39\uff08\u6240\u6709item\u90fd\u96c6\u4e2d\u5728\u4e00\u4e2aslab\u4e2d\uff0c199\u4e2aid\u5168\u90e8\u5206\u914d\u6ee1\uff09\u3002<\/p>\n<p>\u25ceMemcached\u7684\u5b9a\u957f\u4f18\u5316<\/p>\n<p>\u6839\u636e\u4e0a\u9762\u51e0\u8282\u7684\u63cf\u8ff0\uff0c\u591a\u5c11\u5bf9memcached\u6709\u4e86\u4e00\u4e2a\u6bd4\u8f83\u6df1\u5165\u7684\u8ba4\u8bc6\u3002\u5728\u6df1\u5165\u8ba4\u8bc6\u7684\u57fa\u7840\u4e0a\u624d\u597d\u5bf9\u5b83\u8fdb\u884c\u4f18\u5316\u3002<\/p>\n<p>Memcached \u672c\u8eab\u662f\u4e3a\u53d8\u957f\u6570\u636e\u8bbe\u8ba1\u7684\uff0c\u6839\u636e\u6570\u636e\u7279\u6027\uff0c\u53ef\u4ee5\u8bf4\u5b83\u662f\u201c\u9762\u5411\u5927\u4f17\u201d\u7684\u8bbe\u8ba1\uff0c\u4f46\u662f\u5f88\u591a\u65f6\u5019\uff0c\u6211\u4eec\u7684\u6570\u636e\u5e76\u4e0d\u662f\u8fd9\u6837\u7684\u201c\u666e\u904d\u201d\uff0c\u5178\u578b\u7684\u60c5\u51b5\u4e2d\uff0c\u4e00\u79cd\u662f\u975e\u5747\u5300\u5206\u5e03\uff0c\u5373\u6570\u636e\u957f\u5ea6\u96c6\u4e2d\u5728\u51e0\u4e2a\u533a\u57df\u5185\uff08\u5982\u4fdd\u5b58\u7528\u6237 Session\uff09\uff1b\u53e6\u4e00\u79cd\u66f4\u6781\u7aef\u7684\u72b6\u6001\u662f\u7b49\u957f\u6570\u636e\uff08\u5982\u5b9a\u957f\u952e\u503c\uff0c\u5b9a\u957f\u6570\u636e\uff0c\u591a\u89c1\u4e8e\u8bbf\u95ee\u3001\u5728\u7ebf\u7edf\u8ba1\u6216\u6267\u884c\u9501\uff09\u3002<\/p>\n<p>\u8fd9\u91cc\u4e3b\u8981\u7814\u7a76\u4e00\u4e0b\u5b9a\u957f\u6570\u636e\u7684\u4f18\u5316\u65b9\u6848\uff081.2\uff09\uff0c\u96c6\u4e2d\u5206\u5e03\u7684\u53d8\u957f\u6570\u636e\u4ec5\u4f9b\u53c2\u8003\uff0c\u5b9e\u73b0\u8d77\u6765\u4e5f\u5f88\u5bb9\u6613\u3002<\/p>\n<p>\u89e3\u51b3\u5b9a\u957f\u6570\u636e\uff0c\u9996\u5148\u9700\u8981\u89e3\u51b3\u7684\u662fslab\u7684\u5206\u914d\u95ee\u9898\uff0c\u7b2c\u4e00\u4e2a\u9700\u8981\u786e\u8ba4\u7684\u662f\u6211\u4eec\u4e0d\u9700\u8981\u90a3\u4e48\u591a\u4e0d\u540cchunk\u957f\u5ea6\u7684slab\uff0c\u4e3a\u4e86\u6700\u5927\u9650\u5ea6\u5730\u5229\u7528\u8d44\u6e90\uff0c\u6700\u597dchunk\u548citem\u7b49\u957f\uff0c\u6240\u4ee5\u9996\u5148\u8981\u8ba1\u7b97item\u957f\u5ea6\u3002<\/p>\n<p>\u5728\u4e4b\u524d\u5df2\u7ecf\u6709\u4e86\u8ba1\u7b97item\u957f\u5ea6\u7684\u7b97\u6cd5\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u9664\u4e86\u5b57\u7b26\u4e32\u957f\u5ea6\u5916\uff0c\u8fd8\u8981\u52a0\u4e0aitem\u7ed3\u6784\u7684\u957f\u5ea632\u5b57\u8282\u3002<\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u5df2\u7ecf\u8ba1\u7b97\u51fa\u9700\u8981\u4fdd\u5b58200\u5b57\u8282\u7684\u7b49\u957f\u6570\u636e\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u662f\u8981\u4fee\u6539slab\u7684classid\u548cchunk\u957f\u5ea6\u7684\u5173\u7cfb\u3002\u5728\u539f\u59cb\u7248\u672c\u4e2d\uff0cchunk\u957f\u5ea6\u548cclassid\u662f\u6709\u5bf9\u5e94\u5173\u7cfb\u7684\uff0c\u73b0\u5728\u5982\u679c\u628a\u6240\u6709\u7684 chunk\u90fd\u5b9a\u4e3a200\u4e2a\u5b57\u8282\uff0c\u90a3\u4e48\u8fd9\u4e2a\u5173\u7cfb\u5c31\u4e0d\u5b58\u5728\u4e86\uff0c\u6211\u4eec\u9700\u8981\u91cd\u65b0\u786e\u5b9a\u8fd9\u4e8c\u8005\u7684\u5173\u7cfb\u3002\u4e00\u79cd\u65b9\u6cd5\u662f\uff0c\u6574\u4e2a\u5b58\u50a8\u7ed3\u6784\u53ea\u4f7f\u7528\u4e00\u4e2a\u56fa\u5b9a\u7684id\uff0c\u5373\u53ea\u4f7f\u7528199 \u4e2a\u69fd\u4e2d\u76841\u4e2a\uff0c\u5728\u8fd9\u79cd\u6761\u4ef6\u4e0b\uff0c\u5c31\u4e00\u5b9a\u8981\u5b9a\u4e49DONT_PREALLOC_SLABS\u6765\u907f\u514d\u53e6\u5916\u7684\u9884\u5206\u914d\u6d6a\u8d39\u3002\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u5efa\u7acb\u4e00\u4e2ahash\u5173\u7cfb\uff0c\u6765\u4ece item\u786e\u5b9aclassid\uff0c\u4e0d\u80fd\u4f7f\u7528\u957f\u5ea6\u6765\u505a\u952e\uff0c\u53ef\u4ee5\u4f7f\u7528key\u7684NewHash\u7ed3\u679c\u7b49\u4e0d\u5b9a\u6570\u636e\uff0c\u6216\u8005\u76f4\u63a5\u6839\u636ekey\u6765\u505ahash\uff08\u5b9a\u957f\u6570\u636e\u7684key\u4e5f\u4e00\u5b9a\u7b49\u957f\uff09\u3002\u8fd9\u91cc\u7b80\u5355\u8d77\u89c1\uff0c\u9009\u62e9\u7b2c\u4e00\u79cd\u65b9\u6cd5\uff0c\u8fd9\u79cd\u65b9\u6cd5\u7684\u4e0d\u8db3\u4e4b\u5904\u5728\u4e8e\u53ea\u4f7f\u7528\u4e00\u4e2aid\uff0c\u5728\u6570\u636e\u91cf\u975e\u5e38\u5927\u7684\u60c5\u51b5\u4e0b\uff0cslab\u94fe\u4f1a\u5f88\u957f\uff08\u56e0\u4e3a\u6240\u6709\u6570\u636e\u90fd\u6324\u5728\u4e00\u6761\u94fe\u4e0a\u4e86\uff09\uff0c\u904d\u5386\u8d77\u6765\u7684\u4ee3\u4ef7\u6bd4\u8f83\u9ad8\u3002<\/p>\n<p>\u524d\u9762\u4ecb\u7ecd\u4e86\u4e09\u79cd\u7a7a\u95f4\u5197\u4f59\uff0c\u8bbe\u7f6echunk\u957f\u5ea6\u7b49\u4e8eitem\u957f\u5ea6\uff0c\u89e3\u51b3\u4e86\u7b2c\u4e00\u79cd\u7a7a\u95f4\u6d6a\u8d39\u95ee\u9898\uff0c\u4e0d\u9884\u7533\u8bf7\u7a7a\u95f4\u89e3\u51b3\u4e86\u7b2c\u4e8c\u79cd\u7a7a\u95f4\u6d6a\u8d39\u95ee\u9898\uff0c\u90a3\u4e48\u5bf9\u4e8e\u7b2c\u4e00\u79cd\u95ee\u9898\uff08slab\u5185\u5269\u4f59\uff09\u5982\u4f55\u89e3\u51b3\u5462\uff0c\u8fd9\u5c31\u9700\u8981\u4fee\u6539POWER_BLOCK\u5e38\u91cf\uff0c\u4f7f\u5f97\u6bcf\u4e00\u4e2aslab\u5927\u5c0f\u6b63\u597d\u7b49\u4e8echunk\u957f\u5ea6\u7684\u6574\u6570\u500d\uff0c\u8fd9\u6837\u4e00\u4e2aslab\u5c31\u53ef\u4ee5\u6b63\u597d\u5212\u5206\u6210n\u4e2achunk\u3002\u8fd9\u4e2a\u6570\u503c\u5e94\u8be5\u6bd4\u8f83\u63a5\u8fd11MB\uff0c\u8fc7\u5927\u7684\u8bdd\u540c\u6837\u4f1a\u9020\u6210\u5197\u4f59\uff0c\u8fc7\u5c0f\u7684\u8bdd\u4f1a\u9020\u6210\u6b21\u6570\u8fc7\u591a\u7684alloc\uff0c\u6839\u636echunk\u957f\u5ea6\u4e3a200\uff0c\u9009\u62e91000000\u4f5c\u4e3aPOWER_BLOCK\u7684\u503c\uff0c\u8fd9\u6837\u4e00\u4e2aslab\u5c31\u662f100\u4e07\u5b57\u8282\uff0c\u4e0d\u662f 1048576\u3002\u4e09\u4e2a\u5197\u4f59\u95ee\u9898\u90fd\u89e3\u51b3\u4e86\uff0c\u7a7a\u95f4\u5229\u7528\u7387\u4f1a\u5927\u5927\u63d0\u5347\u3002<\/p>\n<p>\u4fee\u6539 slabs_clsid \u51fd\u6570\uff0c\u8ba9\u5b83\u76f4\u63a5\u8fd4\u56de\u4e00\u4e2a\u5b9a\u503c\uff08\u6bd4\u5982 1 \uff09\uff1a<\/p>\n<p>CODE:<br \/>unsigned int slabs_clsid(size_t size) {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 1;<br \/>}<br \/>\u4fee\u6539slabs_init\u51fd\u6570\uff0c\u53bb\u6389\u5faa\u73af\u521b\u5efa\u6240\u6709classid\u5c5e\u6027\u7684\u90e8\u5206\uff0c\u76f4\u63a5\u6dfb\u52a0slabclass[1]\uff1a<\/p>\n<p>CODE:<br \/>slabclass[1].size = 200;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/\u6bcfchunk200\u5b57\u8282<br \/>slabclass[1].perslab = 5000;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/1000000\/200<br \/>\u25ceMemcached\u5ba2\u6237\u7aef<\/p>\n<p>Memcached\u662f\u4e00\u4e2a\u670d\u52a1\u7a0b\u5e8f\uff0c\u4f7f\u7528\u7684\u65f6\u5019\u53ef\u4ee5\u6839\u636e\u5b83\u7684\u534f\u8bae\uff0c\u8fde\u63a5\u5230 memcached\u670d\u52a1\u5668\u4e0a\uff0c\u53d1\u9001\u547d\u4ee4\u7ed9\u670d\u52a1\u8fdb\u7a0b\uff0c\u5c31\u53ef\u4ee5\u64cd\u4f5c\u4e0a\u9762\u7684\u6570\u636e\u3002\u4e3a\u4e86\u65b9\u4fbf\u4f7f\u7528\uff0cmemcached\u6709\u5f88\u591a\u4e2a\u5ba2\u6237\u7aef\u7a0b\u5e8f\u53ef\u4ee5\u4f7f\u7528\uff0c\u5bf9\u5e94\u4e8e\u5404\u79cd\u8bed\u8a00\uff0c\u6709\u5404\u79cd\u8bed\u8a00\u7684\u5ba2\u6237\u7aef\u3002\u57fa\u4e8eC\u8bed\u8a00\u7684\u6709libmemcache\u3001APR_Memcache\uff1b\u57fa\u4e8ePerl\u7684\u6709Cache::Memcached\uff1b\u53e6\u5916\u8fd8\u6709Python\u3001Ruby\u3001Java\u3001C#\u7b49\u8bed\u8a00\u7684\u652f\u6301\u3002PHP\u7684\u5ba2\u6237\u7aef\u662f\u6700\u591a\u7684\uff0c\u4e0d\u5149\u6709mcache\u548cPECL memcache\u4e24\u4e2a\u6269\u5c55\uff0c\u8fd8\u6709\u5927\u628a\u7684\u7531PHP\u7f16\u5199\u7684\u5c01\u88c5\u7c7b\uff0c\u4e0b\u9762\u4ecb\u7ecd\u4e00\u4e0b\u5728PHP\u4e2d\u4f7f\u7528memcached\u7684\u65b9\u6cd5\uff1a<\/p>\n<p>mcache\u6269\u5c55\u662f\u57fa\u4e8elibmemcache\u518d\u5c01\u88c5\u7684libmemcache\u4e00\u76f4\u6ca1\u6709\u53d1\u5e03stable\u7248\u672c\uff0c\u76ee\u524d\u7248\u672c\u662f1.4.0-rc2\uff0c\u53ef\u4ee5\u5728\u8fd9\u91cc\u627e\u5230\u3002 libmemcache\u6709\u4e00\u4e2a\u5f88\u4e0d\u597d\u7684\u7279\u6027\uff0c\u5c31\u662f\u4f1a\u5411stderr\u5199\u5f88\u591a\u9519\u8bef\u4fe1\u606f\uff0c\u4e00\u822c\u7684\uff0c\u4f5c\u4e3alib\u4f7f\u7528\u7684\u65f6\u5019\uff0cstderr\u4e00\u822c\u90fd\u4f1a\u88ab\u5b9a\u5411\u5230\u5176\u5b83\u5730\u65b9\uff0c\u6bd4\u5982Apache\u7684\u9519\u8bef\u65e5\u5fd7\uff0c\u800c\u4e14libmemcache\u4f1a\u81ea\u6740\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5f02\u5e38\uff0c\u4e0d\u8fc7\u5b83\u7684\u6027\u80fd\u8fd8\u662f\u5f88\u597d\u7684\u3002<\/p>\n<p>mcache\u6269\u5c55\u6700\u540e\u66f4\u65b0\u52301.2.0-beta10\uff0c\u4f5c\u8005\u5927\u6982\u662f\u79bb\u804c\u4e86\uff0c\u4e0d\u5149\u505c\u6b62\u66f4\u65b0\uff0c\u8fde\u7f51\u7ad9\u4e5f\u6253\u4e0d\u5f00\u4e86\uff08~_~\uff09\uff0c\u53ea\u80fd\u5230\u5176\u5b83\u5730\u65b9\u53bb\u83b7\u53d6\u8fd9\u4e2a\u4e0d\u8d1f\u8d23\u7684\u6269\u5c55\u4e86\u3002\u89e3\u538b\u540e\u5b89\u88c5\u65b9\u6cd5\u5982\u5e38\uff1aphpize &#038; configure &#038; make &#038; make install\uff0c\u4e00\u5b9a\u8981\u5148\u5b89\u88c5libmemcache\u3002\u4f7f\u7528\u8fd9\u4e2a\u6269\u5c55\u5f88\u7b80\u5355\uff1a<\/p>\n<p>CODE:<br \/><?php<br \/>$mc = memcache();&nbsp;&nbsp;&nbsp;&nbsp;\/\/ \u521b\u5efa\u4e00\u4e2amemcache\u8fde\u63a5\u5bf9\u8c61\uff0c\u6ce8\u610f\u8fd9\u91cc\u4e0d\u662f\u7528new\uff01<br \/>$mc->add_server(&#39;localhost&#39;, 11211);&nbsp;&nbsp;&nbsp;&nbsp;\/\/ \u6dfb\u52a0\u4e00\u4e2a\u670d\u52a1\u8fdb\u7a0b<br \/>$mc->add_server(&#39;localhost&#39;, 11212);&nbsp;&nbsp;&nbsp;&nbsp;\/\/ \u6dfb\u52a0\u7b2c\u4e8c\u4e2a\u670d\u52a1\u8fdb\u7a0b<br \/>$mc->set(&#39;key1&#39;, &#39;Hello&#39;);&nbsp;&nbsp;&nbsp;&nbsp;\/\/ \u5199\u5165key1 => Hello<br \/>$mc->set(&#39;key2&#39;, &#39;World&#39;, 10);&nbsp;&nbsp;&nbsp;&nbsp;\/\/ \u5199\u5165key2 => World\uff0c10\u79d2\u8fc7\u671f<br \/>$mc->set(&#39;arr1&#39;, array(&#39;Hello&#39;, &#39;World&#39;));&nbsp;&nbsp;&nbsp;&nbsp;\/\/ \u5199\u5165\u4e00\u4e2a\u6570\u7ec4<br \/>$key1 = $mc->get(&#39;key1&#39;);&nbsp;&nbsp;&nbsp;&nbsp;\/\/ \u83b7\u53d6&#39;key1&#39;\u7684\u503c\uff0c\u8d4b\u7ed9$key1<br \/>$key2 = $mc->get(&#39;key2&#39;);&nbsp;&nbsp;&nbsp;&nbsp;\/\/ \u83b7\u53d6&#39;key2&#39;\u7684\u503c\uff0c\u8d4b\u7ed9$key2\uff0c\u5982\u679c\u8d85\u8fc710\u79d2\uff0c\u5c31\u53d6\u4e0d\u5230\u4e86<br \/>$arr1 = $mc->get(&#39;arr1&#39;);&nbsp;&nbsp;&nbsp;&nbsp;\/\/ \u83b7\u53d6&#39;arr1&#39;\u6570\u7ec4<br \/>$mc->delete(&#39;arr1&#39;);&nbsp;&nbsp;&nbsp;&nbsp;\/\/ \u5220\u9664&#39;arr1&#39;<br \/>$mc->flush_all();&nbsp;&nbsp;&nbsp;&nbsp;\/\/ \u5220\u6389\u6240\u6709\u6570\u636e<br \/>$stats = $mc->stats();&nbsp;&nbsp;&nbsp;&nbsp;\/\/ \u83b7\u53d6\u670d\u52a1\u5668\u4fe1\u606f<br \/>var_dump($stats);&nbsp;&nbsp;&nbsp;&nbsp;\/\/ \u670d\u52a1\u5668\u4fe1\u606f\u662f\u4e00\u4e2a\u6570\u7ec4<br \/>?><br \/>\u8fd9\u4e2a\u6269\u5c55\u7684\u597d\u5904\u662f\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5b9e\u73b0\u5206\u5e03\u5f0f\u5b58\u50a8\u548c\u8d1f\u8f7d\u5747\u8861\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u6dfb\u52a0\u591a\u4e2a\u670d\u52a1\u5730\u5740\uff0c\u6570\u636e\u5728\u4fdd\u5b58\u7684\u65f6\u5019\u662f\u4f1a\u6839\u636ehash\u7ed3\u679c\u5b9a\u4f4d\u5230\u67d0\u53f0\u670d\u52a1\u5668\u4e0a\u7684\uff0c\u8fd9\u4e5f\u662flibmemcache\u7684\u7279\u6027\u3002libmemcache\u652f\u6301\u96c6\u4e2dhash\u65b9\u5f0f\uff0c\u5305\u62ecCRC32\u3001ELF\u548cPerl hash\u3002<\/p>\n<p>PECL memcache\u662fPECL\u53d1\u5e03\u7684\u6269\u5c55\uff0c\u76ee\u524d\u6700\u65b0\u7248\u672c\u662f2.1.0\uff0c\u53ef\u4ee5\u5728pecl\u7f51\u7ad9\u5f97\u5230\u3002memcache\u6269\u5c55\u7684\u4f7f\u7528\u65b9\u6cd5\u53ef\u4ee5\u5728\u65b0\u4e00\u4e9b\u7684PHP\u624b\u518c\u4e2d\u627e\u5230\uff0c\u5b83\u548cmcache\u5f88\u50cf\uff0c\u771f\u7684\u5f88\u50cf\uff1a<\/p>\n<p>CODE:<br \/><?php\n\n$memcache = new Memcache;<br \/>$memcache->connect(&#39;localhost&#39;, 11211) or die (&#8220;Could not connect&#8221;);<\/p>\n<p>$version = $memcache->getVersion();<br \/>echo &#8220;Server&#39;s version: &#8220;.$version.&#8221;n&#8221;;<\/p>\n<p>$tmp_object = new stdClass;<br \/>$tmp_object->str_attr = &#39;test&#39;;<br \/>$tmp_object->int_attr = 123;<\/p>\n<p>$memcache->set(&#39;key&#39;, $tmp_object, false, 10) or die (&#8220;Failed to save data at the server&#8221;);<br \/>echo &#8220;Store data in the cache (data will expire in 10 seconds)n&#8221;;<\/p>\n<p>$get_result = $memcache->get(&#39;key&#39;);<br \/>echo &#8220;Data from the cache:n&#8221;;<\/p>\n<p>var_dump($get_result);<\/p>\n<p>?><br \/>\u8fd9\u4e2a\u6269\u5c55\u662f\u4f7f\u7528php\u7684stream\u76f4\u63a5\u8fde\u63a5memcached\u670d\u52a1\u5668\u5e76\u901a\u8fc7socket\u53d1\u9001\u547d\u4ee4\u7684\u3002\u5b83\u4e0d\u50cflibmemcache\u90a3\u6837\u5b8c\u5584\uff0c\u4e5f\u4e0d\u652f\u6301 add_server\u8fd9\u79cd\u5206\u5e03\u64cd\u4f5c\uff0c\u4f46\u662f\u56e0\u4e3a\u5b83\u4e0d\u4f9d\u8d56\u5176\u5b83\u7684\u5916\u754c\u7a0b\u5e8f\uff0c\u517c\u5bb9\u6027\u8981\u597d\u4e00\u4e9b\uff0c\u4e5f\u6bd4\u8f83\u7a33\u5b9a\u3002\u81f3\u4e8e\u6548\u7387\uff0c\u5dee\u522b\u4e0d\u662f\u5f88\u5927\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u6709\u5f88\u591a\u7684PHP class\u53ef\u4ee5\u4f7f\u7528\uff0c\u6bd4\u5982MemcacheClient.inc.php\uff0cphpclasses.org\u4e0a\u53ef\u4ee5\u627e\u5230\u5f88\u591a\uff0c\u4e00\u822c\u90fd\u662f\u5bf9perl client API\u7684\u518d\u5c01\u88c5\uff0c\u4f7f\u7528\u65b9\u5f0f\u5f88\u50cf\u3002<\/p>\n<p>\u25ceBSM_Memcache<\/p>\n<p>\u4ece C client\u6765\u8bf4\uff0cAPR_Memcache\u662f\u4e00\u4e2a\u5f88\u6210\u719f\u5f88\u7a33\u5b9a\u7684client\u7a0b\u5e8f\uff0c\u652f\u6301\u7ebf\u7a0b\u9501\u548c\u539f\u5b50\u7ea7\u64cd\u4f5c\uff0c\u4fdd\u8bc1\u8fd0\u884c\u7684\u7a33\u5b9a\u6027\u3002\u4e0d\u8fc7\u5b83\u662f\u57fa\u4e8eAPR\u7684\uff08APR\u5c06\u5728\u6700\u540e\u4e00\u8282\u4ecb\u7ecd\uff09\uff0c\u6ca1\u6709libmemcache\u7684\u5e94\u7528\u8303\u56f4\u5e7f\uff0c\u76ee\u524d\u4e5f\u6ca1\u6709\u5f88\u591a\u57fa\u4e8e\u5b83\u5f00\u53d1\u7684\u7a0b\u5e8f\uff0c\u73b0\u6709\u7684\u591a\u662f\u4e00\u4e9bApache Module\uff0c\u56e0\u4e3a\u5b83\u4e0d\u80fd\u8131\u79bbAPR\u73af\u5883\u8fd0\u884c\u3002\u4f46\u662fAPR\u5012\u662f\u53ef\u4ee5\u8131\u79bbApache\u5355\u72ec\u5b89\u88c5\u7684\uff0c\u5728APR\u7f51\u7ad9\u4e0a\u53ef\u4ee5\u4e0b\u8f7dAPR\u548cAPR-util\uff0c\u4e0d\u9700\u8981\u6709Apache\uff0c\u53ef\u4ee5\u76f4\u63a5\u5b89\u88c5\uff0c\u800c\u4e14\u5b83\u662f\u8de8\u5e73\u53f0\u7684\u3002<\/p>\n<p>BSM_Memcache\u662f\u6211\u5728BS.Magic\u9879\u76ee\u4e2d\u5f00\u53d1\u7684\u4e00\u4e2a\u57fa\u4e8eAPR_Memcache\u7684PHP\u6269\u5c55\uff0c\u8bf4\u8d77\u6765\u6709\u70b9\u62d7\u53e3\uff0c\u81f3\u5c11\u5b83\u628aAPR\u626f\u8fdb\u4e86PHP\u6269\u5c55\u4e2d\u3002\u8fd9\u4e2a\u7a0b\u5e8f\u5f88\u7b80\u5355\uff0c\u4e5f\u6ca1\u505a\u592a\u591a\u7684\u529f\u80fd\uff0c\u53ea\u662f\u4e00\u79cd\u5f62\u5f0f\u7684\u5c1d\u8bd5\uff0c\u5b83\u652f\u6301\u670d\u52a1\u5668\u5206\u7ec4\u3002<\/p>\n<p>\u548c mcache\u6269\u5c55\u652f\u6301\u591a\u670d\u52a1\u5668\u5206\u5e03\u5b58\u50a8\u4e0d\u540c\uff0cBSM_Memcache\u652f\u6301\u591a\u7ec4\u670d\u52a1\u5668\uff0c\u6bcf\u4e00\u7ec4\u5185\u7684\u670d\u52a1\u5668\u8fd8\u662f\u6309\u7167hash\u65b9\u5f0f\u6765\u5206\u5e03\u4fdd\u5b58\u6570\u636e\uff0c\u4f46\u662f\u4e24\u4e2a\u7ec4\u4e2d\u4fdd\u5b58\u7684\u6570\u636e\u662f\u4e00\u6837\u7684\uff0c\u4e5f\u5c31\u662f\u5b9e\u73b0\u4e86\u70ed\u5907\uff0c\u5b83\u4e0d\u4f1a\u56e0\u4e3a\u4e00\u53f0\u670d\u52a1\u5668\u53d1\u751f\u5355\u70b9\u6545\u969c\u5bfc\u81f4\u6570\u636e\u65e0\u6cd5\u83b7\u53d6\uff0c\u9664\u975e\u6240\u6709\u7684\u670d\u52a1\u5668\u7ec4\u90fd\u635f\u574f\uff08\u4f8b\u5982\u673a\u623f\u505c\u7535\uff09\u3002\u5f53\u7136\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\u7684\u4ee3\u4ef7\u5c31\u662f\u6027\u80fd\u4e0a\u7684\u727a\u7272\uff0c\u5728\u6bcf\u6b21\u6dfb\u52a0\u5220\u9664\u6570\u636e\u7684\u65f6\u5019\u90fd\u8981\u626b\u63cf\u6240\u6709\u7684\u7ec4\uff0c\u5728get\u6570\u636e\u7684\u65f6\u5019\u4f1a\u968f\u673a\u9009\u62e9\u4e00\u7ec4\u670d\u52a1\u5668\u5f00\u59cb\u8f6e\u8be2\uff0c\u4e00\u76f4\u5230\u627e\u5230\u6570\u636e\u4e3a\u6b62\uff0c\u6b63\u5e38\u60c5\u51b5\u4e0b\u4e00\u6b21\u5c31\u53ef\u4ee5\u83b7\u53d6\u5f97\u5230\u3002<\/p>\n<p>BSM_Memcache\u53ea\u652f\u6301\u8fd9\u51e0\u4e2a\u51fd\u6570\uff1a<\/p>\n<p>CODE:<br \/>zend_function_entry bsm_memcache_functions[] =<br \/>{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;PHP_FE(mc_get,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NULL)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;PHP_FE(mc_set,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NULL)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;PHP_FE(mc_del,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NULL)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;PHP_FE(mc_add_group,&nbsp;&nbsp;&nbsp;&nbsp;NULL)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;PHP_FE(mc_add_server,&nbsp;&nbsp; NULL)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;PHP_FE(mc_shutdown,&nbsp;&nbsp;&nbsp;&nbsp; NULL)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;{NULL, NULL, NULL}<br \/>};<br \/>mc_add_group\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u6574\u5f62\uff08\u5176\u5b9e\u5e94\u8be5\u662f\u4e00\u4e2aobject\uff0c\u6211\u5077\u61d2\u4e86~_~\uff09\u4f5c\u4e3a\u7ec4ID\uff0cmc_add_server\u7684\u65f6\u5019\u8981\u63d0\u4f9b\u4e24\u4e2a\u53c2\u6570\uff0c\u4e00\u4e2a\u662f\u7ec4ID\uff0c\u4e00\u4e2a\u662f\u670d\u52a1\u5668\u5730\u5740\uff08ADDRORT\uff09\u3002<\/p>\n<p>CODE:<br \/>\/**<br \/>* Add a server group<br \/>*\/<br \/>PHP_FUNCTION(mc_add_group)<br \/>{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;apr_int32_t group_id;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;apr_status_t rv;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;if (0 != ZEND_NUM_ARGS())<br \/>&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WRONG_PARAM_COUNT;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RETURN_NULL();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;group_id = free_group_id();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;if (-1 == group_id)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RETURN_FALSE;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;apr_memcache_t *mc;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;rv = apr_memcache_create(p, MAX_G_SERVER, 0, &#038;mc);<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;add_group(group_id, mc);<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;RETURN_DOUBLE(group_id);<br \/>}<\/p>\n<p>CODE:<br \/>\/**<br \/>* Add a server into group<br \/>*\/<br \/>PHP_FUNCTION(mc_add_server)<br \/>{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;apr_status_t rv;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;apr_int32_t group_id;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;double g;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;char *srv_str;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;int srv_str_l;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;if (2 != ZEND_NUM_ARGS())<br \/>&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WRONG_PARAM_COUNT;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, &#8220;ds&#8221;, &#038;g, &#038;srv_str, &#038;srv_str_l) == FAILURE)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RETURN_FALSE;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;group_id = (apr_int32_t) g;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;if (-1 == is_validate_group(group_id))<br \/>&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RETURN_FALSE;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;char *host, *scope;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;apr_port_t port;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;rv = apr_parse_addr_port(&#038;host, &#038;scope, &#038;port, srv_str, p);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;if (APR_SUCCESS == rv)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Create this server object<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;apr_memcache_server_t *st;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rv = apr_memcache_server_create(p, host, port, 0, 64, 1024, 600, &#038;st);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (APR_SUCCESS == rv)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (NULL == mc_groups[group_id])<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RETURN_FALSE;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Add server<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rv = apr_memcache_add_server(mc_groups[group_id], st);<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (APR_SUCCESS == rv)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RETURN_TRUE;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;RETURN_FALSE;<br \/>}<br \/>\u5728set\u548cdel\u6570\u636e\u7684\u65f6\u5019\uff0c\u8981\u5faa\u73af\u6240\u6709\u7684\u7ec4\uff1a<\/p>\n<p>CODE:<br \/>\/**<br \/>* Store item into all groups<br \/>*\/<br \/>PHP_FUNCTION(mc_set)<br \/>{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;char *key, *value;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;int key_l, value_l;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;double ttl = 0;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;double set_ct = 0;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;if (2 != ZEND_NUM_ARGS())<br \/>&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WRONG_PARAM_COUNT;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, &#8220;ss&#124;d&#8221;, &#038;key, &#038;key_l, &#038;value, &#038;value_l, ttl) == FAILURE)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RETURN_FALSE;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Write data into every object<br \/>&nbsp;&nbsp;&nbsp;&nbsp;apr_int32_t i = 0;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;if (ttl < 0)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ttl = 0;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;apr_status_t rv;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;for (i = 0; i < MAX_GROUP; i++)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (0 == is_validate_group(i))<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Write it!<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rv = apr_memcache_add(mc_groups<i>, key, value, value_l, (apr_uint32_t) ttl, 0);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (APR_SUCCESS == rv)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set_ct++;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;RETURN_DOUBLE(set_ct);<br \/>}<br \/>\u5728mc_get\u4e2d\uff0c\u9996\u5148\u8981\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u7ec4\uff0c\u7136\u540e\u4ece\u8fd9\u4e2a\u7ec4\u5f00\u59cb\u8f6e\u8be2\uff1a<br \/>CODE:<br \/>\/**<br \/>* Fetch a item from a random group<br \/>*\/<br \/>PHP_FUNCTION(mc_get)<br \/>{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;char *key, *value = NULL;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;int key_l;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;apr_size_t value_l;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;if (1 != ZEND_NUM_ARGS())<br \/>&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WRONG_PARAM_COUNT;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, &#8220;s&#8221;, &#038;key, &#038;key_l) == FAILURE)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RETURN_MULL();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ I will try &#8230;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Random read<br \/>&nbsp;&nbsp;&nbsp;&nbsp;apr_int32_t curr_group_id = random_group();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;apr_int32_t i = 0;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;apr_int32_t try = 0;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;apr_uint32_t flag;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;apr_memcache_t *oper;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;apr_status_t rv;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;for (i = 0; i < MAX_GROUP; i++)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try = i + curr_group_id;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try = try % MAX_GROUP;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (0 == is_validate_group(try))<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Get a value<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;oper = mc_groups[try];<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rv = apr_memcache_getp(mc_groups[try], p, (const char *) key, &#038;value, &#038;value_l, 0);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (APR_SUCCESS == rv)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RETURN_STRING(value, 1);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;RETURN_FALSE;<br \/>}<\/p>\n<p>CODE:<br \/>\/**<br \/>* Random group id<br \/>* For mc_get()<br \/>*\/<br \/>apr_int32_t random_group()<br \/>{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;struct timeval tv;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;struct timezone tz;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;int usec;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;gettimeofday(&#038;tv, &#038;tz);<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;usec = tv.tv_usec;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;int curr = usec % count_group();<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;return (apr_int32_t) curr;<br \/>}<br \/>BSM_Memcache\u7684\u4f7f\u7528\u65b9\u5f0f\u548c\u5176\u5b83\u7684client\u7c7b\u4f3c\uff1a<\/p>\n<p>CODE:<br \/><?php<br \/>$g1 = mc_add_group();&nbsp;&nbsp;&nbsp;&nbsp;\/\/ \u6dfb\u52a0\u7b2c\u4e00\u4e2a\u7ec4<br \/>$g2 = mc_add_group();&nbsp;&nbsp;&nbsp;&nbsp;\/\/ \u6dfb\u52a0\u7b2c\u4e8c\u4e2a\u7ec4<br \/>mc_add_server($g1, &#39;localhost:11211&#39;);&nbsp;&nbsp;&nbsp;&nbsp;\/\/ \u5728\u7b2c\u4e00\u4e2a\u7ec4\u4e2d\u6dfb\u52a0\u7b2c\u4e00\u53f0\u670d\u52a1\u5668<br \/>mc_add_server($g1, &#39;localhost:11212&#39;);&nbsp;&nbsp;&nbsp;&nbsp;\/\/ \u5728\u7b2c\u4e00\u4e2a\u7ec4\u4e2d\u6dfb\u52a0\u7b2c\u4e8c\u53f0\u670d\u52a1\u5668<br \/>mc_add_server($g2, &#39;10.0.0.16:11211&#39;);&nbsp;&nbsp;&nbsp;&nbsp;\/\/ \u5728\u7b2c\u4e8c\u4e2a\u7ec4\u4e2d\u6dfb\u52a0\u7b2c\u4e00\u53f0\u670d\u52a1\u5668<br \/>mc_add_server($g2, &#39;10.0.0.17:11211&#39;);&nbsp;&nbsp;&nbsp;&nbsp;\/\/ \u5728\u7b2c\u4e8c\u4e2a\u7ec4\u4e2d\u6dfb\u52a0\u7b2c\u4e8c\u53f0\u670d\u52a1\u5668<\/p>\n<p>mc_set(&#39;key&#39;, &#39;Hello&#39;);&nbsp;&nbsp;&nbsp;&nbsp;\/\/ \u5199\u5165\u6570\u636e<br \/>$key = mc_get(&#39;key&#39;);&nbsp;&nbsp;&nbsp;&nbsp;\/\/ \u8bfb\u51fa\u6570\u636e<br \/>mc_del(&#39;key&#39;);&nbsp;&nbsp;&nbsp;&nbsp;\/\/ \u5220\u9664\u6570\u636e<br \/>mc_shutdown();&nbsp;&nbsp;&nbsp;&nbsp;\/\/ \u5173\u95ed\u6240\u6709\u7ec4<br \/>?><br \/>APR_Memcache\u7684\u76f8\u5173\u8d44\u6599\u53ef\u4ee5\u5728\u8fd9\u91cc\u627e\u5230\uff0cBSM_Memcache\u53ef\u4ee5\u5728\u672c\u7ad9\u4e0b\u8f7d\u3002<\/p>\n<p>\u25ceAPR\u73af\u5883\u4ecb\u7ecd<\/p>\n<p>APR \u7684\u5168\u79f0\uff1aApache Portable Runtime\u3002\u5b83\u662fApache\u8f6f\u4ef6\u57fa\u91d1\u4f1a\u521b\u5efa\u5e76\u7ef4\u6301\u7684\u4e00\u5957\u8de8\u5e73\u53f0\u7684C\u8bed\u8a00\u5e93\u3002\u5b83\u4eceApache httpd1.x\u4e2d\u62bd\u53d6\u51fa\u6765\u5e76\u72ec\u7acb\u4e8ehttpd\u4e4b\u5916\uff0cApache httpd2.x\u5c31\u662f\u5efa\u7acb\u5728APR\u4e0a\u3002APR\u63d0\u4f9b\u4e86\u5f88\u591a\u65b9\u4fbf\u7684API\u63a5\u53e3\u53ef\u4f9b\u4f7f\u7528\uff0c\u5305\u62ec\u5982\u5185\u5b58\u6c60\u3001\u5b57\u7b26\u4e32\u64cd\u4f5c\u3001\u7f51\u7edc\u3001\u6570\u7ec4\u3001hash\u8868\u7b49\u5b9e\u7528\u7684\u529f\u80fd\u3002\u5f00\u53d1 Apache2 Module\u8981\u63a5\u89e6\u5f88\u591aAPR\u51fd\u6570\uff0c\u5f53\u7136APR\u53ef\u4ee5\u72ec\u7acb\u5b89\u88c5\u72ec\u7acb\u7528\uff0c\u53ef\u4ee5\u7528\u6765\u5199\u81ea\u5df1\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u4e0d\u4e00\u5b9a\u662fApache httpd\u7684\u76f8\u5173\u5f00\u53d1\u3002<\/p>\n<p>\u25ce\u540e\u8bb0<\/p>\n<p>\u8fd9\u662f\u6211\u5728\u519c\u5386\u4e19\u620c\u5e74\uff08\u6211\u7684\u672c\u547d\u5e74\uff09\u7684\u6700\u540e\u4e00\u7bc7\u6587\u7ae0\uff0c\u7531\u4e8eMemcached\u7684\u5185\u6db5\u5f88\u591a\uff0c\u4ed3\u4fc3\u6574\u7406\u4e00\u5b9a\u6709\u5f88\u591a\u9057\u6f0f\u548c\u9519\u8bef\u3002\u611f\u8c22\u65b0\u6d6a\u7f51\u63d0\u4f9b\u7684\u7814\u7a76\u673a\u4f1a\uff0c\u611f\u8c22\u90e8\u95e8\u540c\u4e8b\u7684\u5e2e\u52a9\u3002<\/p>\n<p>NP\u535a\u58eb 02-13-2007 <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Memcached\u662fdanga.com\uff08\u8fd0\u8425LiveJournal\u7684\u6280\u672f\u56e2\u961f\uff09\u5f00\u53d1\u7684\u4e00\u5957\u5206\u5e03\u5f0f\u5185\u5b58\u5bf9\u8c61\u7f13\u5b58\u7cfb\u7edf <a href='https:\/\/www.icocean.com\/blog\/?p=1860' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[4114,2657,392,1881,184],"class_list":["post-1860","post","type-post","status-publish","format-standard","hentry","category-lamp","tag-lamp","tag-memcache","tag-392","tag-1881","tag-184","category-24-id","post-seq-1","post-parity-odd","meta-position-corners","fix"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1860","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1860"}],"version-history":[{"count":0,"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1860\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1860"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1860"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}