{"action":"opened","issue":{"url":"https://api.github.com/repos/nrk/predis/issues/326","repository_url":"https://api.github.com/repos/nrk/predis","labels_url":"https://api.github.com/repos/nrk/predis/issues/326/labels{/name}","comments_url":"https://api.github.com/repos/nrk/predis/issues/326/comments","events_url":"https://api.github.com/repos/nrk/predis/issues/326/events","html_url":"https://github.com/nrk/predis/issues/326","id":150005964,"number":326,"title":"How to convert `redis-cli script load $LUA_SCRIPT` into Predis methods?","user":{"login":"AarioAi","id":8835297,"avatar_url":"https://avatars.githubusercontent.com/u/8835297?v=3","gravatar_id":"","url":"https://api.github.com/users/AarioAi","html_url":"https://github.com/AarioAi","followers_url":"https://api.github.com/users/AarioAi/followers","following_url":"https://api.github.com/users/AarioAi/following{/other_user}","gists_url":"https://api.github.com/users/AarioAi/gists{/gist_id}","starred_url":"https://api.github.com/users/AarioAi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AarioAi/subscriptions","organizations_url":"https://api.github.com/users/AarioAi/orgs","repos_url":"https://api.github.com/users/AarioAi/repos","events_url":"https://api.github.com/users/AarioAi/events{/privacy}","received_events_url":"https://api.github.com/users/AarioAi/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-04-21T08:38:20Z","updated_at":"2016-04-21T08:38:20Z","closed_at":null,"body":" [The question on Stackoverflow](http://stackoverflow.com/questions/36760482/php-predis-how-to-convert-redis-cli-script-load-lua-script-into-predis-metho)\r\nHow to convert redis-cli script load $LUA_SCRIPT into Predis methods?\r\n\r\nFollow is the lua script:\r\n\r\n`   \r\n    local lock_key = 'icicle-generator-lock'\r\n    local sequence_key = 'icicle-generator-sequence'\r\n    local logical_shard_id_key = 'icicle-generator-logical-shard-id'\r\n\r\n    local max_sequence = tonumber(KEYS[1])\r\n    local min_logical_shard_id = tonumber(KEYS[2])\r\n    local max_logical_shard_id = tonumber(KEYS[3])\r\n    local num_ids = tonumber(KEYS[4])\r\n\r\n    if redis.call('EXISTS', lock_key) == 1 then\r\n      redis.log(redis.LOG_INFO, 'Icicle: Cannot generate ID, waiting for lock to expire.')\r\n      return redis.error_reply('Icicle: Cannot generate ID, waiting for lock to expire.')\r\n    end\r\n\r\n    --[[\r\n    Increment by a set number, this can\r\n    --]]\r\n    local end_sequence = redis.call('INCRBY', sequence_key, num_ids)\r\n    local start_sequence = end_sequence - num_ids + 1\r\n    local logical_shard_id = tonumber(redis.call('GET', logical_shard_id_key)) or -1\r\n\r\n    if end_sequence >= max_sequence then\r\n      --[[\r\n      As the sequence is about to roll around, we can't generate another ID until we're sure we're not in the same\r\n      millisecond since we last rolled. This is because we may have already generated an ID with the same time and\r\n      sequence, and we cannot allow even the smallest possibility of duplicates. It's also because if we roll the sequence\r\n      around, we will start generating IDs with smaller values than the ones previously in this millisecond - that would\r\n      break our k-ordering guarantees!\r\n      The only way we can handle this is to block for a millisecond, as we can't store the time due the purity constraints\r\n      of Redis Lua scripts.\r\n      In addition to a neat side-effect of handling leap seconds (where milliseconds will last a little bit longer to bring\r\n      time back to where it should be) because Redis uses system time internally to expire keys, this prevents any duplicate\r\n      IDs from being generated if the rate of generation is greater than the maximum sequence per millisecond.\r\n      Note that it only blocks even it rolled around *not* in the same millisecond; this is because unless we do this, the\r\n      IDs won't remain ordered.\r\n      --]]\r\n      redis.log(redis.LOG_INFO, 'Icicle: Rolling sequence back to the start, locking for 1ms.')\r\n      redis.call('SET', sequence_key, '-1')\r\n      redis.call('PSETEX', lock_key, 1, 'lock')\r\n      end_sequence = max_sequence\r\n    end\r\n\r\n    --[[\r\n    The TIME command MUST be called after anything that mutates state, or the Redis server will error the script out.\r\n    This is to ensure the script is \"pure\" in the sense that randomness or time based input will not change the\r\n    outcome of the writes.\r\n    See the \"Scripts as pure functions\" section at http://redis.io/commands/eval for more information.\r\n    --]]\r\n    local time = redis.call('TIME')\r\n\r\n    return {\r\n      start_sequence,\r\n      end_sequence, -- Doesn't need conversion, the result of INCR or the variable set is always a number.\r\n      logical_shard_id,\r\n      tonumber(time[1]),\r\n      tonumber(time[2])\r\n    }\r\n`\r\nI tried\r\n`    \r\n    $predis->eval(file_get_contents($luaPath), 0);\r\n`\r\nand\r\n`\r\nclass ListPushRandomValue extends \\Predis\\Command\\ScriptCommand {\r\n    public function getKeysCount() {\r\n        return 0;\r\n    }\r\n\r\n    public function getScript() {\r\n        $luaPath = Aa::$vendorRoot .'/icicle/id-generation.lua';\r\n        return file_get_contents($luaPath);\r\n    }\r\n}\r\n$predis->getProfile()->defineCommand('t', '\\Controller\\ListPushRandomValue');\r\n\r\n$response = $predis->t();\r\n`\r\n\r\nBut both of above showed error below.\r\n\r\n`\r\nERR Error running script (call to f_5849d008682280eed4ec67b97ba50ae546fc5e8d): @user_script:19: user_script:19: attempt to perform arithmetic on local 'end_sequence' (a table value)\r\n`"}}