348 Commits

Author SHA1 Message Date
Pavlo Yatsukhnenko c2d2254e56 Switch to Fast Parameter Parsing API 2026-06-04 13:25:11 -07:00
michael-grunder 2c5ef19257 Introduce new RedisCmd based command construction
* Introduce a new `RedisCmd` struct to dynamically append RESP arguments
  such that we don't have to precalculate the number of arguments the
  command will have up front.

  Additionally the new `RedisCmd allows both a `void *` context pointer
  but also can attach a `void (*ctx_dtor)(void*)` destructor so we are
  still able to clean up any allocated context when commands fail.

  This moves the context cleanup out of every individual reply handler
  and into the generic processing wrappers.

* Create a small group of `resp_str` helper functions for lower level
  concatination of RESP protocol data over the wire.

* Lots of small modernization of the codebase such as using
  `zend_string*` instead of (`char *`, `size_t`) pairs.

* Greatly simplify `crosslot` handling logic
2026-06-04 12:16:35 -07:00
michael-grunder 8b1280f3cd fix: Reject redirection hosts that cannot fit in our buffer
Previously a corrupted or malicious `MOVED` response could embed a host
name that was larger than the `c->redir_host` buffer which could leave
it non null-terminated.

Worse, `c->redir_host_len` was calculated from the too-large input which
could cause subsequent use to memcpy past the end of our buffer.

This fix simply hard rejects any host that we can't store in
`c->redir_host` while including a null terminator.

In addition we swich from a statically sized buffer in
`RedisCluster::_redir` to using `zend_smart_str`
2026-05-21 12:14:08 -07:00
michael-grunder 10b77a42d6 Implement GCRA command 2026-03-25 11:03:42 -07:00
derrickschoen 409508afa2 fix: Accept null for $seeds in RedisCluster::__construct
The stub declares $seeds as ?array but the C code used format
specifier 'a' (non-nullable) instead of 'a!' in
zend_parse_method_parameters. This caused new RedisCluster(null, null)
to throw TypeError instead of RedisClusterException, contradicting
the declared type signature.

Also treat z_seeds == NULL the same as ZEND_NUM_ARGS() < 2 so that
explicitly passing null falls through to INI-based seed loading,
matching the behaviour when the argument is omitted entirely.

Fixes GH-2810.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-26 14:35:36 -08:00
michael-grunder b97951cddc Rework TLS context logic
Instead of currying around a `php_stream_context` object, just retain
the context array provided by the user itself like we do with other
connection information like host and port. This lets users reconnect in
a loop without leaking memory.

```php
$redis = new \Redis;
while (true) {
    // Previously each reconnect call would leak the
    // `php_stream_context` structure.
    $redis->connect('tls://127.0.0.1', 9999, 1, null, 0, 0, [
        'stream' => ['verify_peer' => false, 'verify_peer_name' => false],
    ]);

    $redis->ping();

    $redis->close();
}
```
2026-02-18 09:46:55 -08:00
Niels Dossche 3401462e00 Replace lagcy WRONG_PARAM_COUNT macro with its definition
Depending on the version this can either throw or not, so we don't port
over the RETURN_THROWS() part of its definition.
2025-11-16 14:07:06 -08:00
Niels Dossche 7d4fa6137c Replace legacy ZEND_WRONG_PARAM_COUNT() alias with zend_wrong_param_count() 2025-11-16 14:07:06 -08:00
Niels Dossche d24528e190 Replace legacy zval_dtor() alias with zval_ptr_dtor_nogc() 2025-11-16 14:07:06 -08:00
michael-grunder 635d87d535 Implement XDELEX command 2025-11-12 09:14:43 -08:00
michael-grunder 41922b6740 Implement MSETEX for RedisCluster
We also need to revisit the multi key command logic (MSET, DEL, and
friends). We may want to create a "per slot" distribution mechanism.
2025-11-12 08:41:21 -08:00
michael-grunder 00c62de277 Implement DELEX command 2025-11-06 09:48:09 -08:00
michael-grunder e2dd13ce7b Implement DIGEST and _digest helper (for php >= 8.1)
Redis implemented new CAS semantics which work both with values and the
XXH3 digest of those values.

This commit implements the Redis command itself and a helper which
computes the XXH3 digest locally. Note that we can only be sure to have
the `XXH3` hashing algorithm in PHP >= 8.1 so the `_digest` helper is
limited to PHP 8.1 or newer.
2025-11-06 09:48:09 -08:00
michael-grunder 6e214c1698 Cloning our objects should not segfault
In a future release we can actually implement cloning logic along with
lazy reconnection but for now, just throw a fatal error.

Fixes #1760
2025-10-31 11:09:45 -07:00
michael-grunder 4de4f727a4 WAIT and WAITAOF 2025-10-30 10:26:18 -07:00
Pavlo Yatsukhnenko 7d3b2e4d6d Add hGetWithMeta method 2025-10-06 16:22:59 -07:00
michael-grunder 6ce3bd533a Implement VRANGE command and add a test 2025-10-02 11:12:39 -07:00
michael-grunder 92137ffd3f We actually do return bool in sismember so do the same here 2025-09-01 09:41:12 -07:00
michael-grunder 38115decb9 Fix VISMEMBER unit test 2025-09-01 09:41:12 -07:00
michael-grunder 92dd256f98 Implement VISMEMBER command. 2025-09-01 09:41:12 -07:00
michael-grunder c4b2ea6ca5 Fix VEMB reply handling in RedisCluster 2025-09-01 09:41:12 -07:00
michael-grunder d80b725824 Implement VGETATTR command 2025-09-01 09:41:12 -07:00
michael-grunder 7f9b1f416e Implement VLINKS command 2025-09-01 09:41:12 -07:00
michael-grunder 65927b53b1 We can use redis_kv_cmd instead of a specific vrem command. 2025-09-01 09:41:12 -07:00
michael-grunder dc91631b3f Implement VREM command
See #2543
2025-09-01 09:41:12 -07:00
michael-grunder 1deca62841 Implement VRANDMEMBER
`VRANDMEMBER` has the exact same semantics of `SRANDMEMBER` so make
`SRANDMEMBER` a keyword based command and use it for `VRANDMEMBER`.

See #2543
2025-09-01 09:41:12 -07:00
michael-grunder 96378b70fd Implement VEMB and slightly rework VINFO
Unfortunately `VEMB` has a unique `RESP2` reply as far as I can tell,
where it sends the embedding mode (int8, bin, fp32) as a simple string.

This would cause any of PhpRedis' generic reply handlers to turn that
into `true` which isn't useful. For that reason we need a custom reply
handler.

Additionally slightly rework `VINFO` to short circuit and return failure
if we read anything other than a bulk string or an integer reply type.
Otherwise we may get out of sync on the socket.

See #2543
2025-09-01 09:41:12 -07:00
michael-grunder 0fda9f293b Implement VCARD, VDIM, and VINFO
All of these commands have the same form `<cmd> key`. `VINFO` is a bit
of an outlier however that uses simple strings as opposed to bulk
strings for the key names, meaning we had to create a custom handler.

See #2543
2025-09-01 09:41:12 -07:00
michael-grunder b1b0c19142 Implement DELIFEQ command
Implement the command and add a test.
2025-08-24 06:37:28 -07:00
Michael Grunder 8685c49c70 Use continue not break if we get a NULL node
This is likely to never happen but just skipping NULL nodes is better than aborting the reset.

Co-authored-by: Pavlo Yatsukhnenko <yatsukhnenko@users.noreply.github.com>
2025-08-21 08:53:51 -07:00
michael-grunder 03837f0230 Remove pointless casts
You never have to explicitly cast between `void*` and any other pointer
type.
2025-08-21 08:53:51 -07:00
michael-grunder c3a7163108 Rework CLUSTER_RESET_MULTI to be a static function 2025-08-21 08:53:51 -07:00
michael-grunder b004051499 Rewowrk CLUSTER_FREE_QUEUE as a static function 2025-08-21 08:53:51 -07:00
michael-grunder f880e1f727 Make CLUSTER_ENQUEUE_RESPONSE a static function 2025-08-21 08:53:51 -07:00
michael-grunder b90e27f285 Rework CLUSTER_PROCESS_KW_CMD to be a small wrapper macro + function
This commit is similar to the last one reworking processing keyword
commands to work to use a function instead of a big multiline macro.
2025-08-21 08:53:51 -07:00
michael-grunder 1db3908914 Rework CLUSTER_PROCESS_CMD to use an underlying function
In theory this should reduce PhpRedis' code size and likely doesn't
affect performance in a measurable way.
2025-08-21 08:53:51 -07:00
michael-grunder d1d690053f Implement VSIM command
This command is similar to `VADD` in that it's pretty simple but allows
for a great many options.

In it's most basic form:

```php
// To get similarity of a different element
$redis->vsim('myvec', 'some-element');

// To get similarity for a vector of scores
```

As seen above the method attempts to infer element or vector from the
argument passed to $member`. However, since we do serialize the member
when doing `ELE` mode, the user can also specify `ELE` explicitly in the
options array to force an `ELE` search sending serialized values.

```php
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
$redis->vsim('myvec', [3.14, 2.71], ['ELE']);
```

See #2543
2025-07-31 08:30:47 -07:00
michael-grunder 286fa63064 Implement VADD command
This is for Redis 8.0's vector sets.

The command itself can be quite complex with all of the various options but
pretty simple using all defaults.

```php
$redis->vadd('myvec', [3.14, 2.17], 'myelement');
```

The implementation takes a default argument `$options` which can be an array in
order to specify the myriad of other knobs users can send. We just do a bit of
validation on inputs (e.g. certain numeric options must be positive) and make
sure the command is constructed in a valid way (e.g. REDUCE <dim> must come
before the floating point values).

By default we deliver `FP32` blobs but allow the user to send `VALUES` in the
options array which will cause PhpRedis to send N individual values. Sending
values is slower but might be nice for debugging (e.g. watching monitor)

See #2543
2025-07-31 00:57:28 -07:00
Michael Grunder ce5b0facc2 Implement HGETEX, HSETEX, HGETDEL, and refactor HMGET (#2667)
* Rework HMGET and implement HGETEX

Instead of using a bespoke NULL terminated `zval**` array for the
context array we can use a `HashTable`. This might be a tiny bit more
expensive but Zend hashtables are quite efficient and this should also
be less error prone.

* Rework our `HashTable` context array to store keys

Instead of sending an array of values we can instead add the fields as
keys to our context array. That way when we combine the keys with the
Redis provided values we can do it in-place and then just give the
HashTable to the user to then do with what they want.

* Implement HGETDEL command.

* Fix edge cases to abide by legacy behavior.

Previously we coerced integer strings into integer keys when zipping
`HMGET` responses. This commit adds logic so we continue to do this and
do not change semantics.

* Implement `HGETDEL` and `HGETEX` for `RedisCluster`.

This commit implements the new commands and reworks the `HMGET` reply
handler to use the new context `HashTable`.

* Fix an edge case where we get zero multiblk elements

* Tests for `HGETEX` and `HGETDEL`

* Minor logic improvement

We don't need to check if `c->reply_len > 0` in the last else block
since we have already determined it must be.

* Implement `HSETEX` for `Redis` and `RedisCluster`

* Use `zval_get_tmp_string` ro populating non-long keys
2025-07-16 16:46:09 -07:00
michael-grunder 7350768cd9 Implement several hash expiration commands
Commands implemented:

`H[P]EXPIRE`
`H[P]TTL`
`H[P]EXPIREAT`
`H[P]EXPIRETIME`
`HPERSIST`
2025-05-07 08:16:14 -07:00
Michael Grunder 0445e683e7 Refactor getWithMeta logic (#2643)
* Refactor `getWithMeta`

* Consolidate `getWithMeta()` test.

* Review comments
2025-03-31 12:42:29 -07:00
michael-grunder d342e4ac18 Implement GETDEL for RedisCluster
Fixes #2629
2025-03-06 10:06:26 -08:00
Pavlo Yatsukhnenko 9036ffca6a Add getWithMeta method 2025-02-25 16:27:10 +02:00
michael-grunder 4cd3f59356 Implement KeyDB's EXPIREMEMBER[AT] commands 2024-11-15 08:59:10 -08:00
michael-grunder 981c69314d Add GETEX to README docs + minor change to command.
* Adds `GETEX` to the README.md documentation.
* Allow the user to send `PERSIST` either as an array key or just in the
  array, to conform with similar methods.
* Implement getEx for `RedisCluster`

Fixes #2512
2024-06-20 13:56:17 -07:00
michael-grunder 0d89e92889 Spelling fixes 2024-05-28 21:05:08 -07:00
Jozsef Koszo eb7f31e7af Fix random connection timeouts with Redis Cluster
When a node timeout occurs, then phpredis will try to connect to another
node, whose answer probably will be MOVED redirect. After this we need
more time to accomplish the redirection, otherwise we get "Timed out
attempting to find data in the correct node" error message.

Fixes #795 #888 #1142 #1385 #1633 #1707 #1811 #2407
2024-03-25 12:04:06 -07:00
michael-grunder 2612d444e5 Update RedisCluster scan logic for large SCAN cursors.
We also need to update the `RedisCluster` logic to handle very large
curosr values, in addition to handling them for the `Redis` and
`RedisArray` classes.

See #2454, #2458
2024-03-18 11:54:02 -07:00
michael-grunder fa1a283ac9 Fix some typos 2024-03-13 13:46:58 -07:00
Viktor Szépe 37c5f8d451 Fix typos 2024-02-21 13:16:12 -08:00