{"id":1069643,"date":"2025-01-08T10:50:49","date_gmt":"2025-01-08T02:50:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1069643.html"},"modified":"2025-01-08T10:50:52","modified_gmt":"2025-01-08T02:50:52","slug":"python%e7%94%b5%e5%95%86%e9%a1%b9%e7%9b%ae%e5%a6%82%e4%bd%95%e5%a4%84%e7%90%86%e7%bc%93%e5%ad%98-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1069643.html","title":{"rendered":"python\u7535\u5546\u9879\u76ee\u5982\u4f55\u5904\u7406\u7f13\u5b58"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100609\/2b951486-49e5-4e4c-b5b6-4217766d7d24.webp\" alt=\"python\u7535\u5546\u9879\u76ee\u5982\u4f55\u5904\u7406\u7f13\u5b58\" \/><\/p>\n<p><p> <strong>Python\u7535\u5546\u9879\u76ee\u5904\u7406\u7f13\u5b58\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Redis\u3001Memcached\u3001Django\u7f13\u5b58\u6846\u67b6\u7b49\u3002<\/strong> \u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u91cd\u70b9\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Redis\u6765\u5904\u7406\u7f13\u5b58\u3002Redis\u662f\u4e00\u79cd\u5f00\u6e90\u7684\u3001\u57fa\u4e8e\u5185\u5b58\u7684\u6570\u636e\u7ed3\u6784\u5b58\u50a8\u7cfb\u7edf\uff0c\u5b83\u53ef\u4ee5\u7528\u4f5c\u6570\u636e\u5e93\u3001\u7f13\u5b58\u548c\u6d88\u606f\u4ee3\u7406\u3002Redis\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u54c8\u5e0c\u3001\u5217\u8868\u3001\u96c6\u5408\u548c\u6709\u5e8f\u96c6\u5408\uff0c\u8fd9\u4f7f\u5f97\u5b83\u6210\u4e3a\u5904\u7406\u7f13\u5b58\u7684\u7406\u60f3\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><p>\u5904\u7406\u7f13\u5b58\u7684\u5173\u952e\u5728\u4e8e\u63d0\u9ad8\u8bfb\u53d6\u901f\u5ea6\u3001\u51cf\u5c11\u6570\u636e\u5e93\u8bbf\u95ee\u3001\u63d0\u9ad8\u7cfb\u7edf\u7684\u6574\u4f53\u6027\u80fd\u3002\u5728\u7535\u5546\u9879\u76ee\u4e2d\uff0c\u7f13\u5b58\u53ef\u4ee5\u7528\u4e8e\u5b58\u50a8\u5546\u54c1\u4fe1\u606f\u3001\u7528\u6237\u4f1a\u8bdd\u4fe1\u606f\u3001\u8d2d\u7269\u8f66\u6570\u636e\u7b49\uff0c\u4ee5\u4fbf\u5728\u540e\u7eed\u8bf7\u6c42\u4e2d\u5feb\u901f\u83b7\u53d6\u8fd9\u4e9b\u6570\u636e\uff0c\u4ece\u800c\u63d0\u9ad8\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001REDIS\u662f\u4ec0\u4e48\u4ee5\u53ca\u5b83\u7684\u57fa\u672c\u4f7f\u7528<\/h2>\n<\/p>\n<p><p>Redis\uff08Remote Dictionary Server\uff09\u662f\u4e00\u79cd\u9ad8\u6027\u80fd\u7684\u952e\u503c\u5bf9\u5b58\u50a8\u6570\u636e\u5e93\uff0c\u5e7f\u6cdb\u7528\u4e8e\u7f13\u5b58\u3001\u4f1a\u8bdd\u5b58\u50a8\u3001\u6d88\u606f\u961f\u5217\u7b49\u573a\u666f\u3002\u5176\u7279\u70b9\u5305\u62ec\u9ad8\u6027\u80fd\u3001\u4e30\u5bcc\u7684\u6570\u636e\u7ed3\u6784\u3001\u6301\u4e45\u5316\u3001\u590d\u5236\u548c\u9ad8\u53ef\u7528\u6027\u7b49\u3002<\/p>\n<\/p>\n<p><h3>1. Redis\u5b89\u88c5\u4e0e\u57fa\u672c\u914d\u7f6e<\/h3>\n<\/p>\n<p><p>\u5b89\u88c5Redis\u975e\u5e38\u7b80\u5355\uff0c\u53ef\u4ee5\u901a\u8fc7\u5305\u7ba1\u7406\u5668\u6216\u8005\u4ece\u6e90\u7801\u7f16\u8bd1\u5b89\u88c5\u3002\u4ee5\u4e0b\u662f\u901a\u8fc7\u5305\u7ba1\u7406\u5668\u5b89\u88c5Redis\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\"># Ubuntu\u7cfb\u7edf<\/p>\n<p>sudo apt-get update<\/p>\n<p>sudo apt-get install redis-server<\/p>\n<h2><strong>CentOS\u7cfb\u7edf<\/strong><\/h2>\n<p>sudo yum install epel-release<\/p>\n<p>sudo yum install redis<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u542f\u52a8Redis\u670d\u52a1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo service redis-server start<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cRedis\u76d1\u542c6379\u7aef\u53e3\u3002\u53ef\u4ee5\u901a\u8fc7<code>redis-cli<\/code>\u547d\u4ee4\u884c\u5de5\u5177\u4e0eRedis\u4ea4\u4e92\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">redis-cli<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. Redis\u57fa\u672c\u547d\u4ee4<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684Redis\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\"># \u8bbe\u7f6e\u952e\u503c\u5bf9<\/p>\n<p>SET key value<\/p>\n<h2><strong>\u83b7\u53d6\u952e\u5bf9\u5e94\u7684\u503c<\/strong><\/h2>\n<p>GET key<\/p>\n<h2><strong>\u5220\u9664\u952e<\/strong><\/h2>\n<p>DEL key<\/p>\n<h2><strong>\u8bbe\u7f6e\u952e\u503c\u5bf9\u5e76\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\uff08\u5355\u4f4d\uff1a\u79d2\uff09<\/strong><\/h2>\n<p>SETEX key seconds value<\/p>\n<h2><strong>\u68c0\u67e5\u952e\u662f\u5426\u5b58\u5728<\/strong><\/h2>\n<p>EXISTS key<\/p>\n<h2><strong>\u83b7\u53d6\u952e\u7684\u5269\u4f59\u8fc7\u671f\u65f6\u95f4<\/strong><\/h2>\n<p>TTL key<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u5728\u7535\u5546\u9879\u76ee\u4e2d\u4f7f\u7528Redis\u5904\u7406\u7f13\u5b58<\/h2>\n<\/p>\n<p><p>\u5728\u7535\u5546\u9879\u76ee\u4e2d\uff0c\u7f13\u5b58\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7cfb\u7edf\u6027\u80fd\uff0c\u51cf\u5c11\u6570\u636e\u5e93\u8d1f\u8f7d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u7f13\u5b58\u573a\u666f\u53ca\u5176\u5b9e\u73b0\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>1. \u5546\u54c1\u4fe1\u606f\u7f13\u5b58<\/h3>\n<\/p>\n<p><p>\u7535\u5546\u7f51\u7ad9\u7684\u5546\u54c1\u4fe1\u606f\u901a\u5e38\u662f\u8bfb\u53d6\u9891\u7387\u975e\u5e38\u9ad8\u7684\u6570\u636e\uff0c\u5c06\u5176\u7f13\u5b58\u8d77\u6765\u53ef\u4ee5\u5927\u5e45\u63d0\u9ad8\u8bfb\u53d6\u901f\u5ea6\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Redis\u7f13\u5b58\u5546\u54c1\u4fe1\u606f\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import redis<\/p>\n<h2><strong>\u8fde\u63a5\u5230Redis\u670d\u52a1\u5668<\/strong><\/h2>\n<p>redis_client = redis.StrictRedis(host=&#39;localhost&#39;, port=6379, db=0)<\/p>\n<p>def get_product_info(product_id):<\/p>\n<p>    # \u4ece\u7f13\u5b58\u4e2d\u83b7\u53d6\u5546\u54c1\u4fe1\u606f<\/p>\n<p>    product_info = redis_client.get(f&#39;product:{product_id}&#39;)<\/p>\n<p>    if product_info is None:<\/p>\n<p>        # \u5982\u679c\u7f13\u5b58\u4e2d\u6ca1\u6709\u5546\u54c1\u4fe1\u606f\uff0c\u4ece\u6570\u636e\u5e93\u4e2d\u83b7\u53d6<\/p>\n<p>        product_info = fetch_product_info_from_db(product_id)<\/p>\n<p>        # \u5c06\u5546\u54c1\u4fe1\u606f\u5b58\u5165\u7f13\u5b58\uff0c\u5e76\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\uff08\u5355\u4f4d\uff1a\u79d2\uff09<\/p>\n<p>        redis_client.setex(f&#39;product:{product_id}&#39;, 3600, product_info)<\/p>\n<p>    return product_info<\/p>\n<p>def fetch_product_info_from_db(product_id):<\/p>\n<p>    # \u4ece\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u5546\u54c1\u4fe1\u606f\u7684\u903b\u8f91<\/p>\n<p>    # \u5047\u8bbe\u8fd4\u56de\u4e00\u4e2aJSON\u5b57\u7b26\u4e32<\/p>\n<p>    return &#39;{&quot;id&quot;: 1, &quot;name&quot;: &quot;Sample Product&quot;, &quot;price&quot;: 100}&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u7528\u6237\u4f1a\u8bdd\u7f13\u5b58<\/h3>\n<\/p>\n<p><p>\u5728\u7535\u5546\u9879\u76ee\u4e2d\uff0c\u7528\u6237\u4f1a\u8bdd\u4fe1\u606f\u662f\u53e6\u4e00\u4e2a\u9700\u8981\u9891\u7e41\u8bfb\u53d6\u7684\u6570\u636e\u3002\u53ef\u4ee5\u5c06\u7528\u6237\u4f1a\u8bdd\u4fe1\u606f\u5b58\u50a8\u5728Redis\u4e2d\uff0c\u63d0\u9ad8\u8bfb\u53d6\u901f\u5ea6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import redis<\/p>\n<h2><strong>\u8fde\u63a5\u5230Redis\u670d\u52a1\u5668<\/strong><\/h2>\n<p>redis_client = redis.StrictRedis(host=&#39;localhost&#39;, port=6379, db=1)<\/p>\n<p>def get_user_session(user_id):<\/p>\n<p>    # \u4ece\u7f13\u5b58\u4e2d\u83b7\u53d6\u7528\u6237\u4f1a\u8bdd\u4fe1\u606f<\/p>\n<p>    session_data = redis_client.get(f&#39;session:{user_id}&#39;)<\/p>\n<p>    if session_data is None:<\/p>\n<p>        # \u5982\u679c\u7f13\u5b58\u4e2d\u6ca1\u6709\u4f1a\u8bdd\u4fe1\u606f\uff0c\u4ece\u6570\u636e\u5e93\u6216\u5176\u4ed6\u5b58\u50a8\u4e2d\u83b7\u53d6<\/p>\n<p>        session_data = fetch_session_from_db(user_id)<\/p>\n<p>        # \u5c06\u4f1a\u8bdd\u4fe1\u606f\u5b58\u5165\u7f13\u5b58\uff0c\u5e76\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\uff08\u5355\u4f4d\uff1a\u79d2\uff09<\/p>\n<p>        redis_client.setex(f&#39;session:{user_id}&#39;, 3600, session_data)<\/p>\n<p>    return session_data<\/p>\n<p>def fetch_session_from_db(user_id):<\/p>\n<p>    # \u4ece\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u4f1a\u8bdd\u4fe1\u606f\u7684\u903b\u8f91<\/p>\n<p>    # \u5047\u8bbe\u8fd4\u56de\u4e00\u4e2aJSON\u5b57\u7b26\u4e32<\/p>\n<p>    return &#39;{&quot;user_id&quot;: 1, &quot;session_data&quot;: &quot;sample_session_data&quot;}&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u8d2d\u7269\u8f66\u6570\u636e\u7f13\u5b58<\/h3>\n<\/p>\n<p><p>\u8d2d\u7269\u8f66\u6570\u636e\u5bf9\u4e8e\u6bcf\u4e2a\u7528\u6237\u6765\u8bf4\u90fd\u662f\u72ec\u7acb\u7684\uff0c\u5c06\u5176\u7f13\u5b58\u8d77\u6765\u53ef\u4ee5\u52a0\u5feb\u8bbf\u95ee\u901f\u5ea6\uff0c\u5c24\u5176\u662f\u5728\u7528\u6237\u9891\u7e41\u4fee\u6539\u8d2d\u7269\u8f66\u5185\u5bb9\u65f6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import redis<\/p>\n<h2><strong>\u8fde\u63a5\u5230Redis\u670d\u52a1\u5668<\/strong><\/h2>\n<p>redis_client = redis.StrictRedis(host=&#39;localhost&#39;, port=6379, db=2)<\/p>\n<p>def get_cart_data(user_id):<\/p>\n<p>    # \u4ece\u7f13\u5b58\u4e2d\u83b7\u53d6\u8d2d\u7269\u8f66\u6570\u636e<\/p>\n<p>    cart_data = redis_client.get(f&#39;cart:{user_id}&#39;)<\/p>\n<p>    if cart_data is None:<\/p>\n<p>        # \u5982\u679c\u7f13\u5b58\u4e2d\u6ca1\u6709\u8d2d\u7269\u8f66\u6570\u636e\uff0c\u4ece\u6570\u636e\u5e93\u6216\u5176\u4ed6\u5b58\u50a8\u4e2d\u83b7\u53d6<\/p>\n<p>        cart_data = fetch_cart_from_db(user_id)<\/p>\n<p>        # \u5c06\u8d2d\u7269\u8f66\u6570\u636e\u5b58\u5165\u7f13\u5b58\uff0c\u5e76\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4\uff08\u5355\u4f4d\uff1a\u79d2\uff09<\/p>\n<p>        redis_client.setex(f&#39;cart:{user_id}&#39;, 3600, cart_data)<\/p>\n<p>    return cart_data<\/p>\n<p>def fetch_cart_from_db(user_id):<\/p>\n<p>    # \u4ece\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u8d2d\u7269\u8f66\u6570\u636e\u7684\u903b\u8f91<\/p>\n<p>    # \u5047\u8bbe\u8fd4\u56de\u4e00\u4e2aJSON\u5b57\u7b26\u4e32<\/p>\n<p>    return &#39;{&quot;user_id&quot;: 1, &quot;items&quot;: [{&quot;product_id&quot;: 1, &quot;quantity&quot;: 2}]}&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u7f13\u5b58\u5931\u6548\u7b56\u7565<\/h2>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u7f13\u5b58\u65f6\uff0c\u7f13\u5b58\u5931\u6548\u7b56\u7565\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u5e38\u89c1\u7684\u7f13\u5b58\u5931\u6548\u7b56\u7565\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a<\/p>\n<\/p>\n<p><h3>1. \u57fa\u4e8e\u65f6\u95f4\u7684\u5931\u6548\u7b56\u7565<\/h3>\n<\/p>\n<p><p>\u57fa\u4e8e\u65f6\u95f4\u7684\u5931\u6548\u7b56\u7565\u662f\u6700\u5e38\u89c1\u7684\u7f13\u5b58\u5931\u6548\u7b56\u7565\uff0c\u5373\u8bbe\u7f6e\u7f13\u5b58\u7684\u8fc7\u671f\u65f6\u95f4\uff0c\u5f53\u7f13\u5b58\u8d85\u8fc7\u8fc7\u671f\u65f6\u95f4\u540e\u81ea\u52a8\u5931\u6548\u3002Redis\u7684<code>SETEX<\/code>\u547d\u4ee4\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><h3>2. \u57fa\u4e8e\u5bb9\u91cf\u7684\u5931\u6548\u7b56\u7565<\/h3>\n<\/p>\n<p><p>\u57fa\u4e8e\u5bb9\u91cf\u7684\u5931\u6548\u7b56\u7565\u662f\u6307\u5f53\u7f13\u5b58\u5bb9\u91cf\u8d85\u8fc7\u8bbe\u5b9a\u9608\u503c\u65f6\uff0c\u6839\u636e\u4e00\u5b9a\u7684\u7b56\u7565\uff08\u5982LRU\u3001LFU\u7b49\uff09\u6dd8\u6c70\u90e8\u5206\u7f13\u5b58\u6570\u636e\u3002Redis\u652f\u6301\u591a\u79cd\u6dd8\u6c70\u7b56\u7565\uff0c\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6e\u6587\u4ef6\u8fdb\u884c\u8bbe\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>3. \u4e3b\u52a8\u5931\u6548\u7b56\u7565<\/h3>\n<\/p>\n<p><p>\u4e3b\u52a8\u5931\u6548\u7b56\u7565\u662f\u6307\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4e3b\u52a8\u5220\u9664\u7f13\u5b58\u6570\u636e\u3002\u4f8b\u5982\uff0c\u5f53\u5546\u54c1\u4fe1\u606f\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u53ef\u4ee5\u4e3b\u52a8\u5220\u9664\u5bf9\u5e94\u7684\u7f13\u5b58\u6570\u636e\uff0c\u4ee5\u4fdd\u8bc1\u7f13\u5b58\u6570\u636e\u7684\u4e00\u81f4\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def update_product_info(product_id, new_info):<\/p>\n<p>    # \u66f4\u65b0\u6570\u636e\u5e93\u4e2d\u7684\u5546\u54c1\u4fe1\u606f<\/p>\n<p>    update_product_info_in_db(product_id, new_info)<\/p>\n<p>    # \u4e3b\u52a8\u5220\u9664\u7f13\u5b58\u4e2d\u7684\u5546\u54c1\u4fe1\u606f<\/p>\n<p>    redis_client.delete(f&#39;product:{product_id}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u7f13\u5b58\u4e00\u81f4\u6027\u95ee\u9898<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7f13\u5b58\u4e00\u81f4\u6027\u95ee\u9898\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u6311\u6218\u3002\u4e3a\u4e86\u4fdd\u8bc1\u7f13\u5b58\u6570\u636e\u4e0e\u6570\u636e\u5e93\u6570\u636e\u7684\u4e00\u81f4\u6027\uff0c\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h3>1. \u8bfb\u5199\u7a7f\u900f<\/h3>\n<\/p>\n<p><p>\u8bfb\u5199\u7a7f\u900f\u662f\u6307\u5728\u8bfb\u53d6\u6570\u636e\u65f6\uff0c\u5982\u679c\u7f13\u5b58\u4e2d\u6ca1\u6709\u6570\u636e\uff0c\u5219\u76f4\u63a5\u4ece\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\uff0c\u5e76\u5c06\u8bfb\u53d6\u5230\u7684\u6570\u636e\u5199\u5165\u7f13\u5b58\u3002\u5728\u5199\u5165\u6570\u636e\u65f6\uff0c\u540c\u65f6\u66f4\u65b0\u6570\u636e\u5e93\u548c\u7f13\u5b58\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def get_product_info(product_id):<\/p>\n<p>    product_info = redis_client.get(f&#39;product:{product_id}&#39;)<\/p>\n<p>    if product_info is None:<\/p>\n<p>        product_info = fetch_product_info_from_db(product_id)<\/p>\n<p>        redis_client.setex(f&#39;product:{product_id}&#39;, 3600, product_info)<\/p>\n<p>    return product_info<\/p>\n<p>def update_product_info(product_id, new_info):<\/p>\n<p>    update_product_info_in_db(product_id, new_info)<\/p>\n<p>    redis_client.setex(f&#39;product:{product_id}&#39;, 3600, new_info)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u7f13\u5b58\u9884\u70ed<\/h3>\n<\/p>\n<p><p>\u7f13\u5b58\u9884\u70ed\u662f\u6307\u5728\u7cfb\u7edf\u542f\u52a8\u65f6\uff0c\u4e3b\u52a8\u5c06\u90e8\u5206\u70ed\u70b9\u6570\u636e\u52a0\u8f7d\u5230\u7f13\u5b58\u4e2d\uff0c\u4ee5\u51cf\u5c11\u7f13\u5b58\u672a\u547d\u4e2d\u5e26\u6765\u7684\u6027\u80fd\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5728\u7cfb\u7edf\u542f\u52a8\u65f6\u5c06\u70ed\u95e8\u5546\u54c1\u4fe1\u606f\u52a0\u8f7d\u5230\u7f13\u5b58\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def preload_hot_products():<\/p>\n<p>    hot_products = fetch_hot_products_from_db()<\/p>\n<p>    for product in hot_products:<\/p>\n<p>        redis_client.setex(f&#39;product:{product[&quot;id&quot;]}&#39;, 3600, product)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u7f13\u5b58\u96ea\u5d29<\/h3>\n<\/p>\n<p><p>\u7f13\u5b58\u96ea\u5d29\u662f\u6307\u5728\u67d0\u4e00\u65f6\u523b\u5927\u91cf\u7f13\u5b58\u540c\u65f6\u5931\u6548\uff0c\u5bfc\u81f4\u5927\u91cf\u8bf7\u6c42\u76f4\u63a5\u6253\u5230\u6570\u636e\u5e93\uff0c\u9020\u6210\u6570\u636e\u5e93\u538b\u529b\u8fc7\u5927\u751a\u81f3\u5b95\u673a\u3002\u4e3a\u4e86\u907f\u514d\u7f13\u5b58\u96ea\u5d29\uff0c\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u63aa\u65bd\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u8bbe\u7f6e\u7f13\u5b58\u8fc7\u671f\u65f6\u95f4\u7684\u968f\u673a\u503c<\/strong>\uff1a\u5728\u8bbe\u7f6e\u7f13\u5b58\u8fc7\u671f\u65f6\u95f4\u65f6\uff0c\u589e\u52a0\u4e00\u4e2a\u968f\u673a\u503c\uff0c\u907f\u514d\u5927\u91cf\u7f13\u5b58\u540c\u65f6\u5931\u6548\u3002<\/li>\n<li><strong>\u591a\u7ea7\u7f13\u5b58<\/strong>\uff1a\u5728\u5e94\u7528\u5c42\u5f15\u5165\u591a\u7ea7\u7f13\u5b58\uff0c\u5982\u672c\u5730\u7f13\u5b58\u548c\u5206\u5e03\u5f0f\u7f13\u5b58\u7ed3\u5408\u4f7f\u7528\uff0c\u51cf\u5c11\u5bf9\u5355\u4e00\u7f13\u5b58\u7684\u4f9d\u8d56\u3002<\/li>\n<li><strong>\u964d\u7ea7\u7b56\u7565<\/strong>\uff1a\u5728\u7f13\u5b58\u5931\u6548\u65f6\uff0c\u91c7\u7528\u964d\u7ea7\u7b56\u7565\uff0c\u5982\u8fd4\u56de\u9ed8\u8ba4\u6570\u636e\u6216\u9650\u6d41\uff0c\u907f\u514d\u6570\u636e\u5e93\u538b\u529b\u8fc7\u5927\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>def set_cache_with_random_expiry(key, value, base_expiry):<\/p>\n<p>    expiry_time = base_expiry + random.randint(0, 300)<\/p>\n<p>    redis_client.setex(key, expiry_time, value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u4f7f\u7528Django\u7f13\u5b58\u6846\u67b6<\/h2>\n<\/p>\n<p><p>\u5728Django\u9879\u76ee\u4e2d\uff0cDjango\u81ea\u5e26\u7684\u7f13\u5b58\u6846\u67b6\u53ef\u4ee5\u65b9\u4fbf\u5730\u96c6\u6210Redis\u7f13\u5b58\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h3>1. \u914d\u7f6eRedis\u7f13\u5b58<\/h3>\n<\/p>\n<p><p>\u5728<code>settings.py<\/code>\u6587\u4ef6\u4e2d\uff0c\u6dfb\u52a0Redis\u7f13\u5b58\u914d\u7f6e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">CACHES = {<\/p>\n<p>    &#39;default&#39;: {<\/p>\n<p>        &#39;BACKEND&#39;: &#39;django_redis.cache.RedisCache&#39;,<\/p>\n<p>        &#39;LOCATION&#39;: &#39;redis:\/\/127.0.0.1:6379\/1&#39;,<\/p>\n<p>        &#39;OPTIONS&#39;: {<\/p>\n<p>            &#39;CLIENT_CLASS&#39;: &#39;django_redis.client.DefaultClient&#39;,<\/p>\n<p>        },<\/p>\n<p>        &#39;KEY_PREFIX&#39;: &#39;my_project&#39;<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4f7f\u7528\u7f13\u5b58\u88c5\u9970\u5668<\/h3>\n<\/p>\n<p><p>Django\u63d0\u4f9b\u4e86\u7f13\u5b58\u88c5\u9970\u5668\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u7f13\u5b58\u89c6\u56fe\u7684\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.views.decorators.cache import cache_page<\/p>\n<p>from django.shortcuts import render<\/p>\n<p>@cache_page(60 * 15)<\/p>\n<p>def product_det<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l(request, product_id):<\/p>\n<p>    product_info = get_product_info(product_id)<\/p>\n<p>    return render(request, &#39;product_detail.html&#39;, {&#39;product&#39;: product_info})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u624b\u52a8\u64cd\u4f5c\u7f13\u5b58<\/h3>\n<\/p>\n<p><p>Django\u7684\u7f13\u5b58\u6846\u67b6\u8fd8\u63d0\u4f9b\u4e86\u624b\u52a8\u64cd\u4f5c\u7f13\u5b58\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.core.cache import cache<\/p>\n<p>def get_product_info(product_id):<\/p>\n<p>    product_info = cache.get(f&#39;product:{product_id}&#39;)<\/p>\n<p>    if product_info is None:<\/p>\n<p>        product_info = fetch_product_info_from_db(product_id)<\/p>\n<p>        cache.set(f&#39;product:{product_id}&#39;, product_info, timeout=3600)<\/p>\n<p>    return product_info<\/p>\n<p>def update_product_info(product_id, new_info):<\/p>\n<p>    update_product_info_in_db(product_id, new_info)<\/p>\n<p>    cache.set(f&#39;product:{product_id}&#39;, new_info, timeout=3600)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516d\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u7535\u5546\u9879\u76ee\u4e2d\uff0c\u5408\u7406\u4f7f\u7528\u7f13\u5b58\u6280\u672f\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7cfb\u7edf\u6027\u80fd\uff0c\u51cf\u5c11\u6570\u636e\u5e93\u538b\u529b\u3002Redis\u4f5c\u4e3a\u4e00\u79cd\u9ad8\u6027\u80fd\u7684\u7f13\u5b58\u89e3\u51b3\u65b9\u6848\uff0c\u5177\u6709\u4e30\u5bcc\u7684\u6570\u636e\u7ed3\u6784\u548c\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u7f13\u5b58\u573a\u666f\u3002\u901a\u8fc7\u5408\u7406\u8bbe\u8ba1\u7f13\u5b58\u7b56\u7565\u3001\u5904\u7406\u7f13\u5b58\u5931\u6548\u548c\u4e00\u81f4\u6027\u95ee\u9898\uff0c\u53ef\u4ee5\u4fdd\u8bc1\u7cfb\u7edf\u7684\u7a33\u5b9a\u6027\u548c\u9ad8\u6548\u6027\u3002\u5e0c\u671b\u672c\u6587\u7684\u4ecb\u7ecd\u80fd\u5bf9\u60a8\u5728\u7535\u5546\u9879\u76ee\u4e2d\u5904\u7406\u7f13\u5b58\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>1. \u5728Python\u7535\u5546\u9879\u76ee\u4e2d\uff0c\u4f7f\u7528\u54ea\u4e9b\u7f13\u5b58\u7b56\u7565\u6700\u4e3a\u6709\u6548\uff1f<\/strong><br \/>\u5728Python\u7535\u5546\u9879\u76ee\u4e2d\uff0c\u5e38\u7528\u7684\u7f13\u5b58\u7b56\u7565\u5305\u62ec\u5185\u5b58\u7f13\u5b58\u3001\u5206\u5e03\u5f0f\u7f13\u5b58\u548c\u9875\u9762\u7f13\u5b58\u7b49\u3002\u5185\u5b58\u7f13\u5b58\uff08\u5982\u4f7f\u7528Redis\u6216Memcached\uff09\u9002\u5408\u5b58\u50a8\u9891\u7e41\u8bbf\u95ee\u7684\u6570\u636e\uff0c\u5982\u7528\u6237\u4f1a\u8bdd\u548c\u5546\u54c1\u4fe1\u606f\u3002\u5206\u5e03\u5f0f\u7f13\u5b58\u80fd\u591f\u652f\u6301\u66f4\u5927\u89c4\u6a21\u7684\u5e94\u7528\uff0c\u9002\u5408\u5904\u7406\u5927\u91cf\u5e76\u53d1\u8bf7\u6c42\u3002\u9875\u9762\u7f13\u5b58\u5219\u9002\u7528\u4e8e\u9759\u6001\u9875\u9762\u7684\u5feb\u901f\u52a0\u8f7d\uff0c\u80fd\u591f\u663e\u8457\u63d0\u9ad8\u7528\u6237\u4f53\u9a8c\u548c\u7f51\u7ad9\u6027\u80fd\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u7f13\u5b58\u5de5\u5177\u6765\u6ee1\u8db3\u7535\u5546\u9879\u76ee\u7684\u9700\u6c42\uff1f<\/strong><br \/>\u9009\u62e9\u7f13\u5b58\u5de5\u5177\u65f6\uff0c\u9700\u8981\u8003\u8651\u9879\u76ee\u7684\u89c4\u6a21\u3001\u7528\u6237\u8bbf\u95ee\u91cf\u3001\u6570\u636e\u4e00\u81f4\u6027\u8981\u6c42\u7b49\u56e0\u7d20\u3002\u5bf9\u4e8e\u5c0f\u578b\u9879\u76ee\uff0c\u53ef\u4ee5\u9009\u62e9\u7b80\u5355\u7684\u5185\u5b58\u7f13\u5b58\u5de5\u5177\u5982Flask-Caching\u6216Django\u7684\u5185\u7f6e\u7f13\u5b58\u7cfb\u7edf\u3002\u5bf9\u4e8e\u5927\u578b\u7535\u5546\u5e73\u53f0\uff0cRedis\u548cMemcached\u662f\u70ed\u95e8\u9009\u62e9\uff0c\u5b83\u4eec\u63d0\u4f9b\u4e86\u9ad8\u6027\u80fd\u548c\u9ad8\u53ef\u7528\u6027\u3002\u8fd8\u9700\u5173\u6ce8\u793e\u533a\u652f\u6301\u548c\u6587\u6863\u5b8c\u5584\u7a0b\u5ea6\uff0c\u786e\u4fdd\u5728\u9047\u5230\u95ee\u9898\u65f6\u80fd\u591f\u5f97\u5230\u53ca\u65f6\u7684\u5e2e\u52a9\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u76d1\u63a7\u548c\u4f18\u5316\u7f13\u5b58\u6548\u679c\uff0c\u4ee5\u63d0\u5347\u7535\u5546\u9879\u76ee\u7684\u6027\u80fd\uff1f<\/strong><br \/>\u76d1\u63a7\u7f13\u5b58\u6548\u679c\u53ef\u4ee5\u901a\u8fc7\u5206\u6790\u7f13\u5b58\u547d\u4e2d\u7387\u3001\u5ef6\u8fdf\u548c\u5b58\u50a8\u4f7f\u7528\u60c5\u51b5\u7b49\u6307\u6807\u6765\u8fdb\u884c\u3002\u4f7f\u7528\u5de5\u5177\u5982Grafana\u548cPrometheus\uff0c\u53ef\u4ee5\u5b9e\u65f6\u8ddf\u8e2a\u8fd9\u4e9b\u6027\u80fd\u6307\u6807\u3002\u4f18\u5316\u7f13\u5b58\u6548\u679c\u7684\u65b9\u6cd5\u5305\u62ec\u5b9a\u671f\u6e05\u7406\u8fc7\u671f\u6570\u636e\u3001\u5408\u7406\u8bbe\u7f6e\u7f13\u5b58\u8fc7\u671f\u65f6\u95f4\uff0c\u4ee5\u53ca\u5bf9\u70ed\u95e8\u5546\u54c1\u6216\u9875\u9762\u8fdb\u884c\u9884\u70ed\uff0c\u786e\u4fdd\u7528\u6237\u8bbf\u95ee\u65f6\u80fd\u591f\u5feb\u901f\u54cd\u5e94\u3002\u901a\u8fc7\u4e0d\u65ad\u7684\u76d1\u6d4b\u548c\u8c03\u6574\uff0c\u53ef\u4ee5\u786e\u4fdd\u7535\u5546\u9879\u76ee\u5728\u9ad8\u6d41\u91cf\u65f6\u6bb5\u4f9d\u7136\u4fdd\u6301\u826f\u597d\u7684\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u7535\u5546\u9879\u76ee\u5904\u7406\u7f13\u5b58\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Redis\u3001Memcached\u3001Django\u7f13\u5b58\u6846\u67b6\u7b49\u3002 \u5728\u8fd9\u7bc7\u6587\u7ae0 [&hellip;]","protected":false},"author":3,"featured_media":1069650,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1069643"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1069643"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1069643\/revisions"}],"predecessor-version":[{"id":1069651,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1069643\/revisions\/1069651"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1069650"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1069643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1069643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1069643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}