{"id":5855,"date":"2024-11-14T13:46:00","date_gmt":"2024-11-14T05:46:00","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5855\/"},"modified":"2024-11-14T13:46:00","modified_gmt":"2024-11-14T05:46:00","slug":"redis%e6%80%8e%e4%b9%88%e4%bd%bf%e7%94%a8php","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5855\/","title":{"rendered":"redis\u600e\u4e48\u4f7f\u7528php"},"content":{"rendered":"<blockquote><p>\n  \u95ee\u9898\uff1a\u5982\u4f55\u5728 php \u4e2d\u4f7f\u7528 redis\uff1f\u5b89\u88c5 redis php \u6269\u5c55\u3002\u8fde\u63a5\u5230 redis \u670d\u52a1\u5668\uff0c\u4f7f\u7528 redis \u7c7b\u3002\u5b58\u50a8\u548c\u68c0\u7d22\u6570\u636e\uff0c\u652f\u6301\u5b57\u7b26\u4e32\u3001\u6563\u5217\u3001\u5217\u8868\u548c\u96c6\u5408\u7b49\u7c7b\u578b\u3002\u4f7f\u7528\u5176\u4ed6\u547d\u4ee4\uff0c\u5982\u68c0\u67e5\u952e\u662f\u5426\u5b58\u5728\u3001\u5220\u9664\u952e\u548c\u8bbe\u7f6e\u952e\u7684\u8fc7\u671f\u65f6\u95f4\u3002\u4f7f\u7528\u5b8c redis \u540e\u5173\u95ed\u8fde\u63a5\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/03\/2024060320514117439.jpg\" class=\"aligncenter\" title=\"redis\u600e\u4e48\u4f7f\u7528php\u63d2\u56fe\" alt=\"redis\u600e\u4e48\u4f7f\u7528php\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 PHP \u4e2d\u4f7f\u7528 Redis<\/strong><\/p>\n<p>Redis \u662f\u4e00\u4e2a\u9ad8\u5ea6\u53ef\u6269\u5c55\u7684\u5185\u5b58\u4e2d\u6570\u636e\u7ed3\u6784\u5b58\u50a8\u7cfb\u7edf\uff0c\u5728 PHP \u4e2d\u4f7f\u7528\u5b83\u53ef\u4ee5\u6781\u5927\u5730\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002<\/p>\n<p><strong>\u5b89\u88c5 Redis PHP \u6269\u5c55<\/strong><\/p>\n<p>\u5728\u4f7f\u7528 Redis \u4e4b\u524d\uff0c\u60a8\u9700\u8981\u5728 PHP \u4e2d\u5b89\u88c5\u5176\u6269\u5c55\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b8c\u6210\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre># Ubuntu\/Debian\nsudo apt-get install php-redis\n\n# CentOS\/Red Hat\nsudo yum install php-redis<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8fde\u63a5\u5230 Redis \u670d\u52a1\u5668<\/strong><\/p>\n<p>\u8fde\u63a5\u5230 Redis \u670d\u52a1\u5668\u662f\u4f7f\u7528 Redis \u7c7b\u5b9e\u73b0\u7684\uff1a<\/p>\n<pre>$redis = new Redis();\n$redis-&gt;connect('localhost', 6379);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5b58\u50a8\u548c\u68c0\u7d22\u6570\u636e<\/strong><\/p>\n<p>Redis \u652f\u6301\u5b58\u50a8\u591a\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u5b57\u7b26\u4e32\u3001\u6563\u5217\u3001\u5217\u8868\u548c\u96c6\u5408\u3002\u4ee5\u4e0b\u662f\u5b58\u50a8\u548c\u68c0\u7d22\u6570\u636e\u7684\u4e00\u4e9b\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li>\n<p><strong>\u5b58\u50a8\u5b57\u7b26\u4e32\uff1a<\/strong><\/p>\n<pre>$redis-&gt;set('key', 'value');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p><strong>\u68c0\u7d22\u5b57\u7b26\u4e32\uff1a<\/strong><\/p>\n<pre>$value = $redis-&gt;get('key');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p><strong>\u5b58\u50a8\u6563\u5217\uff1a<\/strong><\/p>\n<pre>$redis-&gt;hset('user', 'name', 'John Doe');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p><strong>\u68c0\u7d22\u6563\u5217\uff1a<\/strong><\/p>\n<pre>$name = $redis-&gt;hget('user', 'name');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p><strong>\u5b58\u50a8\u5217\u8868\uff1a<\/strong><\/p>\n<pre>$redis-&gt;rpush('users', 'John Doe');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p><strong>\u68c0\u7d22\u5217\u8868\uff1a<\/strong><\/p>\n<pre>$users = $redis-&gt;lrange('users', 0, -1);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ul>\n<p><strong>\u5176\u4ed6\u5e38\u7528\u547d\u4ee4<\/strong><\/p>\n<p>Redis \u8fd8\u63d0\u4f9b\u4e86\u591a\u79cd\u5176\u4ed6\u547d\u4ee4\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\n<p><strong>\u68c0\u67e5\u952e\u662f\u5426\u5b58\u5728\uff1a<\/strong><\/p>\n<pre>$exists = $redis-&gt;exists('key');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p><strong>\u5220\u9664\u952e\uff1a<\/strong><\/p>\n<pre>$redis-&gt;del('key');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p><strong>\u8bbe\u7f6e\u952e\u7684\u8fc7\u671f\u65f6\u95f4\uff1a<\/strong><\/p>\n<pre>$redis-&gt;expire('key', 3600);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ul>\n<p><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/p>\n<p>\u4f7f\u7528 Redis \u540e\uff0c\u8bf7\u52a1\u5fc5\u5173\u95ed\u8fde\u63a5\uff1a<\/p>\n<pre>$redis-&gt;close();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5728 PHP \u4e2d\u5229\u7528 Redis \u7684\u5f3a\u5927\u529f\u80fd\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u4f7f\u7528php\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u95ee\u9898\uff1a\u5982\u4f55\u5728 php \u4e2d\u4f7f\u7528 redis\uff1f\u5b89\u88c5 redis php \u6269\u5c55\u3002\u8fde\u63a5\u5230 redis \u670d\u52a1\u5668\uff0c\u4f7f\u7528 redis \u7c7b\u3002\u5b58\u50a8\u548c\u68c0\u7d22\u6570\u636e\uff0c\u652f\u6301\u5b57\u7b26\u4e32\u3001\u6563\u5217\u3001\u5217\u8868\u548c\u96c6\u5408\u7b49\u7c7b\u578b\u3002\u4f7f\u7528\u5176\u4ed6\u547d\u4ee4\uff0c\u5982\u68c0\u67e5\u952e\u662f\u5426\u5b58\u5728\u3001\u5220\u9664\u952e\u548c\u8bbe\u7f6e\u952e\u7684\u8fc7\u671f\u65f6\u95f4\u3002\u4f7f\u7528\u5b8c redis \u540e\u5173\u95ed\u8fde\u63a5\u3002 \u5982\u4f55\u5728 PHP \u4e2d\u4f7f\u7528 Redis Redis \u662f\u4e00\u4e2a\u9ad8\u5ea6\u53ef\u6269\u5c55\u7684\u5185\u5b58\u4e2d\u6570\u636e\u7ed3\u6784\u5b58\u50a8\u7cfb\u7edf\uff0c\u5728 PHP \u4e2d\u4f7f\u7528\u5b83\u53ef\u4ee5\u6781\u5927\u5730\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002 \u5b89\u88c5 Redis PHP \u6269\u5c55 \u5728\u4f7f\u7528 Redis \u4e4b\u524d\uff0c\u60a8\u9700\u8981\u5728 PHP \u4e2d\u5b89\u88c5\u5176\u6269\u5c55\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b8c\u6210\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b # Ubuntu\/Debian sudo apt-get install php-redis # CentOS\/Red Hat sudo yum install php-redis \u767b\u5f55\u540e\u590d\u5236 \u8fde\u63a5\u5230 Redis \u670d\u52a1\u5668 \u8fde\u63a5\u5230 Redis \u670d\u52a1\u5668\u662f\u4f7f\u7528 Redis \u7c7b\u5b9e\u73b0\u7684\uff1a $redis = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-5855","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5855","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=5855"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5855\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5855"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5855"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5855"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}