Sitelet https://github.com/phpredis/phpredis/pull/2790/files
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ $redis = new Redis([
*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
*client_name*: string, sets the client name for the connection (see [CLIENT SETNAME](#client))
*ssl*: array, SSL context options

#### Class RedisException
Expand Down Expand Up @@ -360,6 +361,13 @@ $redis->pconnect('tls://127.0.0.1'); // enable transport level security, port 63
$redis->pconnect('127.0.0.1', 6379, 2.5); // 2.5 sec timeout and would be another connection than the two before.
$redis->pconnect('127.0.0.1', 6379, 2.5, 'x'); // x is sent as persistent_id and would be another connection than the three before.
$redis->pconnect('/tmp/redis.sock'); // unix domain socket - would be another connection than the four before.

/* Set client name for persistent connections */
$redis->pconnect('127.0.0.1', 6379, 0, 'my-persistent-id', 0, 0, [
'client_name' => 'my-app-worker-1'
]);
// The client name will be set automatically when the connection is established
// and will persist across requests for this persistent connection
~~~

**Note:** `popen` is an alias for `pconnect` and will be removed in future versions of phpredis.
Expand Down Expand Up @@ -4502,6 +4510,24 @@ $redis->client('setname', 'somename'); // Set the name of the current connection
$redis->client('kill', <ip:port>); // Kill the process at ip:port
~~~

**Note:** The client name can also be set when establishing a connection using the `client_name` option in the context array (see [connect](#connect-open) and [pconnect](#pconnect-popen)). This is particularly useful with persistent connections, as the name will be set automatically when the connection is established and will persist across requests:

~~~php
// Set client name during connection
$redis = new Redis();
$redis->pconnect('127.0.0.1', 6379, 0, 'my-pool', 0, 0, [
'client_name' => 'api-server-1'
]);

// Or using the constructor
$redis = new Redis([
'host' => '127.0.0.1',
'port' => 6379,
'persistent' => 'my-pool',
'client_name' => 'api-server-1'
]);
~~~

###### *Return value*
This will vary depending on which client command was executed.

Expand Down
17 changes: 17 additions & 0 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ typedef enum {
REDIS_SOCK_STATUS_FAILED = -1,
REDIS_SOCK_STATUS_DISCONNECTED,
REDIS_SOCK_STATUS_CONNECTED,
REDIS_SOCK_STATUS_REUSED,
REDIS_SOCK_STATUS_AUTHENTICATED,
REDIS_SOCK_STATUS_READY
} redis_sock_status;
Expand Down Expand Up @@ -256,8 +257,23 @@ static inline int redis_strncmp(const char *s1, const char *s2, size_t n) {
typedef struct RedisHello {
zend_string *server;
zend_string *version;
int version_major;
int version_minor;
} RedisHello;

/* Helper to verify server version information */
static inline int redis_version_less_than(const struct RedisHello *hello, int major, int minor) {
if (hello == NULL || hello->version == NULL || ZSTR_LEN(hello->version) == 0) {
return 1;
}

if (hello->version_major < major)
return 1;
if (hello->version_major == major && hello->version_minor < minor)
return 1;
return 0;
}

/* {{{ struct RedisSock */
typedef struct {
php_stream *stream;
Expand All @@ -266,6 +282,7 @@ typedef struct {
int port;
zend_string *user;
zend_string *pass;
zend_string *client_name;
double timeout;
double read_timeout;
long retry_interval;
Expand Down
Loading