|
24 | 24 | */ |
25 | 25 | class RedisHandler extends BaseHandler |
26 | 26 | { |
| 27 | + /** |
| 28 | + * Lua script to execute HMSET and EXPIREAT in a single round trip. |
| 29 | + * |
| 30 | + * Both commands run through pcall and their error reply is returned as-is, |
| 31 | + * so a failing HMSET never runs EXPIREAT and the original error message and |
| 32 | + * type are preserved. phpredis exposes the message through |
| 33 | + * Redis::getLastError(); save() rethrows a permission error as |
| 34 | + * RedisException, as the sequential implementation did, and keeps reporting |
| 35 | + * any other error (e.g. WRONGTYPE) as false. |
| 36 | + * |
| 37 | + * If HMSET succeeds and EXPIREAT is rejected, the hash is stored without a |
| 38 | + * TTL, exactly as with the previous sequential implementation (Redis does |
| 39 | + * not roll back script effects). |
| 40 | + */ |
| 41 | + private const SAVE_SCRIPT = <<<'LUA' |
| 42 | + local res = redis.pcall('HMSET', KEYS[1], '__ci_type', ARGV[2], '__ci_value', ARGV[3]) |
| 43 | + if type(res) == 'table' and res.err then |
| 44 | + return res |
| 45 | + end |
| 46 | + local exp = redis.pcall('EXPIREAT', KEYS[1], ARGV[1]) |
| 47 | + if type(exp) == 'table' and exp.err then |
| 48 | + return exp |
| 49 | + end |
| 50 | + return 1 |
| 51 | + LUA; |
| 52 | + |
27 | 53 | /** |
28 | 54 | * Default config |
29 | 55 | * |
@@ -134,17 +160,92 @@ public function save(string $key, mixed $value, int $ttl = 60): bool |
134 | 160 | return false; |
135 | 161 | } |
136 | 162 |
|
137 | | - if (! $this->redis->hMset($key, ['__ci_type' => $dataType, '__ci_value' => $value])) { |
| 163 | + $fields = ['__ci_type' => $dataType, '__ci_value' => $value]; |
| 164 | + |
| 165 | + if ($ttl === 0) { |
| 166 | + return (bool) $this->redis->hMset($key, $fields); |
| 167 | + } |
| 168 | + |
| 169 | + $expiresAt = Time::now()->getTimestamp() + $ttl; |
| 170 | + |
| 171 | + $shouldFallback = false; |
| 172 | + |
| 173 | + $this->redis->clearLastError(); |
| 174 | + |
| 175 | + try { |
| 176 | + $result = $this->redis->eval( |
| 177 | + self::SAVE_SCRIPT, |
| 178 | + [$key, (string) $expiresAt, $dataType, (string) $value], |
| 179 | + 1, |
| 180 | + ); |
| 181 | + |
| 182 | + if ($result !== false) { |
| 183 | + return (bool) $result; |
| 184 | + } |
| 185 | + |
| 186 | + // phpredis reports server errors as false and keeps the message in getLastError(). |
| 187 | + $error = $this->redis->getLastError(); |
| 188 | + |
| 189 | + if (is_string($error)) { |
| 190 | + if (self::isEvalUnavailable($error)) { |
| 191 | + $shouldFallback = true; |
| 192 | + } elseif (self::isDataCommandPermissionError($error)) { |
| 193 | + // HMSET or EXPIREAT was denied by an ACL: the script itself ran, |
| 194 | + // so keep throwing exactly like the sequential implementation did. |
| 195 | + throw new RedisException($error); |
| 196 | + } |
| 197 | + } |
| 198 | + } catch (RedisException $e) { |
| 199 | + if (! self::isEvalUnavailable($e->getMessage())) { |
| 200 | + throw $e; |
| 201 | + } |
| 202 | + |
| 203 | + $shouldFallback = true; |
| 204 | + } |
| 205 | + |
| 206 | + if (! $shouldFallback) { |
138 | 207 | return false; |
139 | 208 | } |
140 | 209 |
|
141 | | - if ($ttl !== 0) { |
142 | | - $this->redis->expireAt($key, Time::now()->getTimestamp() + $ttl); |
| 210 | + // EVAL is unavailable (renamed command or ACL denial): run both commands sequentially. |
| 211 | + if (! $this->redis->hMset($key, $fields)) { |
| 212 | + return false; |
143 | 213 | } |
144 | 214 |
|
| 215 | + $this->redis->expireAt($key, $expiresAt); |
| 216 | + |
145 | 217 | return true; |
146 | 218 | } |
147 | 219 |
|
| 220 | + /** |
| 221 | + * Checks whether the message only means that the EVAL command itself is not |
| 222 | + * available to this client, either because it was renamed or because ACL denies it. |
| 223 | + * |
| 224 | + * Keep the patterns in sync with PredisHandler::isEvalUnavailable(). |
| 225 | + */ |
| 226 | + private static function isEvalUnavailable(string $message): bool |
| 227 | + { |
| 228 | + return preg_match("/unknown command [`']?eval\\b/i", $message) === 1 |
| 229 | + || preg_match("/no permissions to run the [`']eval[`'] command/i", $message) === 1; |
| 230 | + } |
| 231 | + |
| 232 | + /** |
| 233 | + * Checks whether the message means that a data command executed inside the |
| 234 | + * save script was denied by an ACL. |
| 235 | + * |
| 236 | + * EVAL availability was already checked, so the denied command can only be |
| 237 | + * HMSET or EXPIREAT. Redis reports it either as a command level denial or as |
| 238 | + * an ACL failure while running the script. |
| 239 | + * |
| 240 | + * Predis throws for every error reply, so this mapping only exists for the |
| 241 | + * phpredis false plus getLastError() path. |
| 242 | + */ |
| 243 | + private static function isDataCommandPermissionError(string $message): bool |
| 244 | + { |
| 245 | + return preg_match("/no permissions to run the [`']?[a-z]+[`'] command/i", $message) === 1 |
| 246 | + || preg_match('/ACL failure in script/i', $message) === 1; |
| 247 | + } |
| 248 | + |
148 | 249 | public function delete(string $key): bool |
149 | 250 | { |
150 | 251 | $key = static::validateKey($key, $this->prefix); |
|
0 commit comments