|
| 1 | +-- |
| 2 | +-- Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +-- contributor license agreements. See the NOTICE file distributed with |
| 4 | +-- this work for additional information regarding copyright ownership. |
| 5 | +-- The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +-- (the "License"); you may not use this file except in compliance with |
| 7 | +-- the License. You may obtain a copy of the License at |
| 8 | +-- |
| 9 | +-- http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +-- |
| 11 | +-- Unless required by applicable law or agreed to in writing, software |
| 12 | +-- distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +-- See the License for the specific language governing permissions and |
| 15 | +-- limitations under the License. |
| 16 | +-- |
| 17 | +local ipairs = ipairs |
| 18 | +local pairs = pairs |
| 19 | + |
| 20 | + |
| 21 | +local cmd_to_key_finder = {} |
| 22 | +--[[ |
| 23 | +-- the data is generated from the script below |
| 24 | +local redis = require "resty.redis" |
| 25 | +local red = redis:new() |
| 26 | +
|
| 27 | +local ok, err = red:connect("127.0.0.1", 6379) |
| 28 | +if not ok then |
| 29 | + ngx.say("failed to connect: ", err) |
| 30 | + return |
| 31 | +end |
| 32 | +
|
| 33 | +local res = red:command("info") |
| 34 | +local map = {} |
| 35 | +for _, r in ipairs(res) do |
| 36 | + local first_key = r[4] |
| 37 | + local last_key = r[5] |
| 38 | + local step = r[6] |
| 39 | + local idx = first_key .. ':' .. last_key .. ':' .. step |
| 40 | +
|
| 41 | + if idx ~= "1:1:1" then |
| 42 | + -- "1:1:1" is the default |
| 43 | + if map[idx] then |
| 44 | + table.insert(map[idx], r[1]) |
| 45 | + else |
| 46 | + map[idx] = {r[1]} |
| 47 | + end |
| 48 | + end |
| 49 | +end |
| 50 | +for _, r in pairs(map) do |
| 51 | + table.sort(r) |
| 52 | +end |
| 53 | +local dump = require('pl.pretty').dump; dump(map) |
| 54 | +--]] |
| 55 | +local key_to_cmd = { |
| 56 | + ["0:0:0"] = { |
| 57 | + "acl", |
| 58 | + "asking", |
| 59 | + "auth", |
| 60 | + "bgrewriteaof", |
| 61 | + "bgsave", |
| 62 | + "blmpop", |
| 63 | + "bzmpop", |
| 64 | + "client", |
| 65 | + "cluster", |
| 66 | + "command", |
| 67 | + "config", |
| 68 | + "dbsize", |
| 69 | + "debug", |
| 70 | + "discard", |
| 71 | + "echo", |
| 72 | + "eval", |
| 73 | + "eval_ro", |
| 74 | + "evalsha", |
| 75 | + "evalsha_ro", |
| 76 | + "exec", |
| 77 | + "failover", |
| 78 | + "fcall", |
| 79 | + "fcall_ro", |
| 80 | + "flushall", |
| 81 | + "flushdb", |
| 82 | + "function", |
| 83 | + "hello", |
| 84 | + "info", |
| 85 | + "keys", |
| 86 | + "lastsave", |
| 87 | + "latency", |
| 88 | + "lmpop", |
| 89 | + "lolwut", |
| 90 | + "memory", |
| 91 | + "module", |
| 92 | + "monitor", |
| 93 | + "multi", |
| 94 | + "object", |
| 95 | + "pfselftest", |
| 96 | + "ping", |
| 97 | + "psubscribe", |
| 98 | + "psync", |
| 99 | + "publish", |
| 100 | + "pubsub", |
| 101 | + "punsubscribe", |
| 102 | + "quit", |
| 103 | + "randomkey", |
| 104 | + "readonly", |
| 105 | + "readwrite", |
| 106 | + "replconf", |
| 107 | + "replicaof", |
| 108 | + "reset", |
| 109 | + "role", |
| 110 | + "save", |
| 111 | + "scan", |
| 112 | + "script", |
| 113 | + "select", |
| 114 | + "shutdown", |
| 115 | + "sintercard", |
| 116 | + "slaveof", |
| 117 | + "slowlog", |
| 118 | + "subscribe", |
| 119 | + "swapdb", |
| 120 | + "sync", |
| 121 | + "time", |
| 122 | + "unsubscribe", |
| 123 | + "unwatch", |
| 124 | + "wait", |
| 125 | + "xgroup", |
| 126 | + "xinfo", |
| 127 | + "xread", |
| 128 | + "xreadgroup", |
| 129 | + "zdiff", |
| 130 | + "zinter", |
| 131 | + "zintercard", |
| 132 | + "zmpop", |
| 133 | + "zunion" |
| 134 | + }, |
| 135 | + ["1:-1:1"] = { |
| 136 | + "del", |
| 137 | + "exists", |
| 138 | + "mget", |
| 139 | + "pfcount", |
| 140 | + "pfmerge", |
| 141 | + "sdiff", |
| 142 | + "sdiffstore", |
| 143 | + "sinter", |
| 144 | + "sinterstore", |
| 145 | + "ssubscribe", |
| 146 | + "sunion", |
| 147 | + "sunionstore", |
| 148 | + "sunsubscribe", |
| 149 | + "touch", |
| 150 | + "unlink", |
| 151 | + "watch" |
| 152 | + }, |
| 153 | + ["1:-1:2"] = { |
| 154 | + "mset", |
| 155 | + "msetnx" |
| 156 | + }, |
| 157 | + ["1:-2:1"] = { |
| 158 | + "blpop", |
| 159 | + "brpop", |
| 160 | + "bzpopmax", |
| 161 | + "bzpopmin" |
| 162 | + }, |
| 163 | + ["1:2:1"] = { |
| 164 | + "blmove", |
| 165 | + "brpoplpush", |
| 166 | + "copy", |
| 167 | + "geosearchstore", |
| 168 | + "lcs", |
| 169 | + "lmove", |
| 170 | + "rename", |
| 171 | + "renamenx", |
| 172 | + "rpoplpush", |
| 173 | + "smove", |
| 174 | + "zrangestore" |
| 175 | + }, |
| 176 | + ["2:-1:1"] = { |
| 177 | + "bitop" |
| 178 | + }, |
| 179 | + ["2:2:1"] = { |
| 180 | + "pfdebug" |
| 181 | + }, |
| 182 | + ["3:3:1"] = { |
| 183 | + "migrate" |
| 184 | + } |
| 185 | +} |
| 186 | +local key_finders = { |
| 187 | + ["0:0:0"] = false, |
| 188 | + ["1:-1:1"] = function (idx, narg) |
| 189 | + return 1 < idx |
| 190 | + end, |
| 191 | + ["1:-1:2"] = function (idx, narg) |
| 192 | + return 1 < idx and idx % 2 == 0 |
| 193 | + end, |
| 194 | + ["1:-2:1"] = function (idx, narg) |
| 195 | + return 1 < idx and idx < narg - 1 |
| 196 | + end, |
| 197 | + ["1:2:1"] = function (idx, narg) |
| 198 | + return idx == 2 or idx == 3 |
| 199 | + end, |
| 200 | + ["2:-1:1"] = function (idx, narg) |
| 201 | + return 2 < idx |
| 202 | + end, |
| 203 | + ["2:2:1"] = function (idx, narg) |
| 204 | + return idx == 3 |
| 205 | + end, |
| 206 | + ["3:3:1"] = function (idx, narg) |
| 207 | + return idx == 4 |
| 208 | + end |
| 209 | +} |
| 210 | +for k, cmds in pairs(key_to_cmd) do |
| 211 | + for _, cmd in ipairs(cmds) do |
| 212 | + cmd_to_key_finder[cmd] = key_finders[k] |
| 213 | + end |
| 214 | +end |
| 215 | + |
| 216 | + |
| 217 | +return { |
| 218 | + cmd_to_key_finder = cmd_to_key_finder, |
| 219 | + default_key_finder = function (idx, narg) |
| 220 | + return idx == 2 |
| 221 | + end, |
| 222 | +} |
0 commit comments