{"id":2064,"date":"2023-01-06T19:35:48","date_gmt":"2023-01-06T11:35:48","guid":{"rendered":"https:\/\/www.tra56.com\/?p=2064"},"modified":"2023-01-06T19:35:48","modified_gmt":"2023-01-06T11:35:48","slug":"2064","status":"publish","type":"post","link":"https:\/\/www.tra56.com\/2064.html","title":{"rendered":"PHP+Redis\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501"},"content":{"rendered":"
\u4e00\u3001\u5206\u5e03\u5f0f\u9501\u6982\u8ff0<\/p>\n
\u5728\u5206\u5e03\u5f0f\u73af\u5883\u4e0b\uff0c\u5404\u4e2a\u7ebf\u7a0b\u901a\u8fc7\u5bf9\u516c\u5171\u8d44\u6e90\u7684\u62a2\u5360\uff0c\u80fd\u591f\u4f7f\u4e00\u4e2a\u4ee3\u7801\u5757\u5728\u540c\u4e00\u65f6\u95f4\u53ea\u80fd\u88ab\u4e00\u4e2a\u673a\u5668\u7684\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\uff0c\u8fd9\u4e2a\u673a\u5236\u5c31\u662f\u5206\u5e03\u5f0f\u9501\u3002<\/p>\n
<\/p>\n
\u5206\u5e03\u5f0f\u9501\u4e3b\u8981\u7528\u4e8e\u5728\u5206\u5e03\u5f0f\u73af\u5883\u4e2d\u4fdd\u62a4\u8de8\u8fdb\u7a0b\u3001\u8de8\u4e3b\u673a\u3001\u8de8\u7f51\u7edc\u7684\u5171\u4eab\u8d44\u6e90\u5b9e\u73b0\u4e92\u65a5\u8bbf\u95ee\uff0c\u4ee5\u8fbe\u5230\u4fdd\u8bc1\u6570\u636e\u7684\u4e00\u81f4\u6027<\/p>\n
<\/p>\n
\u5b9e\u73b0\u9501\u7684\u64cd\u4f5c\u4e3b\u8981\u6709\u4e24\u4e2a\uff0c\u5373lock()\u548cunlock()\u3002<\/p>\n
<\/p>\n
\u5206\u5e03\u5f0f\u9501\u5b9e\u73b0\u7684\u6ce8\u610f\u70b9\uff1a<\/p>\n
<\/p>\n
1\uff09\u4e92\u65a5: \u4efb\u610f\u65f6\u523b, \u53ea\u80fd\u6709\u4e00\u4e2a\u5ba2\u6237\u7aef\u83b7\u5f97\u9501<\/p>\n
<\/p>\n
2\uff09\u4e0d\u4f1a\u6b7b\u9501: \u5ba2\u6237\u7aef\u6301\u6709\u9501\u671f\u95f4\u5d29\u6e83, \u6ca1\u6709\u4e3b\u52a8\u89e3\u9664\u9501, \u80fd\u4fdd\u8bc1\u540e\u7eed\u7684\u5176\u4ed6\u5ba2\u6237\u7aef\u83b7\u5f97\u9501<\/p>\n
<\/p>\n
3\uff09\u9501\u5f52\u5c5e\u6807\u8bc6: \u52a0\u9501\u548c\u89e3\u9501\u7684\u5fc5\u987b\u662f\u540c\u4e00\u4e2a\u5ba2\u6237\u7aef, \u5ba2\u6237\u7aef\u4e0d\u80fd\u89e3\u6389\u975e\u81ea\u5df1\u6301\u6709\u7684\u9501(\u9501\u5e94\u5177\u5907\u6807\u8bc6)<\/p>\n
<\/p>\n
4\uff09\u5982\u679c\u662fRedis\u96c6\u7fa4, \u8fd8\u5f97\u8003\u8651\u5177\u6709\u5bb9\u9519\u6027: \u53ea\u8981\u5927\u90e8\u5206Redis\u8282\u70b9\u6b63\u5e38\u8fd0\u884c, \u5ba2\u6237\u7aef\u5c31\u53ef\u4ee5\u52a0\u9501\u548c\u89e3\u9501.<\/p>\n
<\/p>\n
\u4e8c\u3001redis\u5b9e\u73b0\u9501\u7684\u547d\u4ee4<\/p>\n
1\u3001redis\u5b9e\u73b0\u9501\u7684\u547d\u4ee4<\/p>\n
set key value NX EX<\/pre>\n"NX" \u4ec5\u5728key\u4e0d\u5b58\u5728\u65f6\u52a0\u9501, \u6ee1\u8db3\u6761\u4ef61: \u4e92\u65a5\u578b<\/p>\n
"EX" \u8bbe\u7f6e\u9501\u8fc7\u671f\u65f6\u95f4(\u79d2\uff09, \u6ee1\u8db3\u6761\u4ef62: \u907f\u514d\u6b7b\u9501<\/p>\n
<\/p>\n
\u4e0a\u9762\u8fd9\u4e2aset\u547d\u4ee4\u62c6\u89e3\u5f00\u5c31\u662f\uff1a<\/p>\n
setnx cache_key random_value \r\nexpire cache_key 30<\/pre>\n\u867d\u7136\u8fd9\u4e24\u7ec4\u547d\u4ee4\u6267\u884c\u7684\u6548\u679c\u4e00\u6837\uff0c\u4f46\u662f\u7b2c\u4e8c\u4e2a\u662f\u975e\u539f\u5b50\u6027\u64cd\u4f5c\uff0c\u5982\u679c\u6267\u884c\u4e86setnx\u6210\u529f\uff0c\u4f46\u662fexpire\u5931\u8d25\u7684\u8bdd\uff0c\u5c31\u4f1a\u9020\u6210\u8fd9\u4e2akey\u4e00\u76f4\u5b58\u5728\u4e86\uff0c\u65e0\u6cd5\u91ca\u653e\u7684\u60c5\u51b5\uff08\u6b7b\u9501\uff09\u3002<\/p>\n
<\/p>\n
2\u3001\u4f7f\u7528\u968f\u673a\u6570\u589e\u52a0\u9501\u6807\u8bc6<\/p>\n
public function lock(){\r\n \/\/ \u751f\u6210\u968f\u673a\u503c\r\n $this->lockValue = md5(uniqid());\r\n return $this->redis->set($this->scene, $this->lockValue , ['NX', 'EX' => $this->expire]);\r\n}<\/pre>\n\u4f7f\u7528\u9501\u6807\u8bc6\uff08\u589e\u52a0\u968f\u673a\u6570\uff09\u7684\u539f\u56e0\uff1a\u907f\u514d\u67d0\u4e2a\u5ba2\u6237\u7aef\u83b7\u53d6\u9501\u540e\u505a\u5176\u4ed6\u64cd\u4f5c\u8fc7\u4e45\u800c\u5bfc\u81f4\u9501\u88ab\u81ea\u52a8\u91ca\u653e\uff0c\u4f46\u8be5\u5ba2\u6237\u7aef\u4ee5\u4e3a\u8fd8\u83b7\u5f97\u9501\uff0c\u8fd9\u65f6\u5047\u8bbe\u5176\u4ed6\u5ba2\u6237\u7aef\u83b7\u53d6\u4e86\u9501\uff0c\u4f46\u4e0a\u4e2a\u5ba2\u6237\u7aef\u5220\u9664\u9501\u7ee7\u800c\u5bfc\u81f4\u672c\u5ba2\u6237\u7aef\u7684\u9501\u88ab\u5220\u9664\uff0c\u603b\u800c\u8a00\u4e4b\u5ba2\u6237\u7aef\u53ea\u80fd\u5220\u9664\u81ea\u5df1\u7684\u9501\uff0c\u901a\u8fc7\u9501\u6807\u8bc6\u8fdb\u884c\u5224\u65ad<\/p>\n
<\/p>\n
3\u3001\u91ca\u653e\u9501\u7684\u6b65\u9aa4<\/p>\n
get \u6240\u6301\u6709\u9501<\/p>\n
\u5224\u65ad\u8fd9\u4e2a\u9501\u662f\u5426\u81ea\u5df1\u6240\u6301\u6709<\/p>\n
\u5220\u9664\u6301\u6709\u9501<\/p>\n
\u6240\u4ee5\uff0c\u8fd9\u4e09\u6b65\u8981\u4fdd\u8bc1\u539f\u5b50\u6027\uff0c\u7528lua\u811a\u672c\u6765\u6267\u884c\uff0credis\u5b98\u65b9\u5df2\u7ecf\u63d0\u4f9b\u811a\u672c\u6587\u4ef6\u3002<\/p>\n
if redis.call("get",KEYS[1]) == ARGV[1] then\r\n return redis.call("del",KEYS[1])\r\nelse\r\n return 0\r\nend<\/pre>\nlua \u811a\u672c\u4e2d\u6267\u884c get \u548c del \u662f\u539f\u5b50\u6027\u7684, \u6574\u4e2alua\u811a\u672c\u4f1a\u88ab\u5f53\u505a\u4e00\u6761\u547d\u4ee4\u6765\u6267\u884c\uff0c\u5373\u4f7f get \u540e\u9501\u521a\u597d\u8fc7\u671f, \u6b64\u65f6\u4e5f\u4e0d\u4f1a\u88ab\u5176\u4ed6\u5ba2\u6237\u7aef\u52a0\u9501\uff0c\u76f4\u5230eval\u547d\u4ee4\u6267\u884c\u5b8c\u6210\uff0cRedis\u624d\u4f1a\u6267\u884c\u5176\u4ed6\u547d\u4ee4\u3002\u5982\u679c\u4e0d\u7528\u539f\u5b50\u6027\uff0c\u8fd8\u662f\u4f1a\u5bfc\u81f4\u8be5\u9501\u88ab\u5176\u4ed6\u5ba2\u6237\u7aef\u83b7\u5f97\uff0c\u4f46\u4e0a\u5ba2\u6237\u7aef\u5df2\u7ecf\u5224\u65ad\u8fc7\uff08\u81ea\u5df1\u6240\u6301\u6709\uff09\uff0c\u8fdb\u4e00\u6b65\u4f1a\u5220\u9664\u8be5\u9501\u3002<\/p>\n
\/**\r\n * \u89e3\u9501\r\n *\/\r\npublic function unLock(){\r\n $script = <<<LUA\r\n local key=KEYS[1]\r\n local value=ARGV[1]\r\n if(redis.call('get', key) == value)\r\n then\r\n return redis.call('del', key)\r\n end\r\n LUA;\r\n \r\n \/\/ \u6267\u884clua\u811a\u672c\r\n $this->redis->eval($script, [$this->scene, $this->lockValue], 1);\r\n}<\/pre>\n