{"id":159725,"date":"2024-11-03T17:07:45","date_gmt":"2024-11-03T09:07:45","guid":{"rendered":"https:\/\/server.hk\/cnblog\/159725\/"},"modified":"2024-11-03T17:07:46","modified_gmt":"2024-11-03T09:07:46","slug":"%e7%b4%85%e8%89%b2%e7%89%b9%e8%89%b2%e5%88%a9%e7%94%a8redis%e7%b7%a9%e5%ad%98%e6%8f%90%e5%8d%87%e5%9c%96%e5%bd%a2%e6%80%a7%e8%83%bd%ef%bc%88redis%e7%b7%a9%e5%ad%98%e5%9c%96%e5%bd%a2%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/159725\/","title":{"rendered":"\u7d05\u8272\u7279\u8272\u5229\u7528Redis\u7de9\u5b58\u63d0\u5347\u5716\u5f62\u6027\u80fd\uff08redis\u7de9\u5b58\u5716\u5f62\uff09"},"content":{"rendered":"<h1 id=\"%e7%b4%85%e8%89%b2%e7%89%b9%e8%89%b2%e5%88%a9%e7%94%a8redis%e7%b7%a9%e5%ad%98%e6%8f%90%e5%8d%87%e5%9c%96%e5%bd%a2%e6%80%a7%e8%83%bd%ef%bc%88redis%e7%b7%a9%e5%ad%98%e5%9c%96%e5%bd%a2%ef%bc%89-KHqWCBZbEs\">\u7d05\u8272\u7279\u8272\u5229\u7528Redis\u7de9\u5b58\u63d0\u5347\u5716\u5f62\u6027\u80fd\uff08redis\u7de9\u5b58\u5716\u5f62\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u7db2\u7ad9\u6027\u80fd\u7684\u512a\u5316\u5df2\u6210\u70ba\u6bcf\u500b\u958b\u767c\u8005\u548c\u4f01\u696d\u7684\u9996\u8981\u4efb\u52d9\u3002\u5c24\u5176\u662f\u5728\u8655\u7406\u5927\u91cf\u5716\u5f62\u6578\u64da\u6642\uff0c\u5982\u4f55\u6709\u6548\u5730\u63d0\u5347\u5716\u5f62\u6027\u80fd\u6210\u70ba\u4e86\u4e00\u500b\u91cd\u8981\u7684\u8ab2\u984c\u3002Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u7de9\u5b58\u89e3\u6c7a\u65b9\u6848\uff0c\u80fd\u5920\u986f\u8457\u63d0\u9ad8\u5716\u5f62\u6027\u80fd\uff0c\u7279\u5225\u662f\u5728\u7d05\u8272\u7279\u8272\u7684\u61c9\u7528\u5834\u666f\u4e2d\uff0c\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5229\u7528Redis\u7de9\u5b58\u4f86\u63d0\u5347\u5716\u5f62\u6027\u80fd\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afredis%ef%bc%9f-KHqWCBZbEs\">\u4ec0\u9ebc\u662fRedis\uff1f<\/h2>\n<p>Redis\u662f\u4e00\u7a2e\u958b\u6e90\u7684\u9ad8\u6027\u80fd\u9375\u503c\u6578\u64da\u5eab\uff0c\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u54c8\u5e0c\u3001\u5217\u8868\u3001\u96c6\u5408\u7b49\u3002\u7531\u65bc\u5176\u9ad8\u6548\u7684\u5167\u5b58\u5b58\u5132\u7279\u6027\uff0cRedis\u88ab\u5ee3\u6cdb\u61c9\u7528\u65bc\u7de9\u5b58\u3001\u6d88\u606f\u968a\u5217\u548c\u5be6\u6642\u6578\u64da\u8655\u7406\u7b49\u5834\u666f\u3002\u5176\u5feb\u901f\u7684\u8b80\u53d6\u548c\u5beb\u5165\u901f\u5ea6\u4f7f\u5f97Redis\u6210\u70ba\u63d0\u5347\u61c9\u7528\u6027\u80fd\u7684\u7406\u60f3\u9078\u64c7\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87redis%e4%bd%9c%e7%82%ba%e7%b7%a9%e5%ad%98%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%a1%88%ef%bc%9f-KHqWCBZbEs\">\u70ba\u4ec0\u9ebc\u9078\u64c7Redis\u4f5c\u70ba\u7de9\u5b58\u89e3\u6c7a\u65b9\u6848\uff1f<\/h2>\n<ul>\n<li><strong>\u9ad8\u6027\u80fd\uff1a<\/strong>Redis\u7684\u6578\u64da\u5b58\u5132\u5728\u5167\u5b58\u4e2d\uff0c\u8b80\u53d6\u901f\u5ea6\u6975\u5feb\uff0c\u80fd\u5920\u5728\u6beb\u79d2\u7d1a\u5225\u5167\u97ff\u61c9\u8acb\u6c42\u3002<\/li>\n<li><strong>\u9748\u6d3b\u6027\uff1a<\/strong>\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u80fd\u5920\u6839\u64da\u4e0d\u540c\u7684\u9700\u6c42\u9032\u884c\u9748\u6d3b\u7684\u6578\u64da\u5b58\u5132\u548c\u6aa2\u7d22\u3002<\/li>\n<li><strong>\u6301\u4e45\u5316\uff1a<\/strong>\u96d6\u7136Redis\u4e3b\u8981\u7528\u4f5c\u5167\u5b58\u6578\u64da\u5eab\uff0c\u4f46\u5b83\u4e5f\u652f\u6301\u6578\u64da\u6301\u4e45\u5316\uff0c\u80fd\u5920\u5728\u7cfb\u7d71\u91cd\u555f\u5f8c\u6062\u5fa9\u6578\u64da\u3002<\/li>\n<li><strong>\u5206\u4f48\u5f0f\u652f\u6301\uff1a<\/strong>Redis\u652f\u6301\u4e3b\u5f9e\u8907\u88fd\u548c\u5206\u7247\uff0c\u80fd\u5920\u8f15\u9b06\u64f4\u5c55\u4ee5\u61c9\u5c0d\u9ad8\u6d41\u91cf\u7684\u9700\u6c42\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8redis%e7%b7%a9%e5%ad%98%e6%8f%90%e5%8d%87%e5%9c%96%e5%bd%a2%e6%80%a7%e8%83%bd-KHqWCBZbEs\">\u5982\u4f55\u5229\u7528Redis\u7de9\u5b58\u63d0\u5347\u5716\u5f62\u6027\u80fd<\/h2>\n<p>\u5728\u8655\u7406\u5716\u5f62\u6578\u64da\u6642\uff0c\u4f7f\u7528Redis\u9032\u884c\u7de9\u5b58\u53ef\u4ee5\u986f\u8457\u6e1b\u5c11\u6578\u64da\u5eab\u7684\u67e5\u8a62\u6b21\u6578\uff0c\u5f9e\u800c\u63d0\u9ad8\u6574\u9ad4\u6027\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5177\u9ad4\u7684\u5be6\u65bd\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e5%9c%96%e5%bd%a2%e6%95%b8%e6%93%9a%e7%9a%84%e7%b7%a9%e5%ad%98-KHqWCBZbEs\">1. \u5716\u5f62\u6578\u64da\u7684\u7de9\u5b58<\/h3>\n<p>\u5c07\u7d93\u5e38\u4f7f\u7528\u7684\u5716\u5f62\u6578\u64da\u7de9\u5b58\u5230Redis\u4e2d\uff0c\u53ef\u4ee5\u6e1b\u5c11\u5c0d\u5f8c\u7aef\u6578\u64da\u5eab\u7684\u8acb\u6c42\u3002\u4f8b\u5982\uff0c\u7576\u7528\u6236\u8acb\u6c42\u67d0\u500b\u5716\u5f62\u6642\uff0c\u9996\u5148\u6aa2\u67e5Redis\u4e2d\u662f\u5426\u5b58\u5728\u8a72\u5716\u5f62\u7684\u7de9\u5b58\uff0c\u5982\u679c\u5b58\u5728\u5247\u76f4\u63a5\u8fd4\u56de\uff0c\u5426\u5247\u5f9e\u6578\u64da\u5eab\u4e2d\u67e5\u8a62\u4e26\u5c07\u7d50\u679c\u5b58\u5165Redis\u3002<\/p>\n<pre><code>function getImage($imageId) {\n    $cachedImage = $redis-&gt;get($imageId);\n    if ($cachedImage) {\n        return $cachedImage; \/\/ \u5f9e\u7de9\u5b58\u4e2d\u7372\u53d6\n    } else {\n        $image = fetchImageFromDatabase($imageId);\n        $redis-&gt;set($imageId, $image); \/\/ \u5b58\u5165\u7de9\u5b58\n        return $image;\n    }\n}<\/code><\/pre>\n<h3 id=\"2-%e5%9c%96%e5%bd%a2%e7%94%9f%e6%88%90%e7%9a%84%e7%b7%a9%e5%ad%98-KHqWCBZbEs\">2. \u5716\u5f62\u751f\u6210\u7684\u7de9\u5b58<\/h3>\n<p>\u5c0d\u65bc\u9700\u8981\u9032\u884c\u8a08\u7b97\u6216\u751f\u6210\u7684\u5716\u5f62\uff08\u5982\u5831\u8868\u3001\u5716\u8868\u7b49\uff09\uff0c\u53ef\u4ee5\u5c07\u751f\u6210\u7684\u7d50\u679c\u7de9\u5b58\u5230Redis\u4e2d\u3002\u9019\u6a23\uff0c\u7576\u7528\u6236\u518d\u6b21\u8acb\u6c42\u76f8\u540c\u7684\u5716\u5f62\u6642\uff0c\u53ef\u4ee5\u76f4\u63a5\u5f9e\u7de9\u5b58\u4e2d\u7372\u53d6\uff0c\u907f\u514d\u91cd\u8907\u8a08\u7b97\u3002<\/p>\n<pre><code>function generateGraph($data) {\n    $cacheKey = md5(json_encode($data));\n    $cachedGraph = $redis-&gt;get($cacheKey);\n    if ($cachedGraph) {\n        return $cachedGraph; \/\/ \u5f9e\u7de9\u5b58\u4e2d\u7372\u53d6\n    } else {\n        $graph = createGraph($data);\n        $redis-&gt;set($cacheKey, $graph); \/\/ \u5b58\u5165\u7de9\u5b58\n        return $graph;\n    }\n}<\/code><\/pre>\n<h3 id=\"3-%e5%9c%96%e5%bd%a2%e6%9b%b4%e6%96%b0%e7%9a%84%e7%ad%96%e7%95%a5-KHqWCBZbEs\">3. \u5716\u5f62\u66f4\u65b0\u7684\u7b56\u7565<\/h3>\n<p>\u5728\u4f7f\u7528Redis\u9032\u884c\u7de9\u5b58\u6642\uff0c\u5fc5\u9808\u8003\u616e\u5230\u6578\u64da\u7684\u66f4\u65b0\u554f\u984c\u3002\u53ef\u4ee5\u8a2d\u7f6e\u4e00\u500b\u904e\u671f\u6642\u9593\uff0c\u5b9a\u671f\u6e05\u7406\u904e\u671f\u7684\u7de9\u5b58\uff0c\u6216\u8005\u5728\u6578\u64da\u5eab\u4e2d\u7684\u5716\u5f62\u6578\u64da\u66f4\u65b0\u6642\uff0c\u4e3b\u52d5\u66f4\u65b0Redis\u4e2d\u7684\u7de9\u5b58\u3002<\/p>\n<pre><code>function updateImageCache($imageId, $newImage) {\n    $redis-&gt;set($imageId, $newImage); \/\/ \u66f4\u65b0\u7de9\u5b58\n}<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-KHqWCBZbEs\">\u7d50\u8ad6<\/h2>\n<p>\u5229\u7528Redis\u9032\u884c\u5716\u5f62\u6027\u80fd\u7684\u63d0\u5347\uff0c\u4e0d\u50c5\u80fd\u5920\u63d0\u9ad8\u7528\u6236\u9ad4\u9a57\uff0c\u9084\u80fd\u6e1b\u5c11\u4f3a\u670d\u5668\u7684\u8ca0\u64d4\u3002\u901a\u904e\u5408\u7406\u7684\u7de9\u5b58\u7b56\u7565\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u5716\u5f62\u6578\u64da\u7684\u5b58\u53d6\uff0c\u5f9e\u800c\u5be6\u73fe\u9ad8\u6548\u7684\u6578\u64da\u8655\u7406\u3002\u5728\u9999\u6e2f\u7684VPS\u74b0\u5883\u4e2d\uff0c\u9078\u64c7\u5408\u9069\u7684\u4f3a\u670d\u5668\u914d\u7f6e\u548c\u512a\u5316\u7b56\u7565\uff0c\u5c07\u9032\u4e00\u6b65\u63d0\u5347\u61c9\u7528\u7684\u6574\u9ad4\u6027\u80fd\u3002<\/p>\n<p>\u5982\u9700\u4e86\u89e3\u66f4\u591a\u95dc\u65bc<em><a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a><\/em>\u548c<em><a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a><\/em>\u7684\u8cc7\u8a0a\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u5229\u7528Redis\u7de9\u5b58\u63d0\u5347\u7d05\u8272\u7279\u8272\u5716\u5f62\u6027\u80fd\uff0c\u5be6\u73fe\u66f4\u5feb\u7684\u6578\u64da\u52a0\u8f09\u548c\u66f4\u6d41\u66a2\u7684\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-159725","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/159725","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"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=159725"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/159725\/revisions"}],"predecessor-version":[{"id":159726,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/159725\/revisions\/159726"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=159725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=159725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=159725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}