{"id":67397,"date":"2025-05-10T08:48:33","date_gmt":"2025-05-10T00:48:33","guid":{"rendered":"https:\/\/fwq.ai\/blog\/67397\/"},"modified":"2025-05-10T08:48:33","modified_gmt":"2025-05-10T00:48:33","slug":"redis-%e7%bc%93%e5%ad%98%e5%ae%9e%e7%8e%b0%e5%ad%98%e5%82%a8%e5%92%8c%e8%af%bb%e5%8f%96%e5%8e%86%e5%8f%b2%e6%90%9c%e7%b4%a2%e5%85%b3%e9%94%ae%e5%ad%97%e7%9a%84%e6%93%8d%e4%bd%9c%e6%96%b9%e6%b3%95-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/67397\/","title":{"rendered":"Redis \u7f13\u5b58\u5b9e\u73b0\u5b58\u50a8\u548c\u8bfb\u53d6\u5386\u53f2\u641c\u7d22\u5173\u952e\u5b57\u7684\u64cd\u4f5c\u65b9\u6cd5"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis \u7f13\u5b58\u5b9e\u73b0\u5b58\u50a8\u548c\u8bfb\u53d6\u5386\u53f2\u641c\u7d22\u5173\u952e\u5b57\u7684\u64cd\u4f5c\u65b9\u6cd5<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u600e\u4e48\u5165\u95e8\u6570\u636e\u5e93\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762golang\u5b66\u4e60\u7f51\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300aRedis \u7f13\u5b58\u5b9e\u73b0\u5b58\u50a8\u548c\u8bfb\u53d6\u5386\u53f2\u641c\u7d22\u5173\u952e\u5b57\u7684\u64cd\u4f5c\u65b9\u6cd5\u300b\uff0c\u6d89\u53ca\u5230\u5173\u952e\u5b57\u3001redis\u7f13\u5b58\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b<\/p>\n<h2>\u4e00\u3001\u672c\u6848\u4f8b\u6d89\u53ca\u77e5\u8bc6<\/h2>\n<ol>\n<li>&nbsp;Layui<\/li>\n<li>Redis<\/li>\n<li>Vue.js<\/li>\n<li>jQuery<\/li>\n<li>Ajax<\/li>\n<\/ol>\n<h2>\u4e8c\u3001\u6548\u679c\u56fe<\/h2>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221231\/167246373363afc575d102c.png\" class=\"aligncenter\" title=\"Redis \u7f13\u5b58\u5b9e\u73b0\u5b58\u50a8\u548c\u8bfb\u53d6\u5386\u53f2\u641c\u7d22\u5173\u952e\u5b57\u7684\u64cd\u4f5c\u65b9\u6cd5\u63d2\u56fe\" alt=\"Redis \u7f13\u5b58\u5b9e\u73b0\u5b58\u50a8\u548c\u8bfb\u53d6\u5386\u53f2\u641c\u7d22\u5173\u952e\u5b57\u7684\u64cd\u4f5c\u65b9\u6cd5\u63d2\u56fe\" \/><\/p>\n<h2>\u4e09\u3001\u529f\u80fd\u5b9e\u73b0<\/h2>\n<p>\uff08\u4e00\uff09\u4f7f\u7528 Layui \u7684\u6837\u5f0f\u6784\u5efa\u9875\u9762<\/p>\n<pre>\n\n\n\n \n   Redis\u5e94\u7528 - \u641c\u7d22\u5386\u53f2\n   \n   \n   \n \n     \n      \n      \n      \n      \n    \n \n     \n      \n      \n      \u641c\u7d22 \n      \n    \n \n     \n      \n     \n       \u641c\u7d22\u5386\u53f2 \n      \n    \n \n     \n      \n      \n      <span style=\"margin-left: 5px\">PHP<\/span> \n      <span style=\"margin-left: 5px\">JavaScript<\/span> \n      \n    \n\n   \n\n   \n\n   \n   <\/pre>\n<p>\uff08\u4e8c\uff09\u70b9\u51fb\u641c\u7d22\u65f6\u50a8\u5b58\u672c\u6b21\u641c\u7d22\u7684\u5173\u952e\u5b57<\/p>\n<p>\u7ed9\u6587\u672c\u6846\u6dfb\u52a0 Vue \u53cc\u5411\u7ed1\u5b9a<\/p>\n<pre>\n<\/pre>\n<p>\u7ed9\u641c\u7d22\u6309\u94ae\u6dfb\u52a0\u70b9\u51fb\u4e8b\u4ef6<\/p>\n<pre>\n\u641c\u7d22<\/pre>\n<pre>\n<\/pre>\n<p>\u5f53\u6587\u672c\u6846\u88ab\u8f93\u5165\u5185\u5bb9\u540e\uff0c\u8f93\u5165\u7684\u5185\u5bb9\u5c06\u7ed1\u5b9a\u7ed9 Vue \u4e2d <code>data<\/code> \u7684 <code>keyword<\/code> \u5b57\u6bb5\u3002<\/p>\n<p>\u70b9\u51fb\u641c\u7d22\u6309\u94ae\u65f6\uff0c\u89e6\u53d1 <code>addHistory()<\/code> \u51fd\u6570\uff0c\u6b64\u51fd\u6570\u5c06\u8f93\u5165\u7684\u5185\u5bb9\u53d1\u9001\u7ed9 PHP \uff0cPHP \u64cd\u4f5c Redis \u5c06\u5185\u5bb9\u8fdb\u884c\u7f13\u5b58\u3002<\/p>\n<p><code>addHistory()<\/code> \u51fd\u6570\u4e2d\uff1a<\/p>\n<pre>\naddHistory: function () {\n $.ajax({\n  url: \"history.php\",\n  type: \"GET\",\n  data: {type: 'add', keyword: this.keyword},\n  success: function () {\n  \t\/\/ \u8bf7\u6c42\u6210\u529f\u540e\u5237\u65b0\u672c\u9875\u9762\n   window.location.reload();\n  }\n });\n}<\/pre>\n<p><code>data<\/code> \u4e2d\u4f20\u503c\u4e24\u4e2a\u5b57\u6bb5\uff0c<code>type<\/code> \u8868\u793a\u672c\u6b21\u8bf7\u6c42\u7684\u7c7b\u578b\uff0c\u5176\u4e2d <code>add<\/code> \u4ee3\u8868\u5f80\u7f13\u5b58\u4e2d\u6dfb\u52a0\u5173\u952e\u5b57\uff0c<code>read<\/code> \u4ee3\u8868\u4ece\u7f13\u5b58\u4e2d\u8bfb\u53d6\u5173\u952e\u5b57\u3002<\/p>\n<p><code>history.php<\/code> \u4e2d\uff1a<\/p>\n<pre>\nconnect('localhost', 6379);\nif (!$con) {\n echo 'Redis\u8fde\u63a5\u5931\u8d25';\n}\n\/\/ \u63a5\u6536\u8bf7\u6c42\u7c7b\u578b\u53c2\u6570\u7684\u503c\n$type = $_GET['type'];\n\/\/ \u6a21\u62df\u7528\u6237\u7684id\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u7528\u6237\u641c\u7d22\u7684\u5185\u5bb9\u4e0d\u540c\uff0c\u9700\u8981\u8fdb\u884c\u533a\u5206\n$user_id = 'user-1';\n\/\/ \u5982\u679c\u8bf7\u6c42\u7c7b\u578b\u4e3a\u6dfb\u52a0\nif ($type == 'add') {\n\t\/\/ \u63a5\u6536\u8f93\u5165\u7684\u5173\u952e\u5b57\n $keyword = $_GET['keyword'];\n \/\/ \u8bfb\u53d6\u5f53\u524d\u7528\u6237\u961f\u5217\u4e2d\u5b58\u50a8\u7684\u5173\u952e\u5b57\u4e2a\u6570\uff0c\u5373\u961f\u5217\u7684\u957f\u5ea6\n $len = $redis-&gt;llen($user_id);\n \/\/ \u5982\u679c\u4e2a\u6570\u5927\u4e8e\u7b49\u4e8e 5 \u4e2a\uff0c\u5219\u5220\u9664\u6700\u5f00\u59cb\u641c\u7d22\u7684\u5173\u952e\u5b57\uff0c\u52a0\u5165\u6700\u65b0\u641c\u7d22\u7684\u5173\u952e\u5b57\n if ($len &gt;= 5) {\n \t\/\/ \u79fb\u9664\u961f\u5217\u5de6\u4fa7\u7684\u7b2c\u4e00\u4e2a\u5173\u952e\u5b57\n  $redis-&gt;lPop($user_id);\n  \/\/ \u5728\u961f\u5217\u53f3\u4fa7\u52a0\u5165\u65b0\u7684\u5173\u952e\u5b57\n  $redis-&gt;rPush($user_id, $keyword);\n } else {\n \t\/\/ \u4e0d\u591a\u4e8e 5 \u4e2a\u76f4\u63a5\u5728\u961f\u5217\u53f3\u4fa7\u52a0\u5165\u65b0\u7684\u5173\u952e\u5b57\n  $redis-&gt;rPush($user_id, $keyword);\n }\n}<\/pre>\n<p>\uff08\u4e09\uff09\u8bfb\u53d6\u5e76\u5c55\u793a\u5386\u53f2\u641c\u7d22\u7684\u5173\u952e\u5b57<\/p>\n<p>\u7b2c\u4e8c\u6b65\u4e2d\u52a0\u5165\u4e86\u5f53\u8bf7\u6c42\u6dfb\u52a0\u7f13\u5b58\u6210\u529f\u540e\u4f1a\u5237\u65b0\u9875\u9762\u7684\u4ee3\u7801\uff0c<\/p>\n<pre>\nwindow.location.reload();<\/pre>\n<p>\u5728\u8fd9\u4e2a\u57fa\u7840\u4e0a\uff0c\u6211\u4eec\u5e0c\u671b\u5237\u65b0\u7684\u540c\u65f6\u6267\u884c\u53e6\u4e00\u4e2a Ajax \u8bf7\u6c42\u4ece PHP \u4e2d\u64cd\u4f5c Redis \u5c06\u6240\u6709\u7684\u5386\u53f2\u641c\u7d22\u5173\u952e\u5b57\u8bfb\u53d6\u51fa\u6765\u5e76\u5728\u9875\u9762\u4e2d\u5c55\u793a\u3002<\/p>\n<p>\u6240\u4ee5\u5728 Vue \u4e2d\u52a0\u5165\u9875\u9762\u52a0\u8f7d\u5b8c\u6210\u81ea\u52a8\u8c03\u7528<code>getHistory()<\/code>\u51fd\u6570\uff1a<\/p>\n<pre>\nmethods: {\n getHistory: function () {},\n addHistory: function () {\n  $.ajax({\n   url: \"history.php\",\n   type: \"GET\",\n   data: {type: 'add', keyword: this.keyword},\n   success: function () {\n    window.location.reload();\n   }\n  });\n }\n},\n\/\/ \u9875\u9762\u52a0\u8f7d\u5b8c\u6210\u81ea\u52a8\u8c03\u7528 getHistory()\ncreated () {\n this.getHistory();\n}<\/pre>\n<p><code>getHistory()<\/code>\u51fd\u6570\u4e2d\uff1a<\/p>\n<pre>\ngetHistory: function () {\n $.ajax({\n  url: \"history.php\",\n  type: \"GET\",\n  data: {type: 'read'},\n  success: function (r) {\n  \t\/\/ JSON.parse(r) \u5c06\u8bfb\u53d6\u5230\u7684 json \u5b57\u7b26\u4e32\u8f6c\u4e3a json \u5bf9\u8c61\n   vm.history = JSON.parse(r);\n  }\n });\n}<\/pre>\n<p><code>data<\/code> \u4e2d\u4f20\u503c\u4e00\u4e2a\u5b57\u6bb5\uff0c<code>read<\/code> \u4ee3\u8868\u4ece\u7f13\u5b58\u4e2d\u8bfb\u53d6\u5173\u952e\u5b57\uff0c\u8bf7\u6c42\u6210\u529f\u540e\u5c06\u8fd4\u56de\u7684\u7ed3\u679c\u8d4b\u503c\u7ed9 Vue \u4e2d <code>data<\/code> \u7684 <code>history<\/code> \u5b57\u6bb5\u3002<\/p>\n<p><code>history.php<\/code> \u4e2d\u6dfb\u52a0\u8bfb\u53d6\u64cd\u4f5c\uff1a<\/p>\n<pre>\n\/\/ \u5982\u679c\u8bf7\u6c42\u7c7b\u578b\u4e3a\u8bfb\u53d6\nif ($type == 'read') {\n\t\/\/ \u4ece\u961f\u5217\u5de6\u4fa7\u4f9d\u6b21\u53d6\u51fa 5 \u4e2a\u5173\u952e\u5b57\n $history = $redis-&gt;lrange($user_id, 0, 4);\n \/\/ \u8f6c\u4e3a json \u683c\u5f0f\u7684\u6570\u636e\u5e76\u8f93\u51fa\u5230\u9875\u9762\u4e2d\u4f9b Ajax \u4f7f\u7528\n echo json_encode($history, JSON_UNESCAPED_UNICODE);\n}<\/pre>\n<p>\u5c06\u8bfb\u53d6\u5230\u7684\u6570\u636e\u6210\u529f\u8d4b\u503c\u7ed9 Vue \u4e2d <code>data<\/code> \u7684 <code>history<\/code> \u5b57\u6bb5\u540e\uff0c\u9875\u9762\u4e2d\u5373\u53ef\u5c06\u6570\u636e\u5faa\u73af\u8f93\u51fa\u5c55\u793a\uff1a<\/p>\n<pre>\n<span style=\"margin-left: 5px\">{{item}}<\/span><\/pre>\n<p>\u8fde\u8d2f\u8fc7\u7a0b\u4e3a\uff1a\u7528\u6237\u8f93\u5165\u5173\u952e\u5b57\u5e76\u70b9\u51fb\u641c\u7d22\u6309\u94ae\uff0cAjax \u8bf7\u6c42 PHP \u64cd\u4f5c Redis \u8fdb\u884c\u6570\u636e\u7f13\u5b58\u4e14\u7f13\u5b58\u6210\u529f\u540e\u5237\u65b0\u9875\u9762\uff0c\u9875\u9762\u5237\u65b0\u540e\u81ea\u52a8\u8c03\u7528\u51fd\u6570\u6267\u884c Ajax \u8bf7\u6c42 PHP \u64cd\u4f5c Redis \u8fdb\u884c\u7f13\u5b58\u6570\u636e\u7684\u8bfb\u53d6\u5e76\u8fd4\u56de\u4e8e\u9875\u9762\u4e2d\u540c\u65f6\u8fdb\u884c\u6e32\u67d3\u5c55\u793a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis \u7f13\u5b58\u5b9e\u73b0\u5b58\u50a8\u548c\u8bfb\u53d6\u5386\u53f2\u641c\u7d22\u5173\u952e\u5b57\u7684\u64cd\u4f5c\u65b9\u6cd5 \u6536\u85cf \u600e\u4e48\u5165\u95e8\u6570\u636e\u5e93\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762golang\u5b66\u4e60\u7f51\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300aRedis \u7f13\u5b58\u5b9e\u73b0\u5b58\u50a8\u548c\u8bfb\u53d6\u5386\u53f2\u641c\u7d22\u5173\u952e\u5b57\u7684\u64cd\u4f5c\u65b9\u6cd5\u300b\uff0c\u6d89\u53ca\u5230\u5173\u952e\u5b57\u3001redis\u7f13\u5b58\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b \u4e00\u3001\u672c\u6848\u4f8b\u6d89\u53ca\u77e5\u8bc6 &nbsp;Layui Redis Vue.js jQuery Ajax \u4e8c\u3001\u6548\u679c\u56fe \u4e09\u3001\u529f\u80fd\u5b9e\u73b0 \uff08\u4e00\uff09\u4f7f\u7528 Layui \u7684\u6837\u5f0f\u6784\u5efa\u9875\u9762 Redis\u5e94\u7528 &#8211; \u641c\u7d22\u5386\u53f2 \u641c\u7d22 \u641c\u7d22\u5386\u53f2 PHP JavaScript \uff08\u4e8c\uff09\u70b9\u51fb\u641c\u7d22\u65f6\u50a8\u5b58\u672c\u6b21\u641c\u7d22\u7684\u5173\u952e\u5b57 \u7ed9\u6587\u672c\u6846\u6dfb\u52a0 Vue \u53cc\u5411\u7ed1\u5b9a \u7ed9\u641c\u7d22\u6309\u94ae\u6dfb\u52a0\u70b9\u51fb\u4e8b\u4ef6 \u641c\u7d22 \u5f53\u6587\u672c\u6846\u88ab\u8f93\u5165\u5185\u5bb9\u540e\uff0c\u8f93\u5165\u7684\u5185\u5bb9\u5c06\u7ed1\u5b9a\u7ed9 Vue \u4e2d data \u7684 keyword \u5b57\u6bb5\u3002 \u70b9\u51fb\u641c\u7d22\u6309\u94ae\u65f6\uff0c\u89e6\u53d1 addHistory() \u51fd\u6570\uff0c\u6b64\u51fd\u6570\u5c06\u8f93\u5165\u7684\u5185\u5bb9\u53d1\u9001\u7ed9 PHP \uff0cPHP \u64cd\u4f5c Redis \u5c06\u5185\u5bb9\u8fdb\u884c\u7f13\u5b58\u3002 addHistory() \u51fd\u6570\u4e2d\uff1a addHistory: function () { $.ajax({ url: &#8220;history.php&#8221;, type: &#8220;GET&#8221;, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-67397","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/67397","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=67397"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/67397\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=67397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=67397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=67397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}