{"id":202370,"date":"2025-05-10T15:42:49","date_gmt":"2025-05-10T07:42:49","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202370\/"},"modified":"2025-05-10T15:42:49","modified_gmt":"2025-05-10T07:42:49","slug":"%e6%80%8e%e4%b9%88%e7%94%a8phpredis%e5%ae%9e%e7%8e%b0%e6%8e%92%e8%a1%8c%e6%a6%9c","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202370\/","title":{"rendered":"\u600e\u4e48\u7528PHP+Redis\u5b9e\u73b0\u6392\u884c\u699c"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u600e\u4e48\u7528PHP+Redis\u5b9e\u73b0\u6392\u884c\u699c<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662fgolang\u5b66\u4e60\u7f51\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u600e\u4e48\u7528PHP+Redis\u5b9e\u73b0\u6392\u884c\u699c\u300b<\/span>\uff0c\u672c\u6587\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p>\u901a\u8fc7php\u548credis\u5b9e\u73b0\u4e00\u4e2a\u5c0f\u529f\u80fd\u6392\u884c\u699c\uff0c\u7528\u7684\u6570\u636e\u7c7b\u578b\u662f\u6709\u5e8f\u96c6\u5408\uff1azrevrange \u9012\u589e\u6392\u5e8f\uff0czrange \u9012\u51cf\u6392\u5e8f<\/p>\n<pre>\/**\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;\u6392\u884c\u699c&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*\/public&nbsp;function&nbsp;rank()\n&nbsp;&nbsp;&nbsp;&nbsp;{\/\/&nbsp;$this-&gt;zrem($this-&gt;cachekey);$this-&gt;redis-&gt;del($this-&gt;cachekey);$dataOne&nbsp;=&nbsp;[];for&nbsp;($i=0;&nbsp;$i&nbsp;&lt;&nbsp;5;&nbsp;$i++)&nbsp;{&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u751f\u6210\u968f\u673a\u6570$num&nbsp;=&nbsp;rand(0,100);\/\/&nbsp;\u751f\u6210\u968f\u673a\u5b57\u7b26\u4e32$str&nbsp;=&nbsp;$this-&gt;get_random(6,'abcdefghijklmnopqrstuvwxyzABCDEFJHIJKLMNOPQRSTUVWXYZ');&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;redis-&gt;zadd($this-&gt;cachekey,$num,json_encode(['name'=&gt;$str]));\/\/&nbsp;\u7531\u5927\u5230\u5c0f\u6392\u5e8f$dataOne&nbsp;=&nbsp;$this-&gt;redis-&gt;ZREVRANGE($this-&gt;cachekey,&nbsp;0,&nbsp;-1,&nbsp;true);\/\/&nbsp;\u7531\u5c0f\u5230\u5927\u6392\u5e8f$dataTow&nbsp;=&nbsp;$this-&gt;redis-&gt;ZRANGE($this-&gt;cachekey,&nbsp;0,&nbsp;-1,&nbsp;true);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}echo&nbsp;\"&lt;pre&gt;\";print_r($dataOne);print_r($dataTow);\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\/\/&nbsp;\u751f\u6210\u968f\u673a\u5b57\u7b26\u4e32public&nbsp;function&nbsp;get_random($len,$chars)\n&nbsp;&nbsp;&nbsp;&nbsp;{$hash&nbsp;=&nbsp;\"\";$max&nbsp;=&nbsp;strlen($chars)&nbsp;-&nbsp;1;for&nbsp;($i=0;&nbsp;$i&nbsp;&lt;&nbsp;$len;&nbsp;$i++)&nbsp;{&nbsp;$hash&nbsp;.=&nbsp;$chars[mt_rand(0,$max)];\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}return&nbsp;$hash;\n&nbsp;&nbsp;&nbsp;&nbsp;}<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20240426\/1714107249662b3371dc8e2.png\" class=\"aligncenter\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u600e\u4e48\u7528PHP+Redis\u5b9e\u73b0\u6392\u884c&#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-202370","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202370","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=202370"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202370\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}