{"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>\n

 lua \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

eval\u65b9\u6cd5\u7684\u53c2\u6570 3\u4e2a\uff0c\u7b2c\u4e00\u4e2a\u662f\u811a\u672c\u4ee3\u7801\uff08\u5177\u4f53\u6267\u884c\u7684\u4ee3\u7801\uff09\uff0c\u7b2c\u4e8c\u4e2a\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u53c2\u6570\u6570\u7ec4\uff0c\u7b2c\u4e09\u4e2a\u53c2\u6570\u662f\u4e2a\u6574\u6570\uff0c\u8868\u793a\u7b2ckey\u53c2\u6570\u7684\u6570\u91cf\uff0clua\u4ee3\u7801\u4e2d\u7684KEYS\u6570\u91cf\uff08\u4e0d\u5305\u62ecARGV\u6570\u91cf\uff09<\/span><\/p>\n<\/p>\n

\u4e09\u3001PHP+redis\u5206\u5e03\u5f0f\u9501<\/a>\u793a\u4f8b<\/h2>\n
<?php\r\n \r\n\/**\r\n * redis \u5206\u5e03\u5f0f\u9501\r\n *\/\r\n \r\nclass Lock{\r\n \r\n    private $config;\r\n    private $redis;\r\n \r\n    public function __construct($config = []){\r\n        $this->config = $config ? $config : ['host' => '127.0.0.1', 'port' => 6379];\r\n        $this->redis = $this->connect();\r\n    }\r\n \r\n    public function connect(){\r\n        $redis = new Redis();\r\n        $redis->connect($this->config['host'], $this->config['port']);\r\n        return $redis;\r\n    }\r\n \r\n    \/**\r\n     * @param $scene \u9501\u573a\u666f\r\n     * @param $expire \u9501\u6709\u6548\u671f\r\n     * @return bool\r\n     *\/\r\n    public function lock($scene = null , $expire = 10){\r\n        if (!$scene || !$expire){\r\n            return false;\r\n        }\r\n \r\n        \/\/ \u751f\u6210\u968f\u673a\u503c\uff0c\u9501\u6807\u8bc6\r\n        $lockId = md5(uniqid());\r\n        $result = $this->redis->set($scene, $lockId, ['NX', 'EX' => $expire]) ;\r\n \r\n        if($result)\r\n            return $lockId;\r\n        else\r\n            return $result;\r\n    }\r\n \r\n    \/**\r\n     * \u89e3\u9501\r\n     *\/\r\n    public function unLock($scene, $lockId){\r\n \r\n        $lua = <<<SCRIPT\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\nSCRIPT;\r\n        \/\/ \u6267\u884clua\u811a\u672c\r\n        return $this->redis->eval($lua, [$scene, $lockId], 1);\r\n    }\r\n}\r\n \r\n \r\n$lock = new Lock();\r\n \r\n\u7b2c\u4e00\u6b21\u52a0\u9501\r\n$res = $lock->Lock("test", 30);\r\nvar_dump($res); \/\/ \u8fd4\u56delockId\r\n \r\necho "<br>";\r\n \r\n\u7b2c\u4e8c\u6b21\u52a0\u9501\r\n$res1 = $lock->Lock("test", 25);\r\nvar_dump($res1); \/\/\u52a0\u9501\u5931\u8d25 false\r\n \r\n\/\/\u89e3\u9501\r\nif($res){\r\n    $lock -> unLock("test", $res);\r\n}<\/pre>\n

\u56db\u3001redis\u96c6\u7fa4\u5206\u5e03\u5f0f\u9501<\/p>\n

Redis \u96c6\u7fa4\u76f8\u5bf9\u5355\u673a\u6765\u8bf4, \u9700\u8981\u8003\u8651\u4e00\u4e2a \u5bb9\u9519\u6027, \u8bbe\u8ba1\u4e0a\u66f4\u4e3a\u590d\u6742<\/p>\n

<\/p>\n

RedLock \u7b97\u6cd5\uff1a\u5b98\u65b9\u7ed9\u51fa\u4e86\u4e00\u4e2a RedLock \u7b97\u6cd5<\/p>\n

<\/p>\n

\u60c5\u666f: \u5f53\u524d\u6709N\u4e2a\u5b8c\u5168\u72ec\u7acb\u7684Redis master\u8282\u70b9, \u5206\u522b\u90e8\u7f72\u5728\u4e0d\u540c\u7684\u4e3b\u673a\u4e0a<\/p>\n

<\/p>\n

\u5ba2\u6237\u7aef\u83b7\u53d6\u9501\u7684\u64cd\u4f5c:<\/p>\n

<\/p>\n

\u4f7f\u7528\u76f8\u540ckey\u548c\u552f\u4e00\u503c(\u4f5c\u4e3avalue)\u540c\u65f6\u5411\u8fd9N\u4e2aredis\u8282\u70b9\u8bf7\u6c42\u9501, \u9501\u7684\u8d85\u65f6\u65f6\u95f4\u5e94\u8be5 >> \u8d85\u65f6\u65f6\u95f4(\u8003\u8651\u5230\u8bf7\u6c42\u8017\u65f6), \u82e5\u67d0\u4e2a\u8282\u70b9\u963b\u585e\u4e86\u4e86\u5e94\u5c3d\u5feb\u8df3\u8fc7<\/p>\n

\u8ba1\u7b97\u6b65\u9aa41\u6d88\u8017\u7684\u65f6\u95f4, \u82e5\u603b\u6d88\u8017\u65f6\u95f4\u8d85\u8fc7\u8d85\u65f6\u65f6\u95f4, \u5219\u8ba4\u4e3a\u9501\u5931\u8d25. \u5ba2\u6237\u7aef\u9700\u5728\u5927\u591a\u6570(\u8d85\u8fc7\u4e00\u534a)\u7684\u8282\u70b9\u4e0a\u6210\u529f\u83b7\u53d6\u9501, \u624d\u8ba4\u4e3a\u662f\u9501\u6210\u529f.<\/p>\n

\u5982\u679c\u9501\u6210\u529f\u4e86, \u5219\u8be5\u9501\u6709\u6548\u65f6\u95f4\u5c31\u662f \u9501\u539f\u59cb\u6709\u6548\u65f6\u95f4 - \u6b65\u9aa41\u6d88\u8017\u7684\u65f6\u95f4<\/p>\n

\u5982\u679c\u9501\u5931\u8d25\u4e86(\u8d85\u65f6\u6216\u65e0\u6cd5\u83b7\u53d6\u8d85\u8fc7\u4e00\u534a N\/2 + 1 \u5b9e\u4f8b\u7684\u9501), \u5ba2\u6237\u7aef\u4f1a\u5230\u6bcf\u4e2a\u8282\u70b9\u91ca\u653e\u9501(\u662f\u6bcf\u4e2a, \u5373\u4f7f\u4e4b\u524d\u8ba4\u4e3a\u52a0\u9501\u5931\u8d25\u7684\u8282\u70b9)<\/p>\n

<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"

\u4e00\u3001\u5206\u5e03\u5f0f\u9501\u6982\u8ff0         \u5728\u5206\u5e03\u5f0f\u73af\u5883\u4e0b\uff0c\u5404\u4e2a\u7ebf\u7a0b\u901a\u8fc7\u5bf9\u516c\u5171\u8d44 […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,92,143],"tags":[],"class_list":["post-2064","post","type-post","status-publish","format-standard","hentry","category-php","category-phpcode","category-redis-2"],"_links":{"self":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2064"}],"collection":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/comments?post=2064"}],"version-history":[{"count":1,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2064\/revisions"}],"predecessor-version":[{"id":2065,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2064\/revisions\/2065"}],"wp:attachment":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/media?parent=2064"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/categories?post=2064"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/tags?post=2064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}