{"id":237,"date":"2024-03-15T03:09:45","date_gmt":"2024-03-14T19:09:45","guid":{"rendered":"https:\/\/669082.xyz\/?p=237"},"modified":"2024-03-15T03:48:42","modified_gmt":"2024-03-14T19:48:42","slug":"20240315%e5%ad%a6%e4%b9%a0php%e6%93%8d%e4%bd%9credis","status":"publish","type":"post","link":"https:\/\/669082.xyz\/index.php\/2024\/03\/15\/20240315%e5%ad%a6%e4%b9%a0php%e6%93%8d%e4%bd%9credis\/","title":{"rendered":"20240315\u5b66\u4e60PHP\u64cd\u4f5credis"},"content":{"rendered":"<h4>PHP\u64cd\u4f5credis<\/h4>\n<h5>string<\/h5>\n<pre><code class=\"language-php\">&lt;?php\n\/\/\u8fde\u63a5\u672c\u5730\u7684 Redis \u670d\u52a1\n$redis = new Redis();\n$redis-&gt;connect(&#039;127.0.0.1&#039;, 6379);\n\n\/\/redis\u5bc6\u7801  \u4e5f\u53ef\u4ee5\u53d6\u6d88\u5bc6\u7801\n$redis-&gt;auth(&#039;123456&#039;);\n\n\/\/\u8bbe\u7f6ekey value\n$redis-&gt;set(&#039;key&#039;, &#039;hello&#039;);\n\n\/\/\u901a\u8fc7key\u8ffd\u52a0\n$redis-&gt;append(&#039;key&#039;, &#039;world&#039;);\n\n\/\/\u901a\u8fc7key\u53d6\u503c\necho $redis-&gt;get(&#039;key&#039;); \/\/&#039;helloworld&#039;\n\n\/\/\u6839\u636ekey\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\n$redis-&gt;expire(&#039;key&#039;,30);  \n\n\/\/\u6839\u636ekey\u67e5\u8be2\u8fc7\u671f\u65f6\u95f4  \u8fd9\u91cc\u8981\u6ce8\u610f  -1\u4ee3\u8868\u6c38\u4e0d\u8fc7\u671f  -2\u4ee3\u8868\u5df1\u8fc7\u671f\necho $redis-&gt;TTL(&#039;key&#039;);\n\n\/\/\u53c2\u8003\u8d44\u6599 https:\/\/www.kancloud.cn\/a173512\/php_note\/1352132<\/code><\/pre>\n<h5>hash\u8868<\/h5>\n<pre><code class=\"language-php\">\/\/\u589e\uff0c\u6539\uff0c\u5c06\u54c8\u5e0c\u8868key\u4e2d\u7684\u57dffield\u7684\u503c\u8bbe\u4e3avalue,\u4e0d\u5b58\u5728\u521b\u5efa,\u5b58\u5728\u5c31\u8986\u76d6\u30101 | 0\u3011\n\/\/\u5982\u679c\u4e4b\u524d\u4e0d\u5b58\u5728\u5c31\u521b\u5efa \u540c\u65f6\u8fd4\u56de1  \u5982\u679c\u4e4b\u524d\u5b58\u5728\u5c31\u4fee\u6539 \u540c\u65f6\u8fd4\u56de0\n$redis-&gt;hset(&#039;key&#039;,&#039;field&#039;,&#039;value11&#039;);\n\n\/\/\u67e5\uff0c\u53d6\u503c\u3010value|false\u3011\n\/\/\u901a\u8fc7key\u4e0efield\u5b57\u6bb5\u83b7\u53d6value\n$redis-&gt;hget(&#039;key&#039;,&#039;field&#039;);\n\n\/\/\u5b9a\u4e49\u4e24\u4e2a\u6570\u7ec4\n\/\/\u5173\u8054\u6570\u7ec4\n$arr = array(&#039;one&#039;=&gt;1, 2, 3, &#039;name&#039;=&gt;&#039;tom&#039;, 4);\n\/\/\u7d22\u5f15\u6570\u7ec4\n$arr2 = array(&#039;one&#039;, 0, 1, &#039;name&#039;, 2);\n\n\/\/\u589e\uff0c\u6539\uff0c\u8bbe\u7f6e\u591a\u4e2a\u503c$arr\u4e3a(\u7d22\u5f15|\u5173\u8054)\u6570\u7ec4,$arr[key]=field, [ true ]\n$redis-&gt;hMset(&#039;key&#039;,$arr);\n\n\/\/\u67e5\uff0c\u83b7\u53d6\u6307\u5b9a\u4e0b\u6807\u7684field\uff0c[$arr | false]    [&quot;one&quot;=&gt;1,0=&gt;2,1=&gt;3,&quot;name&quot;=&gt;&quot;tom&quot;,2=&gt;4]\n\/\/\u8fd4\u56de\u6570\u7ec4 \u5176\u5b9e\u662f\u628a\u7b2c2\u4e2a\u6570\u7ec4\u91cc\u7684\u6bcf\u4e00\u4e2avalue \u5f53\u4f5c \u67e5\u8be2\u7684field  \u5982\u679c\u6ca1\u6709\u90a3\u4e2a\u5b57\u6bb5\uff0c\u5c31\u8fd4\u56defalse\n$redis-&gt;hMget(&#039;key&#039;,$arr2);\n\n$redis-&gt;hGetAll(&#039;key&#039;);\/\/\u67e5\uff0c\u8fd4\u56de\u54c8\u5e0c\u8868key\u4e2d\u7684\u6240\u6709\u57df\u548c\u503c\u3002[\u5f53key\u4e0d\u5b58\u5728\u65f6\uff0c\u8fd4\u56de\u4e00\u4e2a\u7a7a\u8868]\n$redis-&gt;hKeys(&#039;key&#039;);\/\/\u67e5\uff0c\u8fd4\u56de\u54c8\u5e0c\u8868key\u4e2d\u7684\u6240\u6709field\u3002[\u5f53key\u4e0d\u5b58\u5728\u65f6\uff0c\u8fd4\u56de\u4e00\u4e2a\u7a7a\u8868]\n$redis-&gt;hVals(&#039;key&#039;);\/\/\u67e5\uff0c\u8fd4\u56de\u54c8\u5e0c\u8868key\u4e2d\u7684\u6240\u6709\u503c\u3002[\u5f53key\u4e0d\u5b58\u5728\u65f6\uff0c\u8fd4\u56de\u4e00\u4e2a\u7a7a\u8868]\n$redis-&gt;hDel(&#039;key&#039;,$arr2);\/\/\u5220\uff0c\u5220\u9664\u6307\u5b9a\u4e0b\u6807\u7684field,\u4e0d\u5b58\u5728\u7684\u57df\u5c06\u88ab\u5ffd\u7565,[num | false]\n\n$redis-&gt;hExists(&#039;key&#039;,&#039;field&#039;);\/\/\u67e5\u770bhash\u4e2d\u662f\u5426\u5b58\u5728field,[1 | 0]\n$redis-&gt;hIncrBy(&#039;key&#039;,&#039;field&#039;,$int_num);\/\/\u4e3a\u54c8\u5e0c\u8868key\u4e2d\u7684\u57dffield\u7684\u503c\u52a0\u4e0a\u91cf(+|-)num,[new_num | false]\n$redis-&gt;hLen(&#039;key&#039;);\/\/\u8fd4\u56de\u54c8\u5e0c\u8868key\u4e2d\u57df\u7684\u6570\u91cf\u3002[ num | 0]\n\n\u53c2\u8003\u8d44\u6599 https:\/\/www.kancloud.cn\/a173512\/php_note\/1352131<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u64cd\u4f5credis string &lt;?php \/\/\u8fde\u63a5\u672c\u5730\u7684 Redis \u670d\u52a1 $redis [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-237","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/posts\/237","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=237"}],"version-history":[{"count":6,"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/posts\/237\/revisions"}],"predecessor-version":[{"id":243,"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/posts\/237\/revisions\/243"}],"wp:attachment":[{"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}