Compare commits

..

4 Commits

Author SHA1 Message Date
Till Krüss 07105e0506 tag v2.4.1 2025-11-12 10:00:11 -08:00
Vladyslav Vildanov 07dc6ba6d2 fix(conn): Removed automatic conn_uid assignment (#1551)
* fix(conn): Removed automatic conn_uid assignment

* Restrict redis version

* Updated CHANGELOG.md

* Added test case

* Update CHANGELOG.md

---------

Co-authored-by: Till Krüss <tillkruss@users.noreply.github.com>
2025-06-05 09:54:27 +03:00
Till Krüss 5142011581 Update CHANGELOG.md 2025-05-27 09:55:55 -07:00
Tim Düsterhus c4b9c34d86 Fix return type for ZCOUNT (#1546)
* Fix return type for ZCOUNT

ZCOUNT returns an `int`:

> Integer reply: the number of members in the specified score range.

* Update CHANGELOG.md

---------

Co-authored-by: Till Krüss <tillkruss@users.noreply.github.com>
2025-05-27 10:39:05 +03:00
7 changed files with 84 additions and 7 deletions
+5
View File
@@ -1,5 +1,10 @@
## Changelog
## v2.4.1 (2025-11-12)
### Fixed
- Fixed return type for `ZCOUNT` to be `int` (#1546)
- Removed automatic `conn_uid` parameter assignment (#1551)
## v2.4.0 (2025-04-30)
### Added
- Added new hash-field expiration commands (#1520)
+44
View File
@@ -138,6 +138,50 @@ it is still desired to have control of when the connection is opened or closed:
achieved by invoking `$client->connect()` and `$client->disconnect()`. Please note that the effect
of these methods on aggregate connections may differ depending on each specific implementation.
#### Persistent connections ####
To increase a performance of your application you may set up a client to use persistent TCP connection, this way
client saves a time on socket creation and connection handshake. By default, connection is created on first-command
execution and will be automatically closed by GC before the process is being killed.
However, if your application is backed by PHP-FPM the processes are idle, and you may set up it to be persistent and
reusable across multiple script execution within the same process.
To enable the persistent connection mode you should provide following configuration:
```php
// Standalone
$client = new Predis\Client(['persistent' => true]);
// Cluster
$client = new Predis\Client(
['tcp://host:port', 'tcp://host:port', 'tcp://host:port'],
['cluster' => 'redis', 'parameters' => ['persistent' => true]]
);
```
**Important**
If you operate on multiple clients within the same application, and they communicate with the same resource, by default
they will share the same socket (that's the default behaviour of persistent sockets). So in this case you would need
to additionally provide a `conn_uid` identifier for each client, this way each client will create its own socket so
the connection context won't be shared across clients. This socket behaviour explained
[here](https://www.php.net/manual/en/function.stream-socket-client.php#105393)
```php
// Standalone
$client1 = new Predis\Client(['persistent' => true, 'conn_uid' => 'id_1']);
$client2 = new Predis\Client(['persistent' => true, 'conn_uid' => 'id_2']);
// Cluster
$client1 = new Predis\Client(
['tcp://host:port', 'tcp://host:port', 'tcp://host:port'],
['cluster' => 'redis', 'parameters' => ['persistent' => true, 'conn_uid' => 'id_1']]
);
$client2 = new Predis\Client(
['tcp://host:port', 'tcp://host:port', 'tcp://host:port'],
['cluster' => 'redis', 'parameters' => ['persistent' => true, 'conn_uid' => 'id_2']]
);
```
### Client configuration ###
+1 -1
View File
@@ -53,7 +53,7 @@ use Traversable;
*/
class Client implements ClientInterface, IteratorAggregate
{
public const VERSION = '2.4.0';
public const VERSION = '2.4.1';
/** @var OptionsInterface */
private $options;
+1 -1
View File
@@ -300,7 +300,7 @@ use Predis\Response\Status;
* @method array|null xread(int $count = null, int $block = null, array $streams = null, string ...$id)
* @method int zadd(string $key, array $membersAndScoresDictionary)
* @method int zcard(string $key)
* @method string zcount(string $key, int|string $min, int|string $max)
* @method int zcount(string $key, int|string $min, int|string $max)
* @method array zdiff(array $keys, bool $withScores = false)
* @method int zdiffstore(string $destination, array $keys)
* @method string zincrby(string $key, int $increment, string $member)
-1
View File
@@ -41,7 +41,6 @@ class StreamConnection extends AbstractConnection
public function __construct(ParametersInterface $parameters)
{
parent::__construct($parameters);
$this->parameters->conn_uid = spl_object_hash($this);
}
/**
+22 -4
View File
@@ -1285,17 +1285,35 @@ class ClientTest extends PredisTestCase
/**
* @group connected
* @requiresRedisVersion >= 5.0.0
*/
public function testClientsCreateDifferentPersistentConnections(): void
{
$client1 = new Client($this->getParameters(['database' => 14, 'persistent' => true]));
$client2 = new Client($this->getParameters(['database' => 15, 'persistent' => true]));
$client1 = new Client($this->getParameters(['database' => 14, 'persistent' => true, 'conn_uid' => 1]));
$client2 = new Client($this->getParameters(['database' => 15, 'persistent' => true, 'conn_uid' => 2]));
$client1->set('foo', 'bar');
$client2->set('foo', 'baz');
$this->assertSame('bar', $client1->get('foo'));
$this->assertSame('baz', $client2->get('foo'));
$this->assertNotSame($client1->client('ID'), $client2->client('ID'));
}
/**
* @group connected
* @requiresRedisVersion >= 5.0.0
*/
public function testClientsCreateSamePersistentConnections(): void
{
$client1 = new Client($this->getParameters(['persistent' => true]));
$client2 = new Client($this->getParameters(['persistent' => true]));
$client1->set('foo', 'bar');
$client2->set('foo', 'baz');
$this->assertSame('baz', $client2->get('foo'));
$this->assertSame($client1->client('ID'), $client2->client('ID'));
}
/**
@@ -1307,11 +1325,11 @@ class ClientTest extends PredisTestCase
{
$client1 = new Client(
$this->getDefaultParametersArray(),
['cluster' => 'redis', 'parameters' => ['persistent' => true]]
['cluster' => 'redis', 'parameters' => ['persistent' => true, 'conn_uid' => 1]]
);
$client2 = new Client(
$this->getDefaultParametersArray(),
['cluster' => 'redis', 'parameters' => ['persistent' => true]]
['cluster' => 'redis', 'parameters' => ['persistent' => true, 'conn_uid' => 2]]
);
$client1->set('{shard1}foo', 'bar');
@@ -403,6 +403,17 @@ class ParametersTest extends PredisTestCase
$this->assertSame($expected, Parameters::parse($uri));
}
/**
* @group disconnected
*/
public function testSetParameters(): void
{
$parameters = new Parameters();
$parameters->property = 'value';
$this->assertEquals('value', $parameters->property);
}
// ******************************************************************** //
// ---- HELPER METHODS ------------------------------------------------ //
// ******************************************************************** //