diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 14e8018e3d..3288d59d63 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -186,13 +186,25 @@ jobs: - name: Wait for ${{ matrix.server }} instances run: | + WAIT_TIMEOUT_SECONDS=30 + for PORT in {6379..6382} {7000..7005} {32767..32768} {26379..26380}; do + START_TIME=$SECONDS until echo PING | ${{ matrix.server }}-cli -p "$PORT" 2>&1 | grep -qE 'PONG|NOAUTH'; do + if (( SECONDS - START_TIME >= WAIT_TIMEOUT_SECONDS )); then + echo "Timed out waiting for ${{ matrix.server }} on port $PORT after ${WAIT_TIMEOUT_SECONDS}s" + exit 1 + fi echo "Still waiting for ${{ matrix.server }} on port $PORT" sleep .5 done done + START_TIME=$SECONDS until echo PING | ${{ matrix.server }}-cli -s /tmp/redis.sock 2>&1 | grep -qE 'PONG|NOAUTH'; do + if (( SECONDS - START_TIME >= WAIT_TIMEOUT_SECONDS )); then + echo "Timed out waiting for ${{ matrix.server }} at /tmp/redis.sock after ${WAIT_TIMEOUT_SECONDS}s" + exit 1 + fi echo "Still waiting for ${{ matrix.server }} at /tmp/redis.sock" sleep .5 done @@ -231,7 +243,7 @@ jobs: strategy: fail-fast: false matrix: - php: ['7.4', '8.0', '8.1', '8.2', '8.3', '8.4'] + php: ['7.4', '8.0', '8.1', '8.2', '8.3', '8.4', '8.5'] steps: - name: Checkout uses: actions/checkout@v4 @@ -253,49 +265,29 @@ jobs: phpize ./configure --enable-redis-lzf --enable-redis-zstd --enable-redis-igbinary --enable-redis-msgpack --enable-redis-lz4 --with-liblz4 sudo make install + sudo mkdir -p "$(php-config --ini-dir)" echo 'extension = redis.so' | sudo tee -a "$(php-config --ini-dir)/90-redis.ini" windows: - runs-on: windows-latest + runs-on: windows-2022 continue-on-error: false strategy: fail-fast: false matrix: - php: ['8.0', '8.1', '8.2', '8.3', '8.4'] + php: ['8.0', '8.1', '8.2', '8.3', '8.4', '8.5'] ts: [nts, ts] steps: - name: Checkout - uses: actions/checkout@v4 + uses: actions/checkout@v5 with: submodules: true - - name: Install PHP ${{ matrix.php }} - uses: php/setup-php-sdk@v0.10 - id: setup-php-sdk - with: - version: ${{ matrix.php }} - arch: x64 - ts: ${{matrix.ts}} - cache: true - - name: Install dependencies - uses: ilammy/msvc-dev-cmd@v1 - with: - arch: x64 - toolset: ${{steps.setup-php-sdk.outputs.toolset}} - name: Build phpredis - run: | - phpize - ./configure --enable-redis --with-prefix=${{steps.setup-php-sdk.outputs.prefix}} - nmake - - name: package - run: | - md binaries - copy LICENSE binaries - Get-ChildItem -Recurse -Filter "php_redis.dll" | ForEach-Object {Copy-Item -Path $_.FullName -Destination "binaries"} - - name: Upload artifacts - uses: actions/upload-artifact@v4 + uses: php/php-windows-builder/extension@v1 with: - name: redis-${{matrix.php}}-x64-${{matrix.ts}} - path: binaries + arch: x64 + php-version: ${{ matrix.php }} + ts: ${{ matrix.ts }} + run-tests: false pecl: runs-on: ubuntu-latest diff --git a/.gitignore b/.gitignore index f09ec40423..fa1d633688 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,13 @@ /.idea /.vscode +.cache /docs/.cache .cquery +*.dep *.deps *.libs *.o +*.la *.lo Makefile* configure* @@ -21,3 +24,4 @@ tags compile_commands.json doctum.phar run-tests.php +vendor/ diff --git a/CHANGELOG.md b/CHANGELOG.md index c07b485e06..1815dd32b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ All changes to phpredis will be documented in this file. We're basing this format on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and PhpRedis adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -# [6.3.0RC1] - 2025-10-15 +# [6.3.0RC1] - 2025-10-15 ([GitHub](https://github.com/phpredis/phpredis/releases/tag/6.3.0RC1), [PECL](https://pecl.php.net/package/redis/6.3.0RC1)) This release introduces support for dozens of new commands, including hash field expiration, Valkey’s DELIFEQ, and Redis vector set operations. It also delivers numerous bug fixes and performance improvements. diff --git a/README.md b/README.md index 944d36ae89..b56b7f539a 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ The best way to support the project is through [GitHub Sponsors](https://github. * [Pub/sub](#pubsub) * [Transactions](#transactions) * [Scripting](#scripting) + * [Local Helper Methods](#local-helper-methods) * [Introspection](#introspection) ## Installing/Configuring @@ -74,7 +75,8 @@ phpredis can be used to store PHP sessions. To do this, configure `session.save_ * weight (integer): the weight of a host is used in comparison with the others to customize the session distribution on several hosts. If host A has twice the weight of host B, it will get twice the amount of sessions. In the example, *host1* stores 20% of all the sessions (1/(1+2+2)) while *host2* and *host3* each store 40% (2/(1+2+2)). The target host is determined once and for all at the start of the session, and doesn't change. The default weight is 1. * timeout (float): the connection timeout to a redis host, expressed in seconds. If the host is unreachable in that amount of time, the session storage will be unavailable for the client. The default timeout is very high (86400 seconds). -* persistent (integer, should be 1 or 0): defines if a persistent connection should be used. +* read_timeout (float): the timeout for read operations on an established connection, expressed in seconds. If a Redis response is not received within this time, the session operation fails. The default is `0` (no timeout), meaning PHP can hang indefinitely if Redis becomes unresponsive or a connection goes stale. Setting this to a low value (e.g. `2.5`) is strongly recommended in production environments. +* persistent (integer, should be 1 or 0): defines if a persistent connection should be used. The default value is `0` (persistent connection is not used). * prefix (string, defaults to "PHPREDIS_SESSION:"): used as a prefix to the Redis key in which the session is stored. The key is composed of the prefix followed by the session ID. * auth (string, or an array with one or two elements): used to authenticate with the server prior to sending commands. * database (integer): selects a different database. @@ -161,7 +163,7 @@ php tests/TestRedis.php --class Redis --test echo ## API Documentation -The [API Documentation](https://phpredis.github.io/phpredis) are a work in progress, but will eventually replace our **ONE README TO RULE THEM ALL** docs. +The [API Documentation](https://phpredis.github.io/phpredis) are a work in progress, but will eventually replace our **ONE README TO RULE THEM ALL** docs. ## Classes and methods @@ -204,15 +206,15 @@ $redis = new Redis([ ###### *Parameters* (optional) -*host*: string, the hostname/FQDN/IP address or the path to a unix domain socket; since v5.0.0 it is possible to specify schema -*port*: int, actual port (e.g. `6379`) or `-1` (or `0`) for unix domain socket; setting to < `0` with non-UDS will assume default of `6379` -*connectTimeout*: float, value in seconds (default is 0 meaning unlimited) -*retryInterval*: int, value in milliseconds (optional) -*readTimeout*: float, value in seconds (default is 0 meaning unlimited) +*host*: string, the hostname/FQDN/IP address or the path to a unix domain socket; since v5.0.0 it is possible to specify schema +*port*: int, actual port (e.g. `6379`) or `-1` (or `0`) for unix domain socket; setting to < `0` with non-UDS will assume default of `6379` +*connectTimeout*: float, value in seconds (default is 0 meaning unlimited) +*retryInterval*: int, value in milliseconds (optional) +*readTimeout*: float, value in seconds (default is 0 meaning unlimited) *persistent*: mixed, If the value is a string it is used as a persistent ID. Non-strings are cast to boolean. *auth*: mixed, authentication information *database*: int, database number -*ssl*: array, SSL context options +*ssl*: array, SSL context options #### Class RedisException ----- @@ -231,11 +233,11 @@ Redis::REDIS_SET - Set Redis::REDIS_LIST - List Redis::REDIS_ZSET - Sorted set Redis::REDIS_HASH - Hash +Redis::REDIS_STREAM - Stream +Redis::REDIS_VECTORSET - Vector set Redis::REDIS_NOT_FOUND - Not found / other ~~~ -@TODO: OPT_SERIALIZER, AFTER, BEFORE,... - ### Connection 1. [connect, open](#connect-open) - Connect to a server @@ -251,21 +253,21 @@ Redis::REDIS_NOT_FOUND - Not found / other #### connect, open ----- -_**Description**_: Connects to a Redis instance. +_**Description**_: Connects to a Redis instance. If phpredis cannot reach the server it will usually throw a [`RedisException`](#class-redisexception); only rarely will `FALSE` be returned without an exception. ###### *Parameters* -*host*: string, *host*: string, the hostname/FQDN/IP address or the path to a unix domain socket; since v5.0.0 it is possible to specify schema -*port*: int, actual port (e.g. `6379`) or `-1` (or `0`) for unix domain socket; setting to < `0` with non-UDS will assume default of `6379`, optional -*timeout*: float, value in seconds (optional, default is 0 meaning it will use default_socket_timeout) -*reserved*: should be '' if retry_interval is specified -*retry_interval*: int, value in milliseconds, optional -*read_timeout*: float, value in seconds (optional, default is 0 meaning it will use default_socket_timeout) -*others*: array, with PhpRedis >= 5.3.0, it allows setting _auth_ and [_stream_](https://www.php.net/manual/en/context.ssl.php) configuration. +*host*: string, *host*: string, the hostname/FQDN/IP address or the path to a unix domain socket; since v5.0.0 it is possible to specify schema +*port*: int, actual port (e.g. `6379`) or `-1` (or `0`) for unix domain socket; setting to < `0` with non-UDS will assume default of `6379`, optional +*timeout*: float, value in seconds (optional, default is 0 meaning it will use default_socket_timeout) +*reserved*: should be '' if retry_interval is specified +*retry_interval*: int, value in milliseconds, optional +*read_timeout*: float, value in seconds (optional, default is 0 meaning it will use default_socket_timeout) +*others*: array, with PhpRedis >= 5.3.0, it allows setting _auth_ and [_stream_](https://www.php.net/manual/en/context.ssl.php) configuration. ###### *Return value* -*BOOL*: `TRUE` on success, `FALSE` on error. +*BOOL*: `TRUE` on success. Failures typically raise a `RedisException`, though in some edge cases `FALSE` may be returned instead. ###### *Example* @@ -281,8 +283,43 @@ $redis->connect('/tmp/redis.sock', 0, 1.5, NULL, 0, 1.5); // Unix socket with 1. /* With PhpRedis >= 5.3.0 you can specify authentication and stream information on connect */ $redis->connect('127.0.0.1', 6379, 1, '', 0, 0, ['auth' => ['phpredis', 'phpredis']]); + +/* TLS connections can customise the underlying PHP stream context */ +$redis->connect('tls://redis.example.com', 6380, 1.5, null, 0, 0, [ + 'auth' => ['app-user', 'strong-password'], + 'stream' => [ + 'verify_peer' => true, // validate the server certificate against cafile/capath + 'verify_peer_name' => true, // require the certificate common/SAN name to match peer_name + 'peer_name' => 'redis.example.com', // expected hostname presented by the server certificate + 'cafile' => '/etc/ssl/redis-ca.pem', // CA or bundle used to trust the server certificate + 'capath' => '/etc/ssl/certs', // directory alternative to cafile + 'allow_self_signed' => false, // set to true if you rely on a self-signed certificate + 'local_cert' => '/etc/ssl/client.crt', // client certificate for mutual TLS (optional) + 'local_pk' => '/etc/ssl/client.key', // private key that matches local_cert (optional) + 'passphrase' => 'secret', // passphrase for local_pk if it is encrypted (optional) + 'ciphers' => 'HIGH:!aNULL:!MD5', // TLS cipher list provided to OpenSSL (optional) + ], +]); + +try { + $redis->connect('redis.invalid', 6379); +} catch (RedisException $ex) { + echo "Connection failed: {$ex->getMessage()}"; +} ~~~ +When you pass a `stream` key PhpRedis forwards the options to [`stream_socket_client`](https://www.php.net/manual/en/context.ssl.php). +Commonly used options include: + +- `verify_peer`, `verify_peer_name`, `peer_name`: control server certificate validation behaviour. +- `cafile`/`capath`: provide the trusted certificate authority bundle when the default store is insufficient. +- `allow_self_signed`: permits self-signed certificates when set to `true`. +- `local_cert`, `local_pk`, `passphrase`: configure client-side certificates for mutual TLS. +- `ciphers`: restrict the negotiated TLS cipher suites. +- Any other SSL context option supported by PHP (e.g. `SNI_enabled`, `disable_compression`) can also be supplied. + +The same array format can be used with `pconnect`/`popen`. + **Note:** `open` is an alias for `connect` and will be removed in future versions of phpredis. #### pconnect, popen @@ -303,12 +340,12 @@ persistent equivalents. ###### *Parameters* -*host*: string, *host*: string, the hostname/FQDN/IP address or the path to a unix domain socket; since v5.0.0 it is possible to specify schema -*port*: int, actual port (e.g. `6379`) or `-1` (or `0`) for unix domain socket; setting to < `0` with non-UDS will assume default of `6379`, optional -*timeout*: float, value in seconds (optional, default is 0 meaning it will use default_socket_timeout) -*persistent_id*: string. identity for the requested persistent connection -*retry_interval*: int, value in milliseconds (optional) -*read_timeout*: float, value in seconds (optional, default is 0 meaning it will use default_socket_timeout) +*host*: string, *host*: string, the hostname/FQDN/IP address or the path to a unix domain socket; since v5.0.0 it is possible to specify schema +*port*: int, actual port (e.g. `6379`) or `-1` (or `0`) for unix domain socket; setting to < `0` with non-UDS will assume default of `6379`, optional +*timeout*: float, value in seconds (optional, default is 0 meaning it will use default_socket_timeout) +*persistent_id*: string. identity for the requested persistent connection +*retry_interval*: int, value in milliseconds (optional) +*read_timeout*: float, value in seconds (optional, default is 0 meaning it will use default_socket_timeout) ###### *Return value* @@ -371,18 +408,18 @@ See method for example: [move](#move) #### swapdb ----- -_**Description**_: Swap one Redis database with another atomically +_**Description**_: Swap one Redis database with another atomically -###### *Parameters* -*INTEGER*: db1 -*INTEGER*: db2 +###### *Parameters* +*INTEGER*: db1 +*INTEGER*: db2 -###### *Return value* +###### *Return value* `TRUE` on success and `FALSE` on failure. *Note*: Requires Redis >= 4.0.0 -###### *Example* +###### *Example* ~~~php $redis->swapdb(0, 1); /* Swaps DB 0 with DB 1 atomically */ ~~~ @@ -404,7 +441,7 @@ None. _**Description**_: Set client option. ###### *Parameters* -*parameter name* +*parameter name* *parameter value* ###### *Return value* @@ -448,7 +485,7 @@ Parameter value. ###### *Example* ~~~php -// return Redis::SERIALIZER_NONE, Redis::SERIALIZER_PHP, +// return Redis::SERIALIZER_NONE, Redis::SERIALIZER_PHP, // Redis::SERIALIZER_IGBINARY, Redis::SERIALIZER_MSGPACK or Redis::SERIALIZER_JSON $redis->getOption(Redis::OPT_SERIALIZER); ~~~ @@ -536,6 +573,8 @@ $redis->setOption(Redis::OPT_BACKOFF_CAP, 750); // backoff time capped at 750ms 1. [info](#info) - Get information and statistics about the server 1. [lastSave](#lastsave) - Get the timestamp of the last disk save 1. [save](#save) - Synchronously save the dataset to disk (wait to complete) +1. [wait](#wait) - Block until write acknowledgements from replicas +1. [waitaof](#waitaof) - Wait for local AOF fsync and replica acknowledgements 1. [slaveOf](#slaveof) - Make the server a slave of another instance, or promote it to master 1. [time](#time) - Return the current server time 1. [slowLog](#slowlog) - Access the Redis slowLog entries @@ -597,7 +636,7 @@ $redis->config(string $operation, string|array|null $key = NULL, ?string $value ~~~ ###### *Return value* -*Associative array* for `GET`, key(s) -> value(s) +*Associative array* for `GET`, key(s) -> value(s) *bool* for `SET`, `RESETSTAT`, and `REWRITE` ###### *Examples* @@ -721,6 +760,48 @@ None. $redis->save(); ~~~ +#### wait +----- +_**Description**_: Block the client until a specified number of replicas acknowledge the most recent write or until the timeout elapses. + +###### *Parameters* +*numreplicas* (int): The number of replicas that must confirm the write. +*timeout* (int): Maximum time to wait in milliseconds (`0` to wait indefinitely). + +###### *Return value* +*INT*: The number of replicas that acknowledged the write before timing out, or `FALSE` on error. + +###### *Example* +~~~php +$redis->set('user:1', 'marco'); +$confirmed = $redis->wait(1, 500); + +if ($confirmed < 1) { + // Handle the case where the write was not replicated in time. +} +~~~ + +#### waitaof +----- +_**Description**_: Block until Redis has durably written recent changes to the local AOF file the requested number of times and received acknowledgements from the requested number of replicas (Redis >= 7.2). + +###### *Parameters* +*numlocal* (int): Minimum number of local AOF fsync operations to wait for. +*numreplicas* (int): Number of replicas that must acknowledge the write. +*timeout* (int): Maximum time to wait in milliseconds (`0` to wait indefinitely). + +###### *Return value* +*ARRAY*: Two integers, where element `0` is the number of local fsync operations performed and element `1` is the number of replica acknowledgements, or `FALSE` on error. + +###### *Example* +~~~php +[$local, $replicas] = $redis->waitaof(1, 1, 1000); + +if ($local < 1 || $replicas < 1) { + // The write was not persisted or replicated as requested. +} +~~~ + #### slaveOf ----- _**Description**_: Changes the slave status @@ -759,7 +840,7 @@ $redis->time(); _**Description**_: Access the Redis slowLog ###### *Parameters* -*Operation* (string): This can be either `GET`, `LEN`, or `RESET` +*Operation* (string): This can be either `GET`, `LEN`, or `RESET` *Length* (integer), optional: If executing a `SLOWLOG GET` command, you can pass an optional length. ##### @@ -854,7 +935,7 @@ $redis->get('key'); _**Description**_: Get the value related to the specified key and set its expiration ###### *Parameters* -*key* +*key* *options array* (optional) with the following keys: * `EX` - expire time in seconds * `PX` - expire time in milliseconds @@ -876,9 +957,9 @@ $redis->getEx('key', ['EX' => 10]); // get key and set its expiration to 10 seco _**Description**_: Set the string value in argument as value of the key. If you're using Redis >= 2.6.12, you can pass extended options as explained below ###### *Parameters* -*Key* -*Value* -*Timeout or Options Array* (optional). If you pass an integer, phpredis will redirect to SETEX, and will try to use Redis >= 2.6.12 extended options if you pass an array with valid values +*Key* +*Value* +*Timeout or Options Array* (optional). If you pass an integer, phpredis will redirect to SETEX, and will try to use Redis >= 2.6.12 extended options if you pass an array with valid values ###### *Return value* *Bool* `TRUE` if the command is successful. @@ -904,9 +985,9 @@ $redis->set('key', 'value', ['xx', 'px'=>1000]); _**Description**_: Set the string value in argument as value of the key, with a time to live. PSETEX uses a TTL in milliseconds. ###### *Parameters* -*Key* -*TTL* -*Value* +*Key* +*TTL* +*Value* ###### *Return value* *Bool* `TRUE` if the command is successful, `FALSE` in case of failure. @@ -923,8 +1004,8 @@ $redis->pSetEx('key', 100, 'value'); // sets key → value, with 0.1 sec TTL. _**Description**_: Set the string value in argument as value of the key if the key doesn't already exist in the database. ###### *Parameters* -*Key* -*Value* +*Key* +*Value* ###### *Return value* *Bool* `TRUE` if the command is successful, `FALSE` in case of failure. @@ -992,7 +1073,7 @@ $redis->exists('foo', 'bar', 'baz'); /* 3 */ _**Description**_: Increment the number stored at key by one. If the second argument is filled, it will be used as the integer value of the increment. ###### *Parameters* -*key* +*key* *value*: value that will be added to key (only for incrBy) ###### *Return value* @@ -1018,7 +1099,7 @@ $redis->incrBy('key1', 10); /* 24 */ _**Description**_: Increment the key with floating point precision. ###### *Parameters* -*key* +*key* *value*: (float) value that will be added to the key ###### *Return value* @@ -1039,7 +1120,7 @@ $redis->incrByFloat('key1', 2.5); /* 4 */ _**Description**_: Decrement the number stored at key by one. If the second argument is filled, it will be used as the integer value of the decrement. ###### *Parameters* -*key* +*key* *value*: value that will be subtracted to key (only for decrBy) ###### *Return value* @@ -1286,12 +1367,12 @@ _**Description**_: Returns the type of data pointed by a given key. ###### *Return value* -Depending on the type of the data pointed by the key, this method will return the following value: -string: Redis::REDIS_STRING -set: Redis::REDIS_SET -list: Redis::REDIS_LIST -zset: Redis::REDIS_ZSET -hash: Redis::REDIS_HASH +Depending on the type of the data pointed by the key, this method will return the following value: +string: Redis::REDIS_STRING +set: Redis::REDIS_SET +list: Redis::REDIS_LIST +zset: Redis::REDIS_ZSET +hash: Redis::REDIS_HASH other: Redis::REDIS_NOT_FOUND ###### *Example* @@ -1322,8 +1403,8 @@ $redis->get('key'); /* 'value1value2' */ _**Description**_: Return a substring of a larger string ###### *Parameters* -*key* -*start* +*key* +*start* *end* ###### *Return value* @@ -1376,7 +1457,7 @@ $redis->strlen('key'); /* 5 */ _**Description**_: Return a single bit out of a larger string ###### *Parameters* -*key* +*key* *offset* ###### *Return value* @@ -1394,8 +1475,8 @@ $redis->getBit('key', 1); /* 1 */ _**Description**_: Changes a single bit of a string. ###### *Parameters* -*key* -*offset* +*key* +*offset* *value*: bool or int (1 or 0) ###### *Return value* @@ -1414,9 +1495,9 @@ $redis->get('key'); /* chr(0x2f) = "/" = b("0010 1111") */ _**Description**_: Bitwise operation on multiple keys. ###### *Parameters* -*operation*: either "AND", "OR", "NOT", "XOR" -*ret_key*: return key -*key1* +*operation*: either "AND", "OR", "NOT", "XOR" +*ret_key*: return key +*key1* *key2...* ###### *Return value* @@ -1541,8 +1622,8 @@ $val = $redis->dump('foo'); // $val will be the Redis encoded key value ----- _**Description**_: Restore a key from the result of a DUMP operation. ###### *Parameters* -*key* string. The key name -*ttl* integer. How long the key should live (if zero, no expire will be set on the key) +*key* string. The key name +*ttl* integer. How long the key should live (if zero, no expire will be set on the key) *value* string (binary). The Redis encoded key value (from DUMP) ###### *Examples* ~~~php @@ -1559,13 +1640,13 @@ _**Description**_: Migrates a key to a different Redis instance. that version to call `migrate` with an array of keys. ###### *Parameters* -*host* string. The destination host -*port* integer. The TCP port to connect to. -*key(s)* string or array. -*destination-db* integer. The target DB. -*timeout* integer. The maximum amount of time given to this transfer. -*copy* boolean, optional. Should we send the COPY flag to redis. -*replace* boolean, optional. Should we send the REPLACE flag to redis +*host* string. The destination host +*port* integer. The TCP port to connect to. +*key(s)* string or array. +*destination-db* integer. The target DB. +*timeout* integer. The maximum amount of time given to this transfer. +*copy* boolean, optional. Should we send the COPY flag to redis. +*replace* boolean, optional. Should we send the REPLACE flag to redis ###### *Examples* ~~~php $redis->migrate('backup', 6379, 'foo', 0, 3600); @@ -1600,8 +1681,8 @@ $redis->migrate('backup', 6379, ['key1', 'key2', 'key3'], 0, 3600); ----- _**Description**_: Adds a value to the hash stored at key. ###### *Parameters* -*key* -*hashKey* +*key* +*hashKey* *value* ###### *Return value* @@ -1635,11 +1716,11 @@ $redis->hSetNx('h', 'key1', 'world'); /* FALSE, 'key1' => 'hello' in the hash at ----- _**Description**_: Gets a value from the hash stored at key. If the hash table doesn't exist, or the key doesn't exist, `FALSE` is returned. ###### *Parameters* -*key* +*key* *hashKey* ###### *Return value* -*STRING* The value, if the command executed successfully +*STRING* The value, if the command executed successfully *BOOL* `FALSE` in case of failure @@ -1663,9 +1744,9 @@ $redis->hLen('h'); /* returns 2 */ ----- _**Description**_: Removes a value from the hash stored at key. If the hash table doesn't exist, or the key doesn't exist, `FALSE` is returned. ###### *Parameters* -*key* -*hashKey1* -*hashKey2* +*key* +*hashKey1* +*hashKey2* ... ###### *Return value* @@ -1781,7 +1862,7 @@ The order is random and corresponds to redis' own internal representation of the ----- _**Description**_: Verify if the specified member exists in a key. ###### *Parameters* -*key* +*key* *memberKey* ###### *Return value* *BOOL*: If the member exists in the hash table, return `TRUE`, otherwise return `FALSE`. @@ -1796,8 +1877,8 @@ $redis->hExists('h', 'NonExistingKey'); /* FALSE */ ----- _**Description**_: Increments the value of a member from a hash by a given amount. ###### *Parameters* -*key* -*member* +*key* +*member* *value*: (integer) value that will be added to the member's value ###### *Return value* *LONG* the new value @@ -1812,8 +1893,8 @@ $redis->hIncrBy('h', 'x', 1); /* h[x] ← 2 + 1. Returns 3 */ ----- _**Description**_: Increments the value of a hash member by the provided float value ###### *Parameters* -*key* -*member* +*key* +*member* *value*: (float) value that will be added to the member's value ###### *Return value* *FLOAT* the new value @@ -1829,7 +1910,7 @@ $redis->hIncrByFloat('h', 'x', -3.0); /* returns 0.0: h[x] = 0.0 now */ ----- _**Description**_: Fills in a whole hash. Non-string values are converted to string, using the standard `(string)` cast. NULL values are stored as empty strings. ###### *Parameters* -*key* +*key* *members*: key → value array ###### *Return value* *BOOL* @@ -1844,7 +1925,7 @@ $redis->hIncrBy('user:1', 'salary', 100); // Joe earns 100 more now. ----- _**Description**_: Retrieve the values associated to the specified fields in the hash. ###### *Parameters* -*key* +*key* *memberKeys* Array ###### *Return value* *Array* An array of elements, the values of the specified fields in the hash, with the hash keys as array keys. @@ -1860,9 +1941,9 @@ $redis->hMGet('h', ['field1', 'field2']); /* returns ['field1' => 'value1', 'fie ----- _**Description**_: Scan a HASH value for members, with an optional pattern and count ###### *Parameters* -*key*: String -*iterator*: Long (reference) -*pattern*: Optional pattern to match against +*key*: String +*iterator*: Long (reference) +*pattern*: Optional pattern to match against *count*: How many keys to return in a go (only a suggestion to Redis) ###### *Return value* *Array* An array of members that match our pattern @@ -1883,7 +1964,7 @@ while($arr_keys = $redis->hScan('hash', $it)) { ----- _**Description**_: Get the string length of the value associated with field in the hash stored at key. ###### *Parameters* -*key*: String +*key*: String *field*: String ###### *Return value* *LONG* the string length of the value associated with field, or zero when field is not present in the hash or key does not exist at all. @@ -1913,14 +1994,14 @@ _**Description**_: Is a blocking lPop(rPop) primitive. If at least one of the li If all the list identified by the keys passed in arguments are empty, blPop will block during the specified timeout until an element is pushed to one of those lists. This element will be popped. ###### *Parameters* -*ARRAY* Array containing the keys of the lists -*INTEGER* Timeout -Or -*STRING* Key1 -*STRING* Key2 -*STRING* Key3 -... -*STRING* Keyn +*ARRAY* Array containing the keys of the lists +*INTEGER* Timeout +Or +*STRING* Key1 +*STRING* Key2 +*STRING* Key3 +... +*STRING* Keyn *INTEGER* Timeout ###### *Return value* @@ -1959,8 +2040,8 @@ $redis->lPush('key1', 'A'); _**Description**_: A blocking version of `rPopLPush`, with an integral timeout in the third parameter. ###### *Parameters* -*Key*: srckey -*Key*: dstkey +*Key*: srckey +*Key*: dstkey *Long*: timeout ###### *Return value* @@ -1970,17 +2051,17 @@ _**Description**_: A blocking version of `rPopLPush`, with an integral timeout i ----- _**Description**_: Return the specified element of the list stored at the specified key. -0 the first element, 1 the second ... +0 the first element, 1 the second ... -1 the last element, -2 the penultimate ... Return `FALSE` in case of a bad index or a key that doesn't point to a list. ###### *Parameters* -*key* +*key* *index* ###### *Return value* -*String* the element at this index +*String* the element at this index *Bool* `FALSE` if the key identifies a non-string data type, or no value corresponds to this index in the list `Key`. ###### *Example* @@ -2001,9 +2082,9 @@ The parameter options specify the position of the insert (before or after). If the list didn't exists, or the pivot didn't exists, the value is not inserted. ###### *Parameters* -*key* -*position* Redis::BEFORE | Redis::AFTER -*pivot* +*key* +*position* Redis::BEFORE | Redis::AFTER +*pivot* *value* ###### *Return value* @@ -2035,7 +2116,7 @@ _**Description**_: Return and remove the first element of the list. *key* ###### *Return value* -*STRING* if command executed successfully +*STRING* if command executed successfully *BOOL* `FALSE` in case of failure (empty list) ###### *Example* @@ -2075,7 +2156,7 @@ $redis->lPush('key1', 'C', 'B', 'A'); // Returns 6 _**Description**_: Adds the string value to the head (left) of the list if the list exists. ###### *Parameters* -*key* +*key* *value* String, value to push in key ###### *Return value* @@ -2093,13 +2174,13 @@ $redis->lPushx('key1', 'C'); // returns 3 #### lRange ----- -_**Description**_: Returns the specified elements of the list stored at the specified key in the range [start, end]. start and stop are interpreted as indices: -0 the first element, 1 the second ... +_**Description**_: Returns the specified elements of the list stored at the specified key in the range [start, end]. start and stop are interpreted as indices: +0 the first element, 1 the second ... -1 the last element, -2 the penultimate ... ###### *Parameters* -*key* -*start* +*key* +*start* *end* ###### *Return value* @@ -2120,12 +2201,12 @@ _**Description**_: Removes the first `count` occurrences of the value element fr **Note**: The argument order is not the same as in the Redis documentation. This difference is kept for compatibility reasons. ###### *Parameters* -*key* -*value* +*key* +*value* *count* ###### *Return value* -*LONG* the number of elements to remove +*LONG* the number of elements to remove *BOOL* `FALSE` if the value identified by key is not a list. ###### *Example* @@ -2146,8 +2227,8 @@ $redis->lRange('key1', 0, -1); /* ['C', 'B', 'A'] */ _**Description**_: Set the list at index with the new value. ###### *Parameters* -*key* -*index* +*key* +*index* *value* ###### *Return value* @@ -2168,12 +2249,12 @@ $redis->lindex('key1', 0); /* 'X' */ _**Description**_: Trims an existing list so that it will contain only a specified range of elements. ###### *Parameters* -*key* -*start* +*key* +*start* *stop* ###### *Return value* -*Array* +*Array* *Bool* return `FALSE` if the key identify a non-list value. ###### *Example* @@ -2194,7 +2275,7 @@ _**Description**_: Returns and removes the last element of the list. *key* ###### *Return value* -*STRING* if command executed successfully +*STRING* if command executed successfully *BOOL* `FALSE` in case of failure (empty list) ###### *Example* @@ -2210,7 +2291,7 @@ $redis->rPop('key1'); /* key1 => [ 'A', 'B' ] */ _**Description**_: Pops a value from the tail of a list, and pushes it to the front of another list. Also return this value. (redis >= 1.1) ###### *Parameters* -*Key*: srckey +*Key*: srckey *Key*: dstkey ###### *Return value* @@ -2275,7 +2356,7 @@ $redis->rPush('key1', 'D', 'E', 'F'); // returns 6 _**Description**_: Adds the string value to the tail (right) of the list if the list exists. `FALSE` in case of Failure. ###### *Parameters* -*key* +*key* *value* String, value to push in key ###### *Return value* @@ -2301,7 +2382,7 @@ If the list didn't exist or is empty, the command returns 0. If the data type id *Key* ###### *Return value* -*LONG* The size of the list identified by Key exists. +*LONG* The size of the list identified by Key exists. *BOOL* `FALSE` if the data type identified by Key is not list ###### *Example* @@ -2337,7 +2418,7 @@ $redis->lLen('key1');/* 2 */ ----- _**Description**_: Adds a value to the set value stored at key. ###### *Parameters* -*key* +*key* *value* ###### *Return value* @@ -2522,7 +2603,7 @@ array(2) { ----- _**Description**_: Checks if `value` is a member of the set stored at the key `key`. ###### *Parameters* -*key* +*key* *value* ###### *Return value* @@ -2574,8 +2655,8 @@ The order is random and corresponds to redis' own internal representation of the ----- _**Description**_: Moves the specified member from the set at srcKey to the set at dstKey. ###### *Parameters* -*srcKey* -*dstKey* +*srcKey* +*dstKey* *member* ###### *Return value* *BOOL* If the operation is successful, return `TRUE`. If the srcKey and/or dstKey didn't exist, and/or the member didn't exist in srcKey, `FALSE` is returned. @@ -2595,13 +2676,13 @@ $redis->sMove('key1', 'key2', 'member13'); /* 'key1' => {'member11', 'member12' ----- _**Description**_: Removes and returns a random element from the set value at Key. ###### *Parameters* -*key* +*key* *count*: Integer, optional ###### *Return value (without count argument)* -*String* "popped" value +*String* "popped" value *Bool* `FALSE` if set identified by key is empty or doesn't exist. ###### *Return value (with count argument)* -*Array*: Member(s) returned or an empty array if the set doesn't exist +*Array*: Member(s) returned or an empty array if the set doesn't exist *Bool*: `FALSE` on error if the key is not a set ###### *Example* ~~~php @@ -2620,7 +2701,7 @@ $redis->sPop('key2', 3); /* Will return all members but in no particular order * ----- _**Description**_: Returns a random element from the set value at Key, without removing it. ###### *Parameters* -*key* +*key* *count* (Integer, optional) ###### *Return value* If no count is provided, a random *String* value from the set will be returned. If a count @@ -2649,7 +2730,7 @@ $redis->sRandMember('not-a-set', 100); // Will return FALSE ----- _**Description**_: Removes the specified member from the set value stored at key. ###### *Parameters* -*key* +*key* *member* ###### *Return value* *LONG* The number of elements removed from the set. @@ -2751,9 +2832,9 @@ array(4) { _**Description**_: Scan a set for members ###### *Parameters* -*Key*: The set to search -*iterator*: LONG (reference) to the iterator as we go -*pattern*: String, optional pattern to match against +*Key*: The set to search +*iterator*: LONG (reference) to the iterator as we go +*pattern*: String, optional pattern to match against *count*: How many members to return at a time (Redis might return a different amount) ###### *Return value* @@ -2850,7 +2931,7 @@ $redis->zAdd($key, [ $options ,] $score, $value [, $score1, $value1, ...]); ###### *Parameters* *key*: string *options*: array (optional) -*score*: double +*score*: double *value*: string *score1*: double *value1*: string @@ -2892,8 +2973,8 @@ $redis->zCard('key'); /* 3 */ _**Description**_: Returns the *number* of elements of the sorted set stored at the specified key which have scores in the range [start,end]. Adding a parenthesis before `start` or `end` excludes it from the range. +inf and -inf are also valid limits. ###### *Parameters* -*key* -*start*: string +*key* +*start*: string *end*: string ###### *Return value* @@ -2914,7 +2995,7 @@ _**Description**_: Computes the difference between the first and all successive The second argument is a set of options. It can define `WITHSCORES` so that the scores are returned as well. ###### *Parameters* -*arrayZSetKeys* +*arrayZSetKeys* *arrayOptions* One option is available: `withscores => TRUE`. ###### *Return value* @@ -2948,8 +3029,8 @@ $redis->zDiff(['k3', 'k2', 'k1']); /* ['val4'] */ _**Description**_: Computes the difference between the first and all successive input sorted sets in the second argument. The result of the difference will be stored in the sorted set defined by the first argument. ###### *Parameters* -*keyOutput* -*arrayZSetKeys* +*keyOutput* +*arrayZSetKeys* ###### *Return value* *LONG* The number of values in the new sorted set. @@ -2981,8 +3062,8 @@ $redis->zdiffstore('ko4', ['k3', 'k2', 'k1']); /* 1, 'k04' => ['val4'] */ _**Description**_: Increments the score of a member from a sorted set by a given amount. ###### *Parameters* -*key* -*value*: (double) value that will be added to the member's score +*key* +*value*: (double) value that will be added to the member's score *member* ###### *Return value* @@ -3004,8 +3085,8 @@ The second optional argument defines `weights` to apply to the sorted sets in in The third argument is a set of options. It can define the `AGGREGATE` option which specify how the results of the intersection are aggregated. It can also define `WITHSCORES` so that the scores are returned as well. ###### *Parameters* -*arrayZSetKeys* -*arrayWeights* +*arrayZSetKeys* +*arrayWeights* *arrayOptions* Two options are available: `withscores => TRUE`, and `aggregate => $behaviour`. Either "SUM", "MIN", or "MAX" defines the behaviour to use on duplicate entries during the zinter. ###### *Return value* @@ -3040,9 +3121,9 @@ The third optional argument defines `weights` to apply to the sorted sets in inp The forth argument defines the `AGGREGATE` option which specify how the results of the intersection are aggregated. ###### *Parameters* -*keyOutput* -*arrayZSetKeys* -*arrayWeights* +*keyOutput* +*arrayZSetKeys* +*arrayWeights* *aggregateFunction* Either "SUM", "MIN", or "MAX": defines the behaviour to use on duplicate entries during the zinterstore. ###### *Return value* @@ -3079,7 +3160,7 @@ $redis->zinterstore('ko4', ['k1', 'k2'], [1, 5], 'max'); /* 2, 'ko4' => ['val3', _**Description**_: Returns the scores of the given members in the specified sorted set. ###### *Parameters* -*key* +*key* *members*: member1, member2, ... , memberN: Any number of members in the specified sorted set. ###### *Return value* @@ -3122,14 +3203,14 @@ $redis->zPopMax('zs1', 5); ----- _**Description**_: Returns a range of elements from the ordered set stored at the specified key, with values in the range [start, end]. -Start and stop are interpreted as zero-based indices: -`0` the first element, `1` the second ... -`-1` the last element, `-2` the penultimate ... +Start and stop are interpreted as zero-based indices: +`0` the first element, `1` the second ... +`-1` the last element, `-2` the penultimate ... ###### *Parameters* *key* -*start*: long -*end*: long +*start*: long +*end*: long *withscores*: bool = false ###### *Return value* @@ -3151,9 +3232,9 @@ $redis->zRange('key1', 0, -1, true); /* ['val0' => 0, 'val2' => 2, 'val10' => 10 _**Description**_: Returns the elements of the sorted set stored at the specified key which have scores in the range [start,end]. Adding a parenthesis before `start` or `end` excludes it from the range. +inf and -inf are also valid limits. zRevRangeByScore returns the same items in reverse order, when the `start` and `end` parameters are swapped. ###### *Parameters* -*key* -*start*: string -*end*: string +*key* +*start*: string +*end*: string *options*: array Two options are available: `withscores => TRUE`, and `limit => [$offset, $count]` @@ -3178,10 +3259,10 @@ $redis->zRangeByScore('key', '-inf', '+inf', ['withscores' => TRUE]); /* ['val0' _**Description**_: Returns a lexicographical range of members in a sorted set, assuming the members have the same score. The min and max values are required to start with '(' (exclusive), '[' (inclusive), or be exactly the values '-' (negative inf) or '+' (positive inf). The command must be called with either three *or* five arguments or will return FALSE. ###### *Parameters* -*key*: The ZSET you wish to run against -*min*: The minimum alphanumeric value you wish to get -*max*: The maximum alphanumeric value you wish to get -*offset*: Optional argument if you wish to start somewhere other than the first element. +*key*: The ZSET you wish to run against +*min*: The minimum alphanumeric value you wish to get +*max*: The maximum alphanumeric value you wish to get +*offset*: Optional argument if you wish to start somewhere other than the first element. *limit*: Optional argument if you wish to limit the number of elements returned. ###### *Return value* @@ -3202,7 +3283,7 @@ $redis->zRangeByLex('key','-','[c',1,2) /* ['b','c'] */ _**Description**_: Returns the rank of a given member in the specified sorted set, starting at 0 for the item with the smallest score. zRevRank starts at 0 for the item with the *largest* score. ###### *Parameters* -*key* +*key* *member* ###### *Return value* @@ -3242,8 +3323,8 @@ $redis->zRem('key', 'val0', 'val1', 'val2'); // Returns: 3 _**Description**_: Deletes the elements of the sorted set stored at the specified key which have rank in the range [start,end]. ###### *Parameters* -*key* -*start*: LONG +*key* +*start*: LONG *end*: LONG ###### *Return value* @@ -3263,8 +3344,8 @@ $redis->zRange('key', 0, -1, ['withscores' => TRUE]); /* ['three' => 3] */ _**Description**_: Deletes the elements of the sorted set stored at the specified key which have scores in the range [start,end]. ###### *Parameters* -*key* -*start*: double or "+inf" or "-inf" string +*key* +*start*: double or "+inf" or "-inf" string *end*: double or "+inf" or "-inf" string ###### *Return value* @@ -3280,14 +3361,14 @@ $redis->zRemRangeByScore('key', 0, 3); /* 2 */ #### zRevRange ----- -_**Description**_: Returns the elements of the sorted set stored at the specified key in the range [start, end] in reverse order. start and stop are interpreted as zero-based indices: -`0` the first element, `1` the second ... +_**Description**_: Returns the elements of the sorted set stored at the specified key in the range [start, end] in reverse order. start and stop are interpreted as zero-based indices: +`0` the first element, `1` the second ... `-1` the last element, `-2` the penultimate ... ###### *Parameters* -*key* -*start*: long -*end*: long +*key* +*start*: long +*end*: long *withscores*: bool = false ###### *Return value* @@ -3309,7 +3390,7 @@ $redis->zRevRange('key', 0, -1, true); /* ['val10' => 10, 'val2' => 2, 'val0' => _**Description**_: Returns the score of a given member in the specified sorted set. ###### *Parameters* -*key* +*key* *member* ###### *Return value* @@ -3329,8 +3410,8 @@ The second optional argument defines `weights` to apply to the sorted sets in in The third argument is a set of options. It can define the `AGGREGATE` option which specify how the results of the intersection are aggregated. It can also define `WITHSCORES` so that the scores are returned as well. ###### *Parameters* -*arrayZSetKeys* -*arrayWeights* +*arrayZSetKeys* +*arrayWeights* *arrayOptions* Two options are available: `withscores => TRUE`, and `aggregate => $behaviour`. Either "SUM", "MIN", or "MAX" defines the behaviour to use on duplicate entries during the zunion. ###### *Return value* @@ -3363,9 +3444,9 @@ The third optional argument defines `weights` to apply to the sorted sets in inp The forth argument defines the `AGGREGATE` option which specify how the results of the union are aggregated. ###### *Parameters* -*keyOutput* -*arrayZSetKeys* -*arrayWeights* +*keyOutput* +*arrayZSetKeys* +*arrayWeights* *aggregateFunction* Either "SUM", "MIN", or "MAX": defines the behaviour to use on duplicate entries during the zunionstore. ###### *Return value* @@ -3398,9 +3479,9 @@ $redis->zunionstore('ko3', ['k1', 'k2'], [5, 1]); /* 4, 'ko3' => ['val0', 'val2' _**Description**_: Scan a sorted set for members, with optional pattern and count ###### *Parameters* -*key*: String, the set to scan -*iterator*: Long (reference), initialized to NULL -*pattern*: String (optional), the pattern to match +*key*: String, the set to scan +*iterator*: Long (reference), initialized to NULL +*pattern*: String (optional), the pattern to match *count*: How many keys to return per iteration (Redis might return a different number) ###### *Return value* @@ -3424,14 +3505,14 @@ while($arr_matches = $redis->zScan('zset', $it, '*pattern*')) { _**Description**_: Adds the specified elements to the specified HyperLogLog. -###### *Prototype* +###### *Prototype* ~~~php $redis->pfAdd($key, Array $elements); ~~~ ###### *Parameters* -_Key_ -_Array of values_ +_Key_ +_Array of values_ ###### *Return value* *Integer*: 1 if at least 1 HyperLogLog internal register was altered. 0 otherwise. @@ -3447,14 +3528,14 @@ $redis->pfAdd('hll', ['a', 'b']); // (int) 0 _**Description**_: Return the approximated cardinality of the set(s) observed by the HyperLogLog at key(s). -###### *Prototype* +###### *Prototype* ~~~php $redis->pfCount($key); $redis->pfCount(Array $keys); ~~~ ###### *Parameters* -_Key_ or _Array of keys_ +_Key_ or _Array of keys_ ###### *Return value* *Integer*: The approximated number of unique elements observed via [pfAdd](#pfAdd). @@ -3475,14 +3556,14 @@ $redis->pfCount(['hll1', 'hll2']); // (int) 5 _**Description**_: Merge N different HyperLogLogs into a single one. -###### *Prototype* +###### *Prototype* ~~~php $redis->pfMerge($destkey, Array $sourceKeys); ~~~ ###### *Parameters* -_Destination Key_ -_Array of Source Keys_ +_Destination Key_ +_Array of Source Keys_ ###### *Return value* *BOOL*: `TRUE` on success, `FALSE` on error. @@ -3502,7 +3583,7 @@ $redis->pfCount('hll3'); // (int) 5 #### geoAdd ----- -###### *Prototype* +###### *Prototype* ~~~php $redis->geoAdd($key, $longitude, $latitude, $member [, $longitude, $latitude, $member, ...]); ~~~ @@ -3522,7 +3603,7 @@ $result = $redis->geoAdd( -122.431, 37.773, "San Francisco", -157.858, 21.315, "Honolulu" ); -~~~ +~~~ #### geoHash ----- @@ -3532,19 +3613,19 @@ $result = $redis->geoAdd( $redis->geoHash($key, $member [, $member, $member, ...]); ~~~ -_**Description**_: Retrieve Geohash strings for one or more elements of a geospatial index. +_**Description**_: Retrieve Geohash strings for one or more elements of a geospatial index. -###### *Return value* -*Array*: One or more Redis Geohash encoded strings. +###### *Return value* +*Array*: One or more Redis Geohash encoded strings. -###### *Example* +###### *Example* ~~~php $redis->geoAdd("hawaii", -157.858, 21.306, "Honolulu", -156.331, 20.798, "Maui"); $hashes = $redis->geoHash("hawaii", "Honolulu", "Maui"); var_dump($hashes); ~~~ -###### *Output* +###### *Output* ~~~ array(2) { [0]=> @@ -3557,24 +3638,24 @@ array(2) { #### geoPos ----- -###### *Prototype* +###### *Prototype* ~~~php $redis->geoPos($key, $member [, $member, $member, ...]); ~~~ _**Description**_: Return longitude, latitude positions for each requested member. -###### *Return value* +###### *Return value* *Array*: One or more longitude/latitude positions -###### *Example* +###### *Example* ~~~php $redis->geoAdd("hawaii", -157.858, 21.306, "Honolulu", -156.331, 20.798, "Maui"); $positions = $redis->geoPos("hawaii", "Honolulu", "Maui"); var_dump($positions); ~~~ -###### *Output* +###### *Output* ~~~ array(2) { [0]=> @@ -3594,10 +3675,10 @@ array(2) { } ~~~ -#### GeoDist +#### GeoDist ----- -###### *Prototype* +###### *Prototype* ~~~php $redis->geoDist($key, $member1, $member2 [, $unit]); ~~~ @@ -3611,7 +3692,7 @@ _**Description**_: Return the distance between two members in a geospatial set. * 'ft' => Feet ###### *Return value* -*Double*: The distance between the two passed members in the units requested (meters by default). +*Double*: The distance between the two passed members in the units requested (meters by default). ###### *Example* ~~~php @@ -3632,9 +3713,9 @@ echo " feet : $feet\n"; $inches = $redis->geoDist("hawaii", "Honolulu", "Maui", 'in'); echo "Invalid unit returned:\n"; var_dump($inches); -~~~ +~~~ -###### *Output* +###### *Output* ~~~ Distance between Honolulu and Maui: meters : 168275.204 @@ -3653,10 +3734,10 @@ bool(false) $redis->geoRadius($key, $longitude, $latitude, $radius, $unit [, Array $options]); ~~~ -_**Description**_: Return members of a set with geospatial information that are within the radius specified by the caller. +_**Description**_: Return members of a set with geospatial information that are within the radius specified by the caller. ###### *Options Array* -The georadius command can be called with various options that control how Redis returns results. The following table describes the options phpredis supports. All options are case insensitive. +The georadius command can be called with various options that control how Redis returns results. The following table describes the options phpredis supports. All options are case insensitive. | Key | Value | Description | :--- | :--- | :---- | @@ -3669,12 +3750,12 @@ The georadius command can be called with various options that control how Redis | STORE | _key_ | Store results in _key_ | STOREDIST | _key_ | Store the results as distances in _key_ - *Note*: It doesn't make sense to pass both `ASC` and `DESC` options but if both are passed the last one passed will be used. + *Note*: It doesn't make sense to pass both `ASC` and `DESC` options but if both are passed the last one passed will be used. *Note*: When using `STORE[DIST]` in Redis Cluster, the store key must has to the same slot as the query key or you will get a `CROSSLOT` error. ###### *Return value* *Mixed*: When no `STORE` option is passed, this function returns an array of results. If it is passed this function returns the number of stored entries. - + ###### *Example* ~~~php /* Add some cities */ @@ -3760,7 +3841,7 @@ _**Description**_: This method is identical to [geoRadius](#georadius) except th See [geoRadius](#georadius) command for options array. ###### *Return value* -*Array*: The zero or more entries that are close enough to the member given the distance and radius specified. +*Array*: The zero or more entries that are close enough to the member given the distance and radius specified. ###### *Example* ~~~php @@ -4125,68 +4206,106 @@ $obj_redis->xTrim('mystream', 100, true); ### Pub/sub +* [subscribe](#subscribe) - Subscribe to channels +* [unsubscribe](#unsubscribe) - Stop listening to channels * [pSubscribe](#psubscribe) - Subscribe to channels by pattern +* [punsubscribe](#punsubscribe) - Stop listening to pattern subscriptions * [publish](#publish) - Post a message to a channel -* [subscribe](#subscribe) - Subscribe to channels * [pubSub](#pubsub) - Introspection into the pub/sub subsystem -#### pSubscribe +#### subscribe ----- -_**Description**_: Subscribe to channels by pattern +_**Description**_: Subscribe to channels. Warning: this function will probably change in the future. ###### *Parameters* -*patterns*: An array of patterns to match -*callback*: Either a string or an array with an object and method. The callback will get four arguments ($redis, $pattern, $channel, $message) -*return value*: Mixed. Any non-null return value in the callback will be returned to the caller. +*channels*: an array of channels to subscribe to +*callback*: either a string or [$instance, 'method_name']. The callback function receives 3 parameters: the redis instance, the channel name, and the message. +*return value*: Mixed. Any non-null return value in the callback will be returned to the caller. + +Calling `$redis->unsubscribe()` inside the callback removes the provided channels. Once all channel subscriptions have been removed (for example by omitting the parameter so every channel is removed) PhpRedis leaves pub/sub mode and returns control to your script, which is the safest way to exit a `subscribe()` loop from within the callback. + ###### *Example* ~~~php -function pSubscribe($redis, $pattern, $chan, $msg) { - echo "Pattern: $pattern\n"; - echo "Channel: $chan\n"; - echo "Payload: $msg\n"; -} +$r = new Redis; +$r->connect('localhost', 6379); + +$r->subscribe(['channel'], function ($redis, $chan, $message) { + echo "Received {$chan}: {$message}\n"; + $redis->unsubscribe(['channel']); +}); + +echo "Done\n"; ~~~ -#### publish +#### unsubscribe ----- -_**Description**_: Publish messages to channels. Warning: this function will probably change in the future. +_**Description**_: Remove one or more channel subscriptions. When all active channel subscriptions are removed PhpRedis exits pub/sub mode. ###### *Parameters* -*channel*: a channel to publish to -*message*: string +*channels*: an array of channels to unsubscribe from. Passing an empty array, or omitting the parameter while inside an active subscription loop, unsubscribes from every channel. + +###### *Return value* +*Redis|array|bool*: Outside of a subscription loop the method returns `true` on success. When invoked from within a subscription callback the method returns the server response array (`['unsubscribe', $channel, $remaining]`) until all subscriptions have been removed. ###### *Example* ~~~php -$redis->publish('chan-1', 'hello, world!'); // send message. +$redis->unsubscribe(['first', 'second']); ~~~ -#### subscribe +#### pSubscribe ----- -_**Description**_: Subscribe to channels. Warning: this function will probably change in the future. +_**Description**_: Subscribe to channels by pattern. Warning: this function will probably change in the future. ###### *Parameters* -*channels*: an array of channels to subscribe to -*callback*: either a string or [$instance, 'method_name']. The callback function receives 3 parameters: the redis instance, the channel name, and the message. -*return value*: Mixed. Any non-null return value in the callback will be returned to the caller. +*patterns*: An array of patterns to match +*callback*: Either a string or an array with an object and method. The callback will get four arguments ($redis, $pattern, $channel, $message) +*return value*: Mixed. Any non-null return value in the callback will be returned to the caller. + +When you no longer need a pattern subscription call `$redis->punsubscribe()` from inside the callback. PhpRedis tracks the exact patterns you subscribed to and exits pub/sub mode once no patterns remain (including when the parameter is omitted to remove every pattern). + ###### *Example* ~~~php -function f($redis, $chan, $msg) { - switch($chan) { - case 'chan-1': - ... - break; +$r = new Redis; +$r->connect('localhost', 6379); - case 'chan-2': - ... - break; +$r->psubscribe(['*foo*', '*bar*'], function ($redis, $pattern, $channel, $message) { + echo "Pattern {$pattern} matched {$channel}: {$message}\n"; + $redis->punsubscribe(['*foo*', '*bar*']); +}); - case 'chan-2': - ... - break; - } -} +echo "Done\n"; +~~~ + +#### punsubscribe +----- +_**Description**_: Remove one or more pattern subscriptions that were registered with `pSubscribe()`. -$redis->subscribe(['chan-1', 'chan-2', 'chan-3'], 'f'); // subscribe to 3 chans +###### *Parameters* +*patterns*: An array of patterns to unsubscribe from. + +###### *Return value* +*Redis|array|bool*: The channels that were removed. + +###### *Example* +~~~php +$redis->punsubscribe(['orders.*']); +$redis->punsubscribe(['foo', 'bar']); +$redis->punsubscribe(['*']); +~~~ + +_**Note**_: PhpRedis does not interpret glob-style requests that differ from the patterns you originally supplied to `pSubscribe()`. For example, subscribing with `['foo']` and later calling `$redis->punsubscribe(['*'])` will send the command to Redis, but PhpRedis will still think `foo` is active and will keep invoking the callback. Always pass the same concrete patterns that were used to subscribe. + +#### publish +----- +_**Description**_: Publish messages to channels. Warning: this function will probably change in the future. + +###### *Parameters* +*channel*: a channel to publish to +*message*: string + +###### *Example* +~~~php +$redis->publish('chan-1', 'hello, world!'); // send message. ~~~ #### pubSub @@ -4194,12 +4313,12 @@ $redis->subscribe(['chan-1', 'chan-2', 'chan-3'], 'f'); // subscribe to 3 chans _**Description**_: A command allowing you to get information on the Redis pub/sub system. ###### *Parameters* -*keyword*: String, which can be: "channels", "numsub", or "numpat" +*keyword*: String, which can be: "channels", "numsub", or "numpat" *argument*: Optional, variant. For the "channels" subcommand, you can pass a string pattern. For "numsub" an array of channel names. ###### *Return value* -*CHANNELS*: Returns an array where the members are the matching channels. -*NUMSUB*: Returns a key/value array where the keys are channel names and values are their counts. +*CHANNELS*: Returns an array where the members are the matching channels. +*NUMSUB*: Returns a key/value array where the keys are channel names and values are their counts. *NUMPAT*: Integer return containing the number active pattern subscriptions ###### *Example* @@ -4299,17 +4418,14 @@ $ret = FALSE if x has been modified between the call to WATCH and the call to EX * [script](#script) - Execute the Redis SCRIPT command to perform various operations on the scripting subsystem * [getLastError](#getlasterror) - The last error message (if any) * [clearLastError](#clearlasterror) - Clear the last error message -* [_prefix](#_prefix) - A utility method to prefix the value with the prefix setting for phpredis -* [_unserialize](#_unserialize) - A utility method to unserialize data with whatever serializer is set up -* [_serialize](#_serialize) - A utility method to serialize data with whatever serializer is set up #### eval ----- _**Description**_: Evaluate a LUA script serverside ###### *Parameters* -*script* string. -*args* array, optional. +*script* string. +*args* array, optional. *num_keys* int, optional. ###### *Return value* @@ -4337,8 +4453,8 @@ In order to run this command Redis will have to have already loaded the script, either by running it or via the SCRIPT LOAD command. ###### *Parameters* -*script_sha* string. The sha1 encoded hash of the script you want to run. -*args* array, optional. Arguments to pass to the LUA script. +*script_sha* string. The sha1 encoded hash of the script you want to run. +*args* array, optional. Arguments to pass to the LUA script. *num_keys* int, optional. The number of arguments that should go into the KEYS array, vs. the ARGV array when Redis spins the script ###### *Return value* @@ -4435,33 +4551,64 @@ $err = $redis->getLastError(); // NULL ~~~ -#### _prefix +### Local Helper Methods + +These helpers expose the exact logic PhpRedis uses locally for serialization, compression, and prefixing. + +* [_compress](#_compress) - Compress a string with the configured compression option +* [_uncompress](#_uncompress) - Uncompress a string with the configured compression option +* [_serialize](#_serialize) - Serialize a value with the configured serializer +* [_unserialize](#_unserialize) - Unserialize a string with the configured serializer +* [_pack](#_pack) - Serialize and compress a value exactly as PhpRedis would before sending it to Redis +* [_unpack](#_unpack) - Reverse `_pack` to retrieve the original PHP value +* [_prefix](#_prefix) - Apply the configured key prefix locally +* [_digest](#_digest) - Compute the XXH3 digest of a packed value just like Redis' DIGEST command + +#### _compress ----- -_**Description**_: A utility method to prefix the value with the prefix setting for phpredis. +_**Description**_: Compress a string using whatever `Redis::OPT_COMPRESSION` is currently set to. This is useful when you need to mimic PhpRedis' compression logic without sending data to Redis. ###### *Parameters* -*value* string. The value you wish to prefix +*value* string. The string to compress. ###### *Return value* -If a prefix is set up, the value now prefixed. If there is no prefix, the value will be returned unchanged. +*string* Returns the compressed string, or the original value if compression is disabled. ###### *Examples* ~~~php -$redis->setOption(Redis::OPT_PREFIX, 'my-prefix:'); -$redis->_prefix('my-value'); // Will return 'my-prefix:my-value' +$redis->setOption(Redis::OPT_COMPRESSION, Redis::COMPRESSION_LZF); +$payload = $redis->_compress('large-payload'); +~~~ + +#### _uncompress +----- +_**Description**_: Reverse `_compress` by uncompressing a string with the compression option currently in use. + +###### *Parameters* +*value* string. The raw compressed value to uncompress. + +###### *Return value* +*string* Returns the uncompressed value. Throws an exception if the payload is invalid for the configured compressor. + +###### *Examples* +~~~php +$redis->setOption(Redis::OPT_COMPRESSION, Redis::COMPRESSION_LZ4); +$original = $redis->_uncompress($compressedPayload); ~~~ #### _serialize ----- -_**Description**_: A utility method to serialize values manually. +_**Description**_: Serialize a value manually using whatever serializer is configured. -This method allows you to serialize a value with whatever serializer is configured, manually. This can be useful for serialization/unserialization of data going in and out of EVAL commands -as phpredis can't automatically do this itself. Note that if no serializer is set, phpredis +as phpredis can't automatically do this itself. Note that if no serializer is set, phpredis will change Array values to 'Array', and Objects to 'Object'. ###### *Parameters* -*value*: Mixed. The value to be serialized +*value* mixed. The value to be serialized. + +###### *Return value* +*string* The serialized representation of the value. ###### *Examples* ~~~php @@ -4476,14 +4623,16 @@ $redis->_serialize("foo"); // Returns 's:3:"foo";' #### _unserialize ----- -_**Description**_: A utility method to unserialize data with whatever serializer is set up. +_**Description**_: Unserialize a string using whatever serializer is configured. -If there is no serializer set, the value will be returned unchanged. If there is a serializer set up, -and the data passed in is malformed, an exception will be thrown. This can be useful if phpredis is -serializing values, and you return something from redis in a LUA script that is serialized. +If there is no serializer set, the value will be returned unchanged. If there is a serializer set up, +and the data passed in is malformed, an exception will be thrown. ###### *Parameters* -*value* string. The value to be unserialized +*value* string. The value to be unserialized. + +###### *Return value* +*mixed* The PHP value represented by the serialized string. ###### *Examples* ~~~php @@ -4491,7 +4640,68 @@ $redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP); $redis->_unserialize('a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}'); // Will return [1,2,3] ~~~ +#### _pack +----- +_**Description**_: Pack a value the same way PhpRedis does internally by first serializing (if configured) and then compressing (if configured). This mirrors the exact bytes Redis receives. + +###### *Parameters* +*value* mixed. The PHP value to pack. + +###### *Return value* +*string* The packed payload that would be sent to Redis. + +###### *Examples* +~~~php +$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP); +$redis->setOption(Redis::OPT_COMPRESSION, Redis::COMPRESSION_LZF); +$payload = $redis->_pack(['a' => 1]); +~~~ + +#### _unpack +----- +_**Description**_: Reverse `_pack` by uncompressing and then unserializing a payload using the current PhpRedis options. + +###### *Parameters* +*value* string. The packed payload to unpack. + +###### *Return value* +*mixed* The unpacked PHP value. + +###### *Examples* +~~~php +$value = $redis->_unpack($payload); +~~~ + +#### _prefix +----- +_**Description**_: A utility method to prefix the value with the prefix setting for PhpRedis. + +###### *Parameters* +*value* string. The value you wish to prefix. +###### *Return value* +*string* Returns the prefixed value when a prefix is configured, or the original string otherwise. + +###### *Examples* +~~~php +$redis->setOption(Redis::OPT_PREFIX, 'my-prefix:'); +$redis->_prefix('my-value'); // Will return 'my-prefix:my-value' +~~~ + +#### _digest +----- +_**Description**_: Compute the XXH3 digest of a PHP value after it has been `_pack`ed. This produces the same output as Redis' `DIGEST` command, making it easy to verify values locally. + +###### *Parameters* +*value* mixed. The PHP value to digest. + +###### *Return value* +*string* A 16 character hex string containing the XXH3 digest. + +###### *Examples* +~~~php +$digest = $redis->_digest(['foo' => 'bar']); +~~~ ### Introspection diff --git a/cluster_library.c b/cluster_library.c index 96def05cd2..b342bfe3e8 100644 --- a/cluster_library.c +++ b/cluster_library.c @@ -656,8 +656,7 @@ cluster_node_create(redisCluster *c, char *host, size_t host_len, c->flags->timeout, c->flags->read_timeout, c->flags->persistent, NULL, 0); - /* Stream context */ - node->sock->stream_ctx = c->flags->stream_ctx; + redis_sock_set_context(node->sock, c->flags->context); redis_sock_set_auth(node->sock, c->flags->user, c->flags->pass); @@ -872,6 +871,8 @@ cluster_free(redisCluster *c, int free_ctx) if (c->flags->prefix) zend_string_release(c->flags->prefix); redis_sock_free_auth(c->flags); + redis_sock_free_context(c->flags); + efree(c->flags); /* Call hash table destructors */ @@ -1019,7 +1020,7 @@ void cluster_init_cache(redisCluster *c, redisCachedCluster *cc) { NULL, 0); /* Stream context */ - sock->stream_ctx = c->flags->stream_ctx; + redis_sock_set_context(sock, c->flags->context); /* Add to seed nodes */ zend_hash_str_update_ptr(c->seeds, key, keylen, sock); @@ -1074,11 +1075,9 @@ cluster_init_seeds(redisCluster *c, zend_string **seeds, uint32_t nseeds) c->flags->timeout, c->flags->read_timeout, c->flags->persistent, NULL, 0); - /* Stream context */ - sock->stream_ctx = c->flags->stream_ctx; - - /* Credentials */ + /* Credentials and context */ redis_sock_set_auth(sock, c->flags->user, c->flags->pass); + redis_sock_set_context(sock, c->flags->context); // Index this seed by host/port key_len = snprintf(key, sizeof(key), "%s:%u", ZSTR_VAL(sock->host), @@ -1589,6 +1588,7 @@ PHP_REDIS_API short cluster_send_command(redisCluster *c, short slot, const char * node until we find one that is available. */ if (cluster_sock_write(c, cmd, cmd_len, 0) == -1) { /* We have to abort, as no nodes are reachable */ + cluster_cache_clear(c); CLUSTER_THROW_EXCEPTION("Can't communicate with any node in the cluster", 0); return -1; } @@ -2010,12 +2010,12 @@ PHP_REDIS_API void cluster_sub_resp(INTERNAL_FUNCTION_PARAMETERS, redisCluster * if ((z_tmp = zend_hash_index_find(Z_ARRVAL(z_tab), 0)) == NULL || strcasecmp(Z_STRVAL_P(z_tmp), sctx->kw) != 0 ) { - zval_dtor(&z_tab); + zval_ptr_dtor_nogc(&z_tab); efree(sctx); RETURN_FALSE; } - zval_dtor(&z_tab); + zval_ptr_dtor_nogc(&z_tab); pull = 1; } @@ -2046,7 +2046,7 @@ PHP_REDIS_API void cluster_sub_resp(INTERNAL_FUNCTION_PARAMETERS, redisCluster * { is_pmsg = *Z_STRVAL_P(z_type) == 'p'; } else { - zval_dtor(&z_tab); + zval_ptr_dtor_nogc(&z_tab); continue; } @@ -2085,14 +2085,14 @@ PHP_REDIS_API void cluster_sub_resp(INTERNAL_FUNCTION_PARAMETERS, redisCluster * // If we have a return value, free it zval_ptr_dtor(&z_ret); - zval_dtor(&z_tab); + zval_ptr_dtor_nogc(&z_tab); } // We're no longer subscribing, due to an error c->subscribed_slot = -1; // Cleanup - zval_dtor(&z_tab); + zval_ptr_dtor_nogc(&z_tab); efree(sctx); // Failure @@ -2116,8 +2116,8 @@ PHP_REDIS_API void cluster_unsub_resp(INTERNAL_FUNCTION_PARAMETERS, if (!cluster_zval_mbulk_resp(INTERNAL_FUNCTION_PARAM_PASSTHRU, c, pull, mbulk_resp_loop_raw, &z_tab) || (z_chan = zend_hash_index_find(Z_ARRVAL(z_tab), 1)) == NULL ) { - zval_dtor(&z_tab); - zval_dtor(return_value); + zval_ptr_dtor_nogc(&z_tab); + zval_ptr_dtor_nogc(return_value); RETURN_FALSE; } @@ -2125,8 +2125,8 @@ PHP_REDIS_API void cluster_unsub_resp(INTERNAL_FUNCTION_PARAMETERS, if ((z_flag = zend_hash_index_find(Z_ARRVAL(z_tab), 2)) == NULL || Z_STRLEN_P(z_flag) != 2 ) { - zval_dtor(&z_tab); - zval_dtor(return_value); + zval_ptr_dtor_nogc(&z_tab); + zval_ptr_dtor_nogc(return_value); RETURN_FALSE; } @@ -2136,7 +2136,7 @@ PHP_REDIS_API void cluster_unsub_resp(INTERNAL_FUNCTION_PARAMETERS, // Add result add_assoc_bool(return_value, Z_STRVAL_P(z_chan), flag[1] == '1'); - zval_dtor(&z_tab); + zval_ptr_dtor_nogc(&z_tab); pull = 1; } } @@ -2331,7 +2331,7 @@ cluster_gen_mbulk_resp(INTERNAL_FUNCTION_PARAMETERS, redisCluster *c, /* Call our specified callback */ if (cb(c->cmd_sock, &z_result, c->reply_len, ctx) == FAILURE) { - zval_dtor(&z_result); + zval_ptr_dtor_nogc(&z_result); CLUSTER_RETURN_FALSE(c); } } @@ -2461,7 +2461,7 @@ cluster_xrange_resp(INTERNAL_FUNCTION_PARAMETERS, redisCluster *c, void *ctx) { c->cmd_sock->compression = c->flags->compression; if (redis_read_stream_messages(c->cmd_sock, c->reply_len, &z_messages) < 0) { - zval_dtor(&z_messages); + zval_ptr_dtor_nogc(&z_messages); CLUSTER_RETURN_FALSE(c); } @@ -2485,7 +2485,7 @@ cluster_xread_resp(INTERNAL_FUNCTION_PARAMETERS, redisCluster *c, void *ctx) { } else { array_init(&z_streams); if (redis_read_stream_messages_multi(c->cmd_sock, c->reply_len, &z_streams) < 0) { - zval_dtor(&z_streams); + zval_ptr_dtor_nogc(&z_streams); CLUSTER_RETURN_FALSE(c); } } @@ -2507,7 +2507,7 @@ cluster_xclaim_resp(INTERNAL_FUNCTION_PARAMETERS, redisCluster *c, void *ctx) { ZEND_ASSERT(ctx == NULL || ctx == PHPREDIS_CTX_PTR); if (redis_read_xclaim_reply(c->cmd_sock, c->reply_len, ctx == PHPREDIS_CTX_PTR, &z_msg) < 0) { - zval_dtor(&z_msg); + zval_ptr_dtor_nogc(&z_msg); CLUSTER_RETURN_FALSE(c); } @@ -2542,7 +2542,7 @@ cluster_vemb_resp(INTERNAL_FUNCTION_PARAMETERS, redisCluster *c, void *ctx) { return; fail: - zval_dtor(&z_ret); + zval_ptr_dtor_nogc(&z_ret); CLUSTER_RETURN_FALSE(c); } @@ -2556,7 +2556,7 @@ cluster_vinfo_resp(INTERNAL_FUNCTION_PARAMETERS, redisCluster *c, void *ctx) { array_init_size(&z_ret, c->reply_len / 2); if (redis_read_vinfo_response(c->cmd_sock, &z_ret, c->reply_len) != SUCCESS) { - zval_dtor(&z_ret); + zval_ptr_dtor_nogc(&z_ret); CLUSTER_RETURN_FALSE(c); } @@ -2623,7 +2623,7 @@ cluster_xinfo_resp(INTERNAL_FUNCTION_PARAMETERS, redisCluster *c, void *ctx) array_init(&z_ret); if (redis_read_xinfo_response(c->cmd_sock, &z_ret, c->reply_len) != SUCCESS) { - zval_dtor(&z_ret); + zval_ptr_dtor_nogc(&z_ret); CLUSTER_RETURN_FALSE(c); } @@ -2658,7 +2658,7 @@ cluster_acl_custom_resp(INTERNAL_FUNCTION_PARAMETERS, redisCluster *c, void *ctx array_init(&z_ret); if (cb(c->cmd_sock, &z_ret, c->reply_len) != SUCCESS) { - zval_dtor(&z_ret); + zval_ptr_dtor_nogc(&z_ret); CLUSTER_RETURN_FALSE(c); } @@ -2701,7 +2701,7 @@ PHP_REDIS_API zval *cluster_zval_mbulk_resp(INTERNAL_FUNCTION_PARAMETERS, // Call our callback if (cb(c->cmd_sock, z_ret, c->reply_len, NULL) == FAILURE) { - zval_dtor(z_ret); + zval_ptr_dtor_nogc(z_ret); return NULL; } @@ -2727,7 +2727,7 @@ PHP_REDIS_API void cluster_multi_mbulk_resp(INTERNAL_FUNCTION_PARAMETERS, c->cmd_sock = SLOT_SOCK(c, fi->slot); if (cluster_check_response(c, &c->reply_type) < 0) { - zval_dtor(multi_resp); + zval_ptr_dtor_nogc(multi_resp); RETURN_FALSE; } @@ -2742,7 +2742,7 @@ PHP_REDIS_API void cluster_multi_mbulk_resp(INTERNAL_FUNCTION_PARAMETERS, } // Set our return array - zval_dtor(return_value); + zval_ptr_dtor_nogc(return_value); RETVAL_ZVAL(multi_resp, 0, 1); } @@ -2858,7 +2858,7 @@ PHP_REDIS_API void cluster_mset_resp(INTERNAL_FUNCTION_PARAMETERS, redisCluster if (c->reply_type != TYPE_LINE) { php_error_docref(0, E_ERROR, "Invalid reply type returned for MSET command"); - zval_dtor(mctx->z_multi); + zval_ptr_dtor_nogc(mctx->z_multi); efree(mctx->z_multi); efree(mctx); RETURN_FALSE; @@ -2867,9 +2867,9 @@ PHP_REDIS_API void cluster_mset_resp(INTERNAL_FUNCTION_PARAMETERS, redisCluster // Set our return if it's the last call if (mctx->last) { if (CLUSTER_IS_ATOMIC(c)) { - ZVAL_BOOL(return_value, zval_is_true(mctx->z_multi)); + ZVAL_BOOL(return_value, zend_is_true(mctx->z_multi)); } else { - add_next_index_bool(&c->multi_resp, zval_is_true(mctx->z_multi)); + add_next_index_bool(&c->multi_resp, zend_is_true(mctx->z_multi)); } efree(mctx->z_multi); } @@ -3061,7 +3061,7 @@ static int mbulk_resp_loop_zipdbl(RedisSock *redis_sock, zval *z_result, zend_string *tmp, *zstr = zval_get_tmp_string(z, &tmp); add_assoc_double_ex(z_result, ZSTR_VAL(zstr), ZSTR_LEN(zstr), atof(line)); zend_tmp_string_release(tmp); - zval_dtor(z); + zval_ptr_dtor_nogc(z); /* Free our key and line */ efree(key); @@ -3260,7 +3260,7 @@ PHP_REDIS_API redisCachedCluster *cluster_cache_load(zend_string *hash) { PHP_REDIS_API void cluster_cache_store(zend_string *hash, HashTable *nodes) { redisCachedCluster *cc = cluster_cache_create(hash, nodes); - redis_register_persistent_resource(cc->hash, cc, le_cluster_slot_cache); + zend_register_persistent_resource_ex(cc->hash, cc, le_cluster_slot_cache); } void cluster_cache_clear(redisCluster *c) diff --git a/common.h b/common.h index 8936c8e080..27e79d600d 100644 --- a/common.h +++ b/common.h @@ -46,9 +46,11 @@ #endif #ifndef REDIS_MSVC -# if defined(__has_c_attribute) -# if __has_c_attribute(nodiscard) -# define REDIS_NODISCARD [[nodiscard]] +# ifndef REDIS_NODISCARD +# if defined(__has_c_attribute) +# if __has_c_attribute(nodiscard) +# define REDIS_NODISCARD [[nodiscard]] +# endif # endif # endif # ifndef REDIS_NODISCARD @@ -58,11 +60,11 @@ # endif # elif defined(__GNUC__) || defined(__clang__) # define REDIS_NODISCARD __attribute__((warn_unused_result)) -# else -# define REDIS_NODISCARD # endif # endif -#else +#endif /* ifndef REDIS_MSVC */ + +#ifndef REDIS_NODISCARD # define REDIS_NODISCARD #endif @@ -221,11 +223,15 @@ typedef enum { #define REDIS_STRICMP_STATIC(s, len, sstr) \ (len == sizeof(sstr) - 1 && !strncasecmp(s, sstr, len)) -/* On some versions of glibc strncmp is a macro. This wrapper allows us to - use it in combination with ZEND_STRL in those cases. */ +/* On some versions of glibc strncmp and strncasecmp can be a macro a macro. + * This wrapper allows us to use it in combination with ZEND_STRL in those + * cases. */ static inline int redis_strncmp(const char *s1, const char *s2, size_t n) { return strncmp(s1, s2, n); } +static inline int redis_strncasecmp(const char *s1, const char *s2, size_t n) { + return strncasecmp(s1, s2, n); +} /* Test if a zval is a string and (case insensitive) matches a static string */ #define ZVAL_STRICMP_STATIC(zv, sstr) \ @@ -259,11 +265,11 @@ typedef struct RedisHello { /* {{{ struct RedisSock */ typedef struct { php_stream *stream; - php_stream_context *stream_ctx; zend_string *host; int port; zend_string *user; zend_string *pass; + HashTable *context; double timeout; double read_timeout; long retry_interval; diff --git a/docs/DOCTUM_VERSION b/docs/DOCTUM_VERSION index d41f08f1f3..0413736d69 100644 --- a/docs/DOCTUM_VERSION +++ b/docs/DOCTUM_VERSION @@ -1 +1 @@ -5.5.1 \ No newline at end of file +5.5.4 \ No newline at end of file diff --git a/docs/PROJECT_VERSION b/docs/PROJECT_VERSION index 6563189c54..ce57f64563 100644 --- a/docs/PROJECT_VERSION +++ b/docs/PROJECT_VERSION @@ -1 +1 @@ -develop +develop \ No newline at end of file diff --git a/docs/Redis.html b/docs/Redis.html index dd7ee25ffc..009c813d0a 100644 --- a/docs/Redis.html +++ b/docs/Redis.html @@ -5,7 +5,7 @@
class - Redis (View source) + Redis (View source)
@@ -92,6 +481,511 @@| + REDIS_NOT_FOUND + | +
+ Returned by |
+
| + REDIS_STRING + | +
+ Returned by |
+
| + REDIS_SET + | +
+ Returned by |
+
| + REDIS_LIST + | +
+ Returned by |
+
| + REDIS_ZSET + | +
+ Returned by |
+
| + REDIS_HASH + | +
+ Returned by |
+
| + REDIS_STREAM + | +
+ Returned by |
+
| + REDIS_VECTORSET + | +
+ Returned by |
+
| + ATOMIC + | +
+ Returned from |
+
| + MULTI + | +
+ Returned from |
+
| + PIPELINE + | +
+ Returned from |
+
| + OPT_SERIALIZER + | +
+ Used with |
+
| + OPT_PREFIX + | +
+ Used to set an automatic prefix for keys used in commands. |
+
| + OPT_READ_TIMEOUT + | +
+ Used to set the read timeout for the connection. |
+
| + OPT_TCP_KEEPALIVE + | +
+ Used to enable or disable TCP keepalive on the connection. |
+
| + OPT_COMPRESSION + | +
+ Used to set the compression algorithm to use for compressing |
+
| + OPT_REPLY_LITERAL + | +
+ Causes PhpRedis to return the actual string in |
+
| + OPT_COMPRESSION_LEVEL + | +
+ Used to specify the compression level to use when compressing data. |
+
| + OPT_NULL_MULTIBULK_AS_NULL + | +
+ Tells PhpRedis to return a NULL multi-bulk ( |
+
| + OPT_PACK_IGNORE_NUMBERS + | +
+ When enabled, this option tells PhpRedis to ignore purely numeric values
+when packing and unpacking data. This does not include numeric strings. If you want numeric strings to be ignored, typecast them to an int or +float. +The primary purpose of this option is to make it more ergonomic when +setting keys that will later be incremented or decremented. +Note: This option incurs a small performance penalty when reading data +because we have to see if the data is a string representation of an int +or float. + |
+
| + SERIALIZER_NONE + | +
+ Sets the serializer to none (no serialization). |
+
| + SERIALIZER_PHP + | +
+ Sets the serializer to PHP's built-in |
+
| + SERIALIZER_IGBINARY + | +
+ Sets the serializer to igbinary. Note that phpredis must be compiled
+with ighbinary support to use this serializer. |
+
| + SERIALIZER_MSGPACK + | +
+ Sets the serializer to msgpack. Note that phpredis must be compiled
+with msgpack support to use this serializer. |
+
| + SERIALIZER_JSON + | +
+ Sets the serializer to JSON. |
+
| + COMPRESSION_NONE + | +
+ Disables compression. |
+
| + COMPRESSION_LZF + | +
+ Sets the compression algorithm to LZF. PhpRedis must be compiled with
+lzf support but this serializer is bundled with the extension. |
+
| + COMPRESSION_ZSTD + | +
+ Sets the compression algorithm to ZSTD. PhpRedis must be compiled with
+zstd support to use this serializer. This is often the best balance
+between speed and compression ratio. |
+
| + COMPRESSION_ZSTD_DEFAULT + | +
+ This constant represents the "default" compression level for ZSTD. If
+PhpRedis is compiled against a new enough ZSTD the value comes from the
+library, otherwise we just set it to 3. |
+
| + COMPRESSION_ZSTD_MIN + | +
+ The minimum compression level ZSTD supports, which comes from the
+underlying ZSTD library if new enough. Otherwise we just set it to 1. |
+
| + COMPRESSION_ZSTD_MAX + | +
+ The maximum compression level ZSTD supports, which comes from the
+underlying ZSTD library. |
+
| + COMPRESSION_LZ4 + | +
+ Set the compression algorithm to LZ4. PhpRedis must be compiled with
+lz4 support to use this serializer. This algorithm is generally
+the fastest but has a lower compression ratio than ZSTD. |
+
| + OPT_SCAN + | +
+ Used with |
+
| + SCAN_RETRY + | +
+ When enabled, this option causes PhpRedis to automatically retry |
+
| + SCAN_NORETRY + | +
+ Then enabled, this option tells PhpRedis to not retry |
+
| + SCAN_PREFIX + | +
+ Tells PhpRedis to prefix keys returned from |
+
| + SCAN_NOPREFIX + | +
+ Tells PhpRedis to NOT prefix keys returned from |
+
| + BEFORE + | +
+ This is just the string "before" which is used with various list
+commands to indicate an insertion point. |
+
| + AFTER + | +
+ This is just the string "after" which is used with various list commands
+to indicate an insertion point. |
+
| + LEFT + | +
+ This is just the string "left" which is used with various list commands
+such as |
+
| + RIGHT + | +
+ This is just the string "right" which is used with various list commands
+such as |
+
| + OPT_MAX_RETRIES + | +
+ How many times should |
+
| + OPT_BACKOFF_ALGORITHM + | +
+ Used to specify the backoff algorithm to use when reconnecting. |
+
| + BACKOFF_ALGORITHM_DEFAULT + | +
+ Default backoff - random delay before the first retry, then constant |
+
| + BACKOFF_ALGORITHM_CONSTANT + | +
+ Constant backoff - always sleep for exactly |
+
| + BACKOFF_ALGORITHM_UNIFORM + | +
+ Uniform backoff - randomly sleep between 0 and |
+
| + BACKOFF_ALGORITHM_EXPONENTIAL + | +
+ Exponential backoff - doubles the delay every retry (up to 2^10) before |
+
| + BACKOFF_ALGORITHM_FULL_JITTER + | +
+ Full jitter - exponential delay but pick a random value between 0 and that delay. |
+
| + BACKOFF_ALGORITHM_EQUAL_JITTER + | +
+ Equal jitter - half the exponential delay plus a random amount up to the other half. |
+
| + BACKOFF_ALGORITHM_DECORRELATED_JITTER + | +
+ Decorrelated jitter - pick a random delay between |
+
| + OPT_BACKOFF_BASE + | +
+ Backoff base - minimum delay in milliseconds that algorithms start from. |
+
| + OPT_BACKOFF_CAP + | +
+ Backoff cap - maximum delay in milliseconds that any algorithm can reach. |
+
Create a new Redis instance. If passed sufficient information in the options array it is also possible to connect to an instance at the same @@ -116,8 +1010,7 @@
No description
-Destructor to clean up the Redis object.
Compress a value with the currently configured compressor as set with -Redis::setOption().
Compress a value with the currently configured compressor (Redis::OPT_COMPRESSION) +exactly the same way PhpRedis does before sending data to Redis.
Uncompress the provided argument that has been compressed with the -currently configured compressor as set with Redis::setOption().
Uncompress the provided argument using the compressor configured via +Redis::setOption() (Redis::OPT_COMPRESSION).
Pack the provided value with the configured serializer and compressor -as set with Redis::setOption().
Pack the provided value by first serializing it (if Redis::OPT_SERIALIZER is set) +and then compressing the serialized payload (if Redis::OPT_COMPRESSION is set), +mirroring exactly what PhpRedis transmits to Redis.
Compute the XXH3 digest of a PHP value after it has been _packed, producing
+the same digest Redis' DIGEST command would return for the stored value.
Unpack the provided value with the configured compressor and serializer -as set with Redis::setOption().
Unpack the provided value by first uncompressing it (if Redis::OPT_COMPRESSION +is set) and then unserializing it (if Redis::OPT_SERIALIZER is set) to recover +the original PHP value.
No description
-Execute Redis ACL subcommands.
Asynchronously rewrite Redis' append-only file
No description
+No description
-Execute Redis CLIENT subcommands.
No description
-Execute Redis COMMAND subcommands.
Execute the Redis CONFIG command in a variety of ways.
No description
-Connect to a Redis server
Make a copy of a key.
No description
-Execute the Redis DEBUG command. Note that this is disabled by default
+and can be very dangerous, even allowing you to crash the server. Use
+with caution
Delete a key conditionally based on its value or hash digest
Delete a key if it's equal to the specified value. This command is +specific to Valkey >= 9.0
Discard a transaction currently in progress.
Dump Redis' internal binary representation of a key.
Sets an expiration in seconds on the key in question. If connected to redis-server >= 7.0.0 you may send an additional "mode" argument which @@ -621,7 +1555,7 @@
Set a key to expire at an exact unix timestamp.
Get the expiration timestamp of a given Redis key but in milliseconds.
+ +Invoke a function.
This is a read-only variant of the FCALL command that cannot execute commands that modify data.
Deletes all the keys of the currently selected database.
Functions is an API for managing code to be executed on the server.
Retrieve a string keys value.
Retrieve a value and metadata of key.
Get the port we are connected to. This number will be zero if we are connected to a unix socket.
Get the server name as reported by the HELLO response.
Get the server version as reported by the HELLO response.
Get the longest common subsequence between two string keys.
No description
-Get the number of bytes sent and received on the socket.
Reset the number of bytes sent and received on the socket.
Read every field and value from a hash.
Retrieve a value and metadata of hash field.
Get one or more fields from a hash.
Get one or more fields of a hash while optionally setting expiration +information
Set one or more fields in a hash with optional expiration information.
Get one or more fields and delete them
Get one or more random field from a hash.
No description
-Add or update one or more hash fields and values.
Set a hash field and value, but only if that field does not exist
Get all of the values from a hash.
+ +Set the expiration on one or more fields in a hash.
Set the expiration on one or more fields in a hash in milliseconds.
Set the expiration time on one or more fields of a hash.
Set the expiration time on one or more fields of a hash in milliseconds.
Get the TTL of one or more fields in a hash
Get the millisecond TTL of one or more fields in a hash
Get the expiration time of one or more fields in a hash
Get the expiration time in milliseconds of one or more fields in a hash
Persist one or more hash fields
Set an expiration on a key member (KeyDB only).
Set an expiration on a key membert to a specific unix timestamp (KeyDB only).
No description
+Retrieve the index of an element in a list.
Get one ore more string keys.
Get one or more string keys.
No description
-Proxy for the Redis MIGRATE command.
Set one ore more string keys.
Set one or more string keys.
Set one or more keys and values with optional expiry information.
Set one ore more string keys but only if none of the key exist.
Set one or more string keys but only if none of the key exist.
No description
-Get encoding and other information about a key.
No description
No description
-Connects to a Redis server creating or reusing a persistent connection.
Sets an expiration in milliseconds on a given key. If connected to Redis >= 7.0.0 you can pass an optional mode argument that modifies how the command will execute.
Set a key's expiration to a specific Unix Timestamp in milliseconds. If connected to Redis >= 7.0.0 you can pass an optional 'mode' argument.
Retrieve the cardinality of a Redis HyperLogLog key.
PING the redis server with an optional string argument.
No description
No description
-Interact with the Redis PubSub subsystem.
Restore a key by the binary payload generated by the DUMP command.
Add one ore more values to a Redis SET key. This is an alternative to Redis::sadd() but +
Add one or more values to a Redis SET key. This is an alternative to Redis::sadd() but instead of being variadic, takes a single array of values.
Incrementally scan the Redis keyspace, with optional pattern and type matching.
Create or set a Redis STRING key to a value.
Turn a redis instance into a replica of another or promote a replica to a primary.
Used to turn a Redis instance into a replica of another, or to remove replica status promoting the instance to a primary.
Scan the members of a redis SET key.
Subscribes the client to the specified shard channels.
Subscribe to one or more Redis pubsub channels.
Unsubscribes the client from the given shard channels, +or from all of them if none is given.
Acknowledge one ore more messages that are pending (have been consumed using XREADGROUP but +
Acknowledge one or more messages that are pending (have been consumed using XREADGROUP but not yet acknowledged by XACK.)
XGROUP
Remove one or more IDs from a stream with extended options.
XGROUP
Retrieve information about a stream key.
Get a range of entries from a STREAM key in reverse chronological order.
Add to a vector set
Query similarity of a vector by element or scores
Get the length of a vector set
Get the dimensions of a vector set
Get various bits of information about a vector set
Check if an element is a member of a vectorset
Get the embeddings for a specific member
Get one or more random members from a vector set
Retreive a lexographical range of elements from a vector set
Remove an element from a vector set
Set the attributes of a vector set element
Get the attributes of a vector set element
Get any adajcent values for a member of a vector set.
Get rate limiting information
Count the number of members in a sorted set with scores inside a provided range.
Pop one or more of the highest scoring elements from a sorted set.
Pop one or more of the lowest scoring elements from a sorted set.
Retrieve a range of elements of a sorted set between a start and end point.
This command is similar to ZRANGE except that instead of returning the values directly it will store them in a destination key provided by the user
Retrieve one or more random members from a Redis sorted set.
Given one or more sorted set key names, return every element that is in the first set but not any of the others.
Compute the intersection of one ore more sorted sets storing the result in a new sorted set.
Compute the intersection of one or more sorted sets storing the result in a new sorted set.
Scan the members of a sorted set incrementally, using a cursor
Perform a union on one or more Redis sets and store the result in a destination sorted set.
Ask the server for the XXH3 digest of a given key's value
Redis
- __construct(array $options = null)
-
+
+ Redis __construct(array|null $options = null)
Create a new Redis instance. If passed sufficient information in the options array it is also possible to connect to an instance at the same time.
NOTE: Below is an example options array with various setting
-$options = [
- 'host' => 'localhost',
- 'port' => 6379,
- 'readTimeout' => 2.5,
- 'connectTimeout' => 2.5,
- 'persistent' => true,
-
- // Valid formats: NULL, ['user', 'pass'], 'pass', or ['pass']
- 'auth' => ['phpredis', 'phpredis'],
-
- // See PHP stream options for valid SSL configuration settings.
- 'ssl' => ['verify_peer' => false],
-
- // How quickly to retry a connection after we time out or it closes.
- // Note that this setting is overridden by 'backoff' strategies.
- 'retryInterval' => 100,
-
- // Which backoff algorithm to use. 'decorrelated jitter' is
- // likely the best one for most solution, but there are many
- // to choose from:
- // REDIS_BACKOFF_ALGORITHM_DEFAULT
- // REDIS_BACKOFF_ALGORITHM_CONSTANT
- // REDIS_BACKOFF_ALGORITHM_UNIFORM
- // REDIS_BACKOFF_ALGORITHM_EXPONENTIAL
- // REDIS_BACKOFF_ALGORITHM_FULL_JITTER
- // REDIS_BACKOFF_ALGORITHM_EQUAL_JITTER
- // REDIS_BACKOFF_ALGORITHM_DECORRELATED_JITTER
- // 'base', and 'cap' are in milliseconds and represent the first
- // delay redis will use when reconnecting, and the maximum delay
- // we will reach while retrying.
- 'backoff' => [
- 'algorithm' => Redis::BACKOFF_ALGORITHM_DECORRELATED_JITTER,
- 'base' => 500,
- 'cap' => 750,
- ]
+$options = [
+ 'host' => 'localhost',
+ 'port' => 6379,
+ 'readTimeout' => 2.5,
+ 'connectTimeout' => 2.5,
+ 'persistent' => true,
+
+ // Valid formats: NULL, ['user', 'pass'], 'pass', or ['pass']
+ 'auth' => ['phpredis', 'phpredis'],
+
+ // See PHP stream options for valid SSL configuration settings.
+ 'ssl' => ['verify_peer' => false],
+
+ // How quickly to retry a connection after we time out or it closes.
+ // Note that this setting is overridden by 'backoff' strategies.
+ 'retryInterval' => 100,
+
+ // Which backoff algorithm to use. 'decorrelated jitter' is
+ // likely the best one for most solution, but there are many
+ // to choose from:
+ // REDIS_BACKOFF_ALGORITHM_DEFAULT
+ // REDIS_BACKOFF_ALGORITHM_CONSTANT
+ // REDIS_BACKOFF_ALGORITHM_UNIFORM
+ // REDIS_BACKOFF_ALGORITHM_EXPONENTIAL
+ // REDIS_BACKOFF_ALGORITHM_FULL_JITTER
+ // REDIS_BACKOFF_ALGORITHM_EQUAL_JITTER
+ // REDIS_BACKOFF_ALGORITHM_DECORRELATED_JITTER
+ // 'base', and 'cap' are in milliseconds and represent the first
+ // delay redis will use when reconnecting, and the maximum delay
+ // we will reach while retrying.
+ 'backoff' => [
+ 'algorithm' => Redis::BACKOFF_ALGORITHM_DECORRELATED_JITTER,
+ 'base' => 500,
+ 'cap' => 750,
+ ]
];
Note: If you do wish to connect via the constructor, only 'host' is
strictly required, which will cause PhpRedis to connect to that
@@ -2706,22 +4056,24 @@
Parameters
- array
+ array|null
$options
- Return Value
+ Return Value
-
+
+
Redis
+
See also
@@ -2743,24 +4095,31 @@ See also
+ Examples
+
+
+
+ $redis = new Redis(['host' => '127.0.0.1', 'port' => 6380]);
+
+
+
- __destruct()
-
+
+ __destruct()
No description
- +Destructor to clean up the Redis object.
This method will disconnect from Redis. If the connection is persistento +it will be stashed for future reuse.
string
- _compress(string $value)
-
+
+ string _compress(string $value)
Compress a value with the currently configured compressor as set with -Redis::setOption().
+Compress a value with the currently configured compressor (Redis::OPT_COMPRESSION) +exactly the same way PhpRedis does before sending data to Redis.
string
- _uncompress(string $value)
-
+
+ string _uncompress(string $value)
Uncompress the provided argument that has been compressed with the -currently configured compressor as set with Redis::setOption().
+Uncompress the provided argument using the compressor configured via +Redis::setOption() (Redis::OPT_COMPRESSION).
string
- _prefix(string $key)
-
+
+ string _prefix(string $key)
| string | The prefixed string |
$redis->_prefix('user:42'); |
+
string
- _serialize(mixed $value)
-
+
+ string _serialize(mixed $value)
| string | The serialized result |
$redis->_serialize(['answer' => 42]); |
+
mixed
- _unserialize(string $value)
-
+
+ mixed _unserialize(string $value)
| mixed | The unserialized result |
$redis->_unserialize($redis->_serialize(['answer' => 42])); |
+
string
- _pack(mixed $value)
-
+
+ string _pack(mixed $value)
Pack the provided value with the configured serializer and compressor -as set with Redis::setOption().
+Pack the provided value by first serializing it (if Redis::OPT_SERIALIZER is set) +and then compressing the serialized payload (if Redis::OPT_COMPRESSION is set), +mirroring exactly what PhpRedis transmits to Redis.