Skip to content

Commit e8c3b13

Browse files
committed
Add 'database' parameter to Redis sessions
Fixes GitHub issue phpredis#235.
1 parent e3417ac commit e8c3b13

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

README.markdown

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ session.save_path = "tcp://host1:6379?weight=1, tcp://host2:6379?weight=2&timeou
5656
* persistent (integer, should be 1 or 0): defines if a persistent connection should be used. **(experimental setting)**
5757
* 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.
5858
* auth (string, empty by default): used to authenticate with the Redis server prior to sending commands.
59+
* database (integer): selects a different database.
5960

6061
Sessions have a lifetime expressed in seconds and stored in the INI variable "session.gc_maxlifetime". You can change it with [`ini_set()`](http://php.net/ini_set).
6162
The session handler requires a version of Redis with the `SETEX` command (at least 2.0).

redis_session.c

+28-2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ typedef struct redis_pool_member_ {
4848

4949
RedisSock *redis_sock;
5050
int weight;
51+
int database;
5152

5253
char *prefix;
5354
size_t prefix_len;
@@ -75,11 +76,12 @@ redis_pool_new(TSRMLS_D) {
7576

7677
PHPAPI void
7778
redis_pool_add(redis_pool *pool, RedisSock *redis_sock, int weight,
78-
char *prefix, char *auth TSRMLS_DC) {
79+
int database, char *prefix, char *auth TSRMLS_DC) {
7980

8081
redis_pool_member *rpm = ecalloc(1, sizeof(redis_pool_member));
8182
rpm->redis_sock = redis_sock;
8283
rpm->weight = weight;
84+
rpm->database = database;
8385

8486
rpm->prefix = prefix;
8587
rpm->prefix_len = (prefix?strlen(prefix):0);
@@ -129,6 +131,22 @@ redis_pool_member_auth(redis_pool_member *rpm TSRMLS_DC) {
129131
efree(cmd);
130132
}
131133

134+
static void
135+
redis_pool_member_select(redis_pool_member *rpm TSRMLS_DC) {
136+
RedisSock *redis_sock = rpm->redis_sock;
137+
char *response, *cmd;
138+
int response_len, cmd_len;
139+
140+
cmd_len = redis_cmd_format_static(&cmd, "SELECT", "d", rpm->database);
141+
142+
if(redis_sock_write(redis_sock, cmd, cmd_len TSRMLS_CC) >= 0) {
143+
if ((response = redis_sock_read(redis_sock, &response_len TSRMLS_CC))) {
144+
efree(response);
145+
}
146+
}
147+
efree(cmd);
148+
}
149+
132150
PHPAPI redis_pool_member *
133151
redis_pool_get_sock(redis_pool *pool, const char *key TSRMLS_DC) {
134152

@@ -148,6 +166,9 @@ redis_pool_get_sock(redis_pool *pool, const char *key TSRMLS_DC) {
148166
if(needs_auth) {
149167
redis_pool_member_auth(rpm TSRMLS_CC);
150168
}
169+
if(rpm->database >= 0) { /* default is -1 which leaves the choice to redis. */
170+
redis_pool_member_select(rpm TSRMLS_CC);
171+
}
151172

152173
return rpm;
153174
}
@@ -183,6 +204,7 @@ PS_OPEN_FUNC(redis)
183204
int weight = 1;
184205
double timeout = 86400.0;
185206
int persistent = 0;
207+
int database = -1;
186208
char *prefix = NULL, *auth = NULL, *persistent_id = NULL;
187209

188210
/* translate unix: into file: */
@@ -234,6 +256,10 @@ PS_OPEN_FUNC(redis)
234256
if (zend_hash_find(Z_ARRVAL_P(params), "auth", sizeof("auth"), (void **) &param) != FAILURE) {
235257
auth = estrndup(Z_STRVAL_PP(param), Z_STRLEN_PP(param));
236258
}
259+
if (zend_hash_find(Z_ARRVAL_P(params), "database", sizeof("database"), (void **) &param) != FAILURE) {
260+
convert_to_long_ex(param);
261+
database = Z_LVAL_PP(param);
262+
}
237263

238264
/* // not supported yet
239265
if (zend_hash_find(Z_ARRVAL_P(params), "retry_interval", sizeof("retry_interval"), (void **) &param) != FAILURE) {
@@ -258,7 +284,7 @@ PS_OPEN_FUNC(redis)
258284
} else { /* unix */
259285
redis_sock = redis_sock_create(url->path, strlen(url->path), 0, timeout, persistent, persistent_id);
260286
}
261-
redis_pool_add(pool, redis_sock, weight, prefix, auth TSRMLS_CC);
287+
redis_pool_add(pool, redis_sock, weight, database, prefix, auth TSRMLS_CC);
262288

263289
php_url_free(url);
264290
}

0 commit comments

Comments
 (0)