{"id":2619,"date":"2021-09-04T01:11:39","date_gmt":"2021-09-03T16:11:39","guid":{"rendered":"https:\/\/took.jp\/?p=2619"},"modified":"2021-09-04T01:11:39","modified_gmt":"2021-09-03T16:11:39","slug":"redis-lua-php","status":"publish","type":"post","link":"https:\/\/took.jp\/tech-blog\/redis-lua-php\/","title":{"rendered":"PHPRedis\u3067Lua\u3092\u4f7f\u3063\u3066\u307f\u305f\u306e\u3067\u5099\u5fd8\u9332\u3010\u30b5\u30f3\u30d7\u30eb\u4ed8\u304d\u3011"},"content":{"rendered":"<h2>Redis\u3067Lua\u3092\u4f7f\u3063\u305f\u306e\u3067\u5099\u5fd8\u9332\u3010PHP\u30b5\u30f3\u30d7\u30eb\u4ed8\u304d\u3011<\/h2>\n<p>&nbsp;<\/p>\n<h3>\u306a\u305cLua\u3092\u4f7f\u3046\u306e\u304b<\/h3>\n<p>Lua\u3092\u4f7f\u3046\u3068\u3001Redis\u306b\u5b58\u5728\u3057\u3066\u3044\u308b\u30b3\u30de\u30f3\u30c9\u3067\u5b9f\u73fe\u3067\u304d\u306a\u3044\u52d5\u4f5c\u3092\u5b9f\u73fe\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u3053\u308c\u306b\u306f\u3001\u300cRedis Eval\u300d\u306a\u3069\u3068\u691c\u7d22\u3059\u308b\u3068\u3001\u53c2\u8003\u8cc7\u6599\u304c\u51fa\u3066\u304f\u308b\u304b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u203b \u5f8c\u8ff0\u306b\u8a73\u7d30\u3092\u8a18\u8f09\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u307e\u305f\u3001\u300cRedis Lua\u300d\u3067\u691c\u7d22\u3059\u308b\u3068\u3001\u3088\u304fAtomic\u306a\u51e6\u7406\u3068\u51fa\u3066\u304d\u307e\u3059\u3002<\/p>\n<p>Atomic\u306a\u64cd\u4f5c\u3068\u306f\u3001\u8abf\u3079\u3066\u307f\u308b\u3068\u3001\u4ed6\u8005\u304c\u5272\u308a\u8fbc\u3080\u3053\u3068\u306e\u51fa\u6765\u306a\u3044\u64cd\u4f5c\u3068\u3044\u3046\u610f\u5473\u3060\u305d\u3046\u3067\u3059\u3002<\/p>\n<p>\u3064\u307e\u308a\u3001\u540c\u6642\u306b\u30c7\u30fc\u30bf\u306b\u30a2\u30af\u30bb\u30b9\u3055\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u969b\u306b\u3001\u30ed\u30c3\u30af\u3092\u3057\u306a\u304f\u3066\u3082\u826f\u3044\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h3>\u5b9f\u969b\u306b\u4f7f\u3063\u3066\u307f\u308b<\/h3>\n<p>Redis\u3067Lua\u3092\u4f7f\u7528\u3059\u308b\u969b\u306b\u306f\u3001\u524d\u8ff0\u306b\u3082\u3042\u308a\u307e\u3057\u305f\u300cEval\u300d\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u4ee5\u4e0b\u306e\u8a18\u8ff0\u306f\u3001\u540c\u3058\u52d5\u4f5c\u3092\u3057\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\uff08redis-cli\u3092\u7acb\u3061\u4e0a\u3052\u3066\u3001\u5b9f\u884c\u3057\u307e\u3059\uff09<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u901a\u5e38\u306e\u5834\u5408<\/strong><\/p>\n<pre class=\"\"><code class=\"language-other\">&gt; set test 123<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Eval\u306e\u5834\u5408<\/strong><\/p>\n<pre class=\"\"><code class=\"language-other\">&gt; eval \"redis.call('set','test' , 123)\"  0<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>eval\u306e\u5f8c\u306b\u3042\u308b\u5f15\u6570\u306e\u6587\u5b57\u5217\u304cLua\u3068\u306a\u308a\u3001<\/p>\n<p>Lua\u306e\u4e2d\u3067redis.call\u95a2\u6570\u3092\u5b9f\u884c\u3059\u308b\u3068\u3001Redis\u306b\u5b58\u5728\u3059\u308b\u30b3\u30de\u30f3\u30c9\u3092\u547c\u3073\u51fa\u3059\u3053\u3068\u304c\u3067\u304d\u308b\u3068\u3044\u3046\u610f\u5473\u3067\u3059\u306d\u3002<\/p>\n<p>&nbsp;<\/p>\n<h3>\u5148\u307b\u3069\u306e\u52d5\u304d\u3092PHP\u3067\u66f8\u3044\u3066\u307f\u308b<\/h3>\n<p>&nbsp;<\/p>\n<p><strong>\u901a\u5e38\u306e\u5834\u5408<\/strong><\/p>\n<pre class=\"line-numbers\"><code class=\"language-php\">$redis-&gt;set('test', 123);<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Eval\u306e\u5834\u5408<\/strong><\/p>\n<pre class=\"line-numbers\"><code class=\"language-php\">$command = \u201credis.call(\u2019set', 'test', 123)\";\r\n$redis-&gt;eval($command, 0);<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>\u3053\u3053\u307e\u3067\u3060\u3068\u3001\u3069\u3046\u3044\u3046\u6642\u306bLua\u3092\u4f7f\u3046\u306e\u304b\u308f\u304b\u308a\u306b\u304f\u3044\u3068\u601d\u3044\u307e\u3059\u304c\u3001Lua\u306e\u4e2d\u3067\u8907\u6570\u306e\u51e6\u7406\u3092\u3059\u308b\u3053\u3068\u3082\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u5024\u3092set\u3057\u3066\u3001\u305d\u306e\u5024\u3092get\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"line-numbers\"><code class=\"language-php\">$command = \u201c\r\n    redis.call(\u2019set', 'test', 123)\r\n    return redis.call(\u2019get', 'test')\r\n\";\r\n$test = $redis-&gt;eval($command, 0);<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>\u3053\u306e\u3088\u3046\u306b\u3057\u305f\u5834\u5408\u306b\u306f\u3001test\u5909\u6570\u306b\u300c123\u300d\u304c\u5165\u308a\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<h3>PHP\u304b\u3089Lua\u306b\u5024\u3092\u6e21\u3057\u3066\u307f\u308b<\/h3>\n<p>\u5148\u307b\u3069\u307e\u3067\u306e\u52d5\u4f5c\u3067\u306f\u3001\u300ctest\u300d\u3068\u3044\u3046\u30ad\u30fc\u540d\u3092\u305d\u306e\u307e\u307eLua\u306e\u4e2d\u306b\u8a18\u8ff0\u3057\u3066\u3044\u308b\u305f\u3081\u3001<\/p>\n<p>\u56fa\u5b9a\u306e\u30ad\u30fc\u540d\u3067\u306e\u52d5\u4f5c\u3057\u304b\u305b\u305a\u3001\u3053\u306e\u307e\u307e\u3067\u306f\u5f79\u306b\u7acb\u3061\u307e\u305b\u3093\u3002<\/p>\n<p>\u305d\u306e\u305f\u3081\u3001PHP\u304b\u3089Lua\u306b\u5024\u3092\u6e21\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Redis\u304b\u3089\u8a66\u3059<\/strong><\/p>\n<pre class=\"\"><code class=\"language-other\">&gt; eval \"return redis.call('set', KEYS[1], ARGV[1])\" 1 'test' 123<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>\u6025\u306b\u308f\u304b\u308a\u3065\u3089\u304f\u306a\u308a\u307e\u3057\u305f\u306d\u3002<\/p>\n<p>\u307e\u305aeval\u306e\u5f15\u6570\u306b\u3064\u3044\u3066\u3001\u5b66\u3073\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><span class=\"sc_content_icon\" style=\"color: #ccc;\"><i class=\"fa fa-angle-right\" aria-hidden=\"true\"><span>fa-angle-right<\/span><\/i><\/span>\u7b2c\uff11\u5f15\u6570 : Lua script<\/p>\n<p><span class=\"sc_content_icon\" style=\"color: #ccc;\"><i class=\"fa fa-angle-right\" aria-hidden=\"true\"><span>fa-angle-right<\/span><\/i><\/span>\u7b2c2\u5f15\u6570 : \u30ad\u30fc\u306e\u6570<\/p>\n<p><span class=\"sc_content_icon\" style=\"color: #ccc;\"><i class=\"fa fa-angle-right\" aria-hidden=\"true\"><span>fa-angle-right<\/span><\/i><\/span>\u7b2c3\u5f15\u6570\uff08\u6307\u5b9a\u5206\uff09 : \u30ad\u30fc<\/p>\n<p><span class=\"sc_content_icon\" style=\"color: #ccc;\"><i class=\"fa fa-angle-right\" aria-hidden=\"true\"><span>fa-angle-right<\/span><\/i><\/span>\u7b2c4\u5f15\u6570 : \u5f15\u6570<\/p>\n<p>&nbsp;<\/p>\n<p>\u4eca\u307e\u3067\u7b2c2\u5f15\u6570\u30670\u3092\u6e21\u3057\u3066\u3044\u305f\u306e\u306f\u3001\u30ad\u30fc\u304c\u306a\u304b\u3063\u305f\u304b\u3089\u3067\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u30ad\u30fc\u304c2\u3064\u3001\u5f15\u6570\u304c3\u3064\u3068\u304b\u306e\u5834\u5408\u3001\u7b2c2\u5f15\u6570\u306b\u300c2\u300d\u3068\u3044\u308c\u3066\u3084\u308c\u3070\u3001<\/p>\n<p>3,4\u756a\u76ee\u306e\u5024\u304c\u30ad\u30fc\u3001\u6b8b\u308a\u304c\u5f15\u6570\u3068\u3057\u3066\u6271\u308f\u308c\u308b\u3088\u3046\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u307e\u305f\u3001Lua\u306b\u6e21\u3059\u5024\u3092\u53d6\u308a\u51fa\u3059\u5834\u5408\u306b\u306f\u3001KEYS, ARGV \u3067\u305d\u308c\u305e\u308c\u3092\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u3053\u306e\u6642\u306b\u3001\u4e00\u756a\u76ee\u306e\u8981\u7d20\u3092\u53d6\u308a\u51fa\u305d\u3046\u3068\u3057\u3066\u3001KEYS[0]\u306e\u3088\u3046\u306b\u9593\u9055\u3048\u306a\u3044\u3088\u3046\u306b\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u4eca\u5ea6\u306f\u3001\u5148\u307b\u3069\u306e\u51e6\u7406\u3092PHP\u3067\u66f8\u3044\u3066\u307f\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>PHP\u306e\u5834\u5408<\/strong><\/p>\n<pre class=\"line-numbers\"><code class=\"language-php\">$command = \u201credis.call(\u2019set',  KEYS[1], ARGV[1])\";\r\n$test = $redis-&gt;eval($command, 1 , 'test', 123);<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>\u66f8\u304d\u65b9\u306f\u3001\u307b\u3068\u3093\u3069\u5909\u308f\u3089\u306a\u3044\u306e\u3067\u3001\u7c21\u5358\u3067\u3059\u306d\u3002<\/p>\n<p>&nbsp;<\/p>\n<h3>\u30ad\u30fc\u3068\u5f15\u6570\u306e\u9055\u3044\u306b\u3064\u3044\u3066<\/h3>\n<p>\u305d\u3082\u305d\u3082\u306b\u300c\u30ad\u30fc\u300d\u3068\u300c\u5f15\u6570\u300d\u3068\u3044\u3046\u547c\u3073\u65b9\u304c\u6b63\u5f0f\u306a\u3082\u306e\u304b\u3082\u308f\u304b\u3089\u306a\u3044\u3067\u3059\u304c\u3001<\/p>\n<p>\u3053\u306e\u8a18\u4e8b\u5185\u3067\u306f\u3001\u3053\u306e\u547c\u3073\u65b9\u3067\u7d71\u4e00\u3044\u305f\u3057\u307e\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>eval\u306e\u5f15\u6570\u3068\u3057\u3066\u6e21\u3057\u3066\u3044\u308b\u3001\u30ad\u30fc\u3068\u5f15\u6570\u3001\u5225\u306b\u5206\u3051\u308b\u5fc5\u8981\u306a\u3044\u306e\u3067\u306f\u3001\u3068\u611f\u3058\u305f\u65b9\u3082\u3044\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/p>\n<p>\u3057\u304b\u3057\u3001\u30ad\u30fc\u306bprefix\u3092\u3064\u3051\u308b\u7b49\u3092\u3057\u3066\u3044\u308b\u5834\u5408\u306b\u9055\u3044\u304c\u51fa\u3066\u304f\u308b\u3088\u3046\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u305d\u306e\u4ed6\u306b\u3082\u7406\u7531\u304c\u3042\u308b\u3068\u601d\u3046\u306e\u3067\u3059\u304c\u3001\u77e5\u3089\u306a\u3044\u305f\u3081\u3001\u3054\u5b58\u77e5\u306e\u65b9\u3044\u308c\u3070\u3001\u3053\u3063\u305d\u308a\u6559\u3048\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u3067Lua\u3092\u4f7f\u3063\u305f\u306e\u3067\u5099\u5fd8\u9332\u3010PHP\u30b5\u30f3\u30d7\u30eb\u4ed8\u304d\u3011 &nbsp; \u306a\u305cLua\u3092\u4f7f\u3046\u306e\u304b Lua\u3092\u4f7f\u3046\u3068\u3001Redis\u306b\u5b58\u5728\u3057\u3066\u3044\u308b\u30b3\u30de\u30f3\u30c9\u3067\u5b9f\u73fe\u3067\u304d\u306a\u3044\u52d5\u4f5c\u3092\u5b9f\u73fe\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002 \u3053\u308c\u306b\u306f\u3001\u300cRedis Eval\u300d\u306a\u3069\u3068\u691c\u7d22\u3059\u308b\u3068\u2026<\/p>\n","protected":false},"author":3,"featured_media":2624,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[67,83],"tags":[],"class_list":["post-2619","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","category-redis"],"_links":{"self":[{"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/posts\/2619","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/comments?post=2619"}],"version-history":[{"count":0,"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/posts\/2619\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/media\/2624"}],"wp:attachment":[{"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/media?parent=2619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/categories?post=2619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/took.jp\/tech-blog\/wp-json\/wp\/v2\/tags?post=2619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}