{"id":1089,"date":"2023-04-27T16:42:31","date_gmt":"2023-04-27T08:42:31","guid":{"rendered":"https:\/\/white-night.club\/?p=1089"},"modified":"2023-11-09T11:07:55","modified_gmt":"2023-11-09T03:07:55","slug":"translate1","status":"publish","type":"post","link":"https:\/\/white-night.club\/index.php\/2023\/04\/27\/translate1\/","title":{"rendered":"\u82f1\u7ffb\u4e2d\uff1ahttps:\/\/redis.io\/commands\/set"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>SET<\/strong>\u6307\u4ee4<\/h2>\n\n\n\n<p><strong>\u683c\u5f0f<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SET key value &#91;NX|XX] &#91;GET] &#91;EX seconds|PX milliseconds|EXAT unix-time-seconds|PXAt unix-time-millisecond|KEEPTTL]<\/code><\/pre>\n\n\n\n<p><strong>\u53ef\u7528\u7248\u672c<\/strong>\uff1a1.0.0<\/p>\n\n\n\n<p><strong>\u65f6\u95f4\u590d\u6742\u5ea6<\/strong>\uff1aO(1)<\/p>\n\n\n\n<p><strong>\u8bbf\u95ee\u63a7\u5236\u5217\u8868<\/strong>(ACL:Acess Control List)\u7c7b\u522b\uff1a@write\uff0c@string\uff0c@slow<\/p>\n\n\n\n<p>SET\u6307\u4ee4\u901a\u8fc7\u8bbe\u7f6ekey\u6765\u4fdd\u5b58\u4e00\u4e2astring\u7c7b\u578b\u7684\u503c\u3002\u5373\u4fbfkey\u5df2\u7ecf\u4fdd\u5b58\u4e86\u67d0\u4e2a\u503c\uff0cSET\u6307\u4ee4\u4e5f\u4f1a\u76f4\u63a5\u5c06\u5176\u8986\u5199\u5e76\u5ffd\u7565\u539f\u6765\u503c\u7684\u7c7b\u578b\u3002\u5e76\u4e14\u5728SET\u6307\u4ee4\u6267\u884c\u6210\u529f\u7684\u60c5\u51b5\u4e0b\uff0c\u8be5\u952e\u5728\u6b64\u4e4b\u524d\u8bbe\u7f6e\u7684\u7684TTL(Time To Live)\u90fd\u5c06\u5931\u6548\u3002<\/p>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u53ef\u7528\u9009\u9879<\/strong><\/h2>\n\n\n\n<p>SET\u6307\u4ee4\u652f\u6301\u4e00\u7cfb\u5217\u4fee\u6539\u5176\u884c\u4e3a\u7684\u9009\u9879\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul>\n<li><strong>EX<\/strong> <em>seconds<\/em>&#8212; \u8bbe\u7f6e\u4e00\u4e2a\u7279\u5b9a\u7684\u8fc7\u671f\u65f6\u95f4\uff0c\u8be5\u8fc7\u671f\u65f6\u95f4\u4ee5\u79d2\u4e3a\u5355\u4f4d\u3002<\/li>\n\n\n\n<li><strong>PX<\/strong> <em>milliseconds<\/em> &#8212; \u8bbe\u7f6e\u4e00\u4e2a\u7279\u5b9a\u7684\u8fc7\u671f\u65f6\u95f4\uff0c\u8be5\u8fc7\u671f\u65f6\u95f4\u4ee5\u6beb\u79d2\u4e3a\u5355\u4f4d\u3002<\/li>\n\n\n\n<li><strong>EXAT <\/strong><em>timestamp-seconds<\/em> &#8212; \u5c06\u8bbe\u7f6e\u4ee5UNIX\u65f6\u95f4\u4e3a\u51c6\u7684\u8fc7\u671f\u65f6\u95f4\uff0c\u5355\u4f4d\u4e3a\u79d2\u3002<\/li>\n\n\n\n<li><strong>PXAT <\/strong><em>timestamp-millisecond <\/em>&#8212; \u5c06\u8bbe\u7f6e\u4ee5UNIX\u65f6\u95f4\u4e3a\u51c6\u7684\u8fc7\u671f\u65f6\u95f4\uff0c\u5355\u4f4d\u4e3a\u6beb\u79d2\u3002<\/li>\n\n\n\n<li><strong>NX <\/strong>&#8212; \u5f53\u952e\u4e0d\u5b58\u5728\u65f6\u6267\u884cSET\u6307\u4ee4\u3002<\/li>\n\n\n\n<li><strong>XX <\/strong>&#8212; \u5f53\u952e\u5b58\u5728\u65f6\u6267\u884cSET\u6307\u4ee4\u3002<\/li>\n\n\n\n<li><strong>KEEPTTL <\/strong>&#8212; \u4fdd\u5b58TTL\u65f6\u95f4\u3002<\/li>\n\n\n\n<li><strong>GET <\/strong>&#8212; \u8fd4\u56de\u8be5\u952e\u5728SET\u6307\u4ee4\u6267\u884c\u524d\u6240\u4fdd\u5b58\u7684\u503c\uff0c\u5982\u679c\u952e\u4e0d\u5b58\u5728\u5219\u8fd4\u56denil\u3002\u5982\u679ckey\u5bf9\u5e94\u7684\u503c\u4e0d\u662fString\u7c7b\u578b\uff0c<strong>SET<\/strong>\u6307\u4ee4\u4f1a\u53d6\u6d88\u6267\u884c\u5e76\u8fd4\u56deError\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u6ce8\uff1a\u7531\u4e8e<strong>SET<\/strong>\u6307\u4ee4\u5e26\u4e0a\u8fd9\u4e9b\u53ef\u7528\u9009\u9879\u4e4b\u540e\uff0c\u5c31\u53ef\u4ee5\u4ee3\u66ff<strong>SETNX\uff0cSETEX\uff0cPSETEX\uff0cGETSET<\/strong>\u3002\u6240\u4ee5\u5728redis\u4ee5\u540e\u7684\u7248\u672c\u4e2d\u53ef\u80fd\u4f1a\u79fb\u9664\u8fd9\u4e9b\u6307\u4ee4\u3002<\/p>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u8fd4\u56de\u503c<\/strong><\/h2>\n\n\n\n<p>Simple string reply\uff1a\u5982\u679c<strong>SET<\/strong>\u6307\u4ee4\u6267\u884c\u6210\u529f\u4f1a\u8fd4\u56de<strong>OK<\/strong>\u3002<\/p>\n\n\n\n<p>NUll reply\uff1a\u5982\u679c\u7528\u6237\u5728\u4f7f\u7528SET\u6307\u4ee4\u65f6\u6dfb\u52a0\u4e86 NX\u6216XX\u9009\u9879\uff0c\u4f46\u5bf9\u5e94\u7684\u952e\u5b58\u5728\/\u4e0d\u5b58\u5728\uff0c\u5bfc\u81f4SET\u6307\u4ee4\u6ca1\u6709\u6267\u884c\uff0c\u4f1a\u8fd4\u56denil\u3002<\/p>\n\n\n\n<p>\u5982\u679cSET\u6307\u4ee4\u52a0\u4e0a\u4e86GET\u9009\u9879\uff0c\u90a3\u4e48\u4e0a\u9762\u4e24\u6761\u5219\u4e0d\u518d\u9002\u7528\u3002\u65e0\u8bbaSET\u6307\u4ee4\u662f\u5426\u6b63\u5e38\u6267\u884c\u90fd\u4f1a\u8fd4\u56de\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n\n\n\n<p>Bulk string reply\uff1a\u952e\u5728SET\u6307\u4ee4\u4e4b\u524d\u524d\u4fdd\u5b58\u7684\u503c\u3002<\/p>\n\n\n\n<p>Null reply\uff1a\u5982\u679c\u952e\u4e0d\u5b58\u5728\u5219\u8fd4\u56denil\u3002<\/p>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u8bbe\u8ba1\u6a21\u5f0f<\/strong><\/h2>\n\n\n\n<p>\u6ce8\u610f\uff1a\u4e0b\u5217\u7684\u6a21\u5f0f\u5e76\u4e0d\u5efa\u8bae\u62ff\u6765\u5b9e\u73b0redis\u7684\u5206\u5e03\u5f0f\u9501\u3002\u800c\u662f\u53c2\u8003\u5b9e\u73b0\u66f4\u590d\u6742\u4f46\u63d0\u4f9b\u66f4\u597d\u4fdd\u969c\u548c\u5bb9\u9519\u7387\u7684the Redlock algorithm\u3002<\/p>\n\n\n\n<p>\u6307\u4ee4<strong>SET resource-name anystring NX EX max-lock-time<\/strong>\u662f\u5728redis\u4e2d\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501\u7684\u4e00\u79cd\u7b80\u5355\u65b9\u5f0f\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u4e0a\u9762\u8fd9\u6761\u6307\u4ee4\u8fd4\u56de<strong>OK<\/strong>\uff0c\u90a3\u4e48\u5ba2\u6237\u7aef\u5c31\u53ef\u4ee5\u83b7\u5f97\u9501\uff0c\uff08\u6216\u8005\u5ba2\u6237\u7aef\u5728\u8be5\u6307\u4ee4\u8fd4\u56denil\u65f6\u5ba2\u6237\u7aef\u591a\u8bd5\u51e0\u6b21\uff09\uff0c\u7136\u540e\u901a\u8fc7<strong>DEL<\/strong>\u6307\u4ee4\u91ca\u653e\u9501\uff0c<\/p>\n\n\n\n<p>\u5f53\u952e\u5230\u4e86\u8fc7\u671f\u65f6\u95f4\u65f6\uff0c\u9501\u4f1a\u88ab\u81ea\u52a8\u91ca\u653e\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u4fee\u6539\u89e3\u9501\u6a21\u5f0f\u8fd8\u53ef\u4ee5\u4f7f\u7cfb\u7edf\u53d8\u5f97\u66f4\u52a0\u9c81\u68d2:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote has-vivid-cyan-blue-color has-text-color\">\n<p class=\"has-vivid-cyan-blue-color has-text-color\">\u5199\u8005\u6ce8<\/p>\n<cite>\u9c81\u68d2\uff0c\u5373robust\u7684\u8c10\u97f3\u3002\u9c81\u68d2\u6027\u6307\u7684\u662f\u7cfb\u7edf\u5728\u6536\u5230\u5916\u90e8\u5e72\u6270\u6216\u8005\u5185\u90e8\u53c2\u6570\u6444\u52a8\u7b49\u4e0d\u786e\u5b9a\u56e0\u7d20\u7684\u5e72\u6270\u65f6\uff0c\u8fd8\u80fd\u4fdd\u6301\u5176\u7ed3\u6784\u548c\u529f\u80fd\u7a33\u5b9a\u3002<\/cite><\/blockquote>\n\n\n\n<ul>\n<li>\u8bbe\u7f6e\u4e00\u4e2a\u968f\u673a\u4e0d\u53ef\u731c\u6d4b\u7684\u5927\u5b57\u7b26\u4e32\uff0c\u800c\u4e0d\u662f\u8bbe\u7f6e\u4e00\u4e2a\u56fa\u5b9a\u7684\u3002\u8fd9\u4e2a\u5927\u5b57\u7b26\u4e32\u88ab\u79f0\u4e3a\u8bb0\u53f7(token)\u3002<\/li>\n\n\n\n<li>\u8bbe\u7f6e\u4e00\u4e2a\u53ea\u6709\u5f53\u503c\u5339\u914d\u65f6\u624d\u79fb\u9664\u952e\u7684\u811a\u672c\uff0c\u800c\u4e0d\u662f\u7528<strong>DEL<\/strong>\u6307\u4ee4\u53bb\u91ca\u653e\u9501\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u5f53A\u5ba2\u6237\u7aef\u7684\u952e\u7531\u4e8e\u5230\u4e86\u8fc7\u671f\u65f6\u95f4\u800c\u88ab\u5220\u9664\u540e\uff0c\u800cB\u5ba2\u6237\u7aef\u5728\u8be5\u65f6\u95f4\u540e\u91cd\u65b0\u521b\u5efa\u4e86\u4e00\u4e2a\u76f8\u540c\u952e\u7684\u9501\uff0c\u6b64\u65f6A\u5ba2\u6237\u7aef\u82e5\u518d\u6267\u884c<strong>DEL<\/strong>\u6307\u4ee4\u5c31\u4f1a\u91ca\u653eB\u5ba2\u6237\u7aef\u7684\u9501\u3002<\/p>\n\n\n\n<p>\u89e3\u9501\u7684\u811a\u672c\u548c\u4e0b\u5217\u7684\u4ee3\u7801\u76f8\u4f3c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if redis.call(\"get\",KEYS&#91;1]) == ARGV&#91;1]\nthen\n    return redis.call(\"del\",KEYS&#91;1])\nelse\n    return 0\nend<\/code><\/pre>\n\n\n\n<p>\u8be5\u811a\u672c\u7684\u8c03\u7528\u65b9\u5f0f\u4e3a <strong>EVAL &#8230;script&#8230; 1 resource-name token-value<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>redis\u5b98\u7f51&#8211;set\u6307\u4ee4\u7ffb\u8bd1<\/p>\n","protected":false},"author":1,"featured_media":3691,"comment_status":"open","ping_status":"","sticky":false,"template":"base","format":"standard","meta":{"footnotes":""},"categories":[40,43],"tags":[39,41],"_links":{"self":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/1089"}],"collection":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/comments?post=1089"}],"version-history":[{"count":0,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/1089\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media\/3691"}],"wp:attachment":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media?parent=1089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/categories?post=1089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/tags?post=1089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}