Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 19e6f31

Browse files
Merge branch 'release/2.2.4'
2 parents 4c24dfa + 8ed1a29 commit 19e6f31

17 files changed

+849
-377
lines changed

README.markdown

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ _**Description**_: Connects to a Redis instance.
165165
*host*: string. can be a host, or the path to a unix domain socket
166166
*port*: int, optional
167167
*timeout*: float, value in seconds (optional, default is 0 meaning unlimited)
168+
*reserved*: should be NULL if retry_interval is specified
169+
*retry_interval*: int, value in milliseconds (optional)
168170

169171
##### *Return value*
170172

@@ -177,6 +179,7 @@ $redis->connect('127.0.0.1', 6379);
177179
$redis->connect('127.0.0.1'); // port 6379 by default
178180
$redis->connect('127.0.0.1', 6379, 2.5); // 2.5 sec timeout.
179181
$redis->connect('/tmp/redis.sock'); // unix domain socket.
182+
$redis->connect('127.0.0.1', 6379, 1, NULL, 100); // 1 sec timeout, 100ms delay between reconnection attempts.
180183
~~~
181184

182185
### pconnect, popen
@@ -199,6 +202,7 @@ persistent equivalents.
199202
*port*: int, optional
200203
*timeout*: float, value in seconds (optional, default is 0 meaning unlimited)
201204
*persistent_id*: string. identity for the requested persistent connection
205+
*retry_interval*: int, value in milliseconds (optional)
202206

203207
##### *Return value*
204208

@@ -322,6 +326,7 @@ _**Description**_: Sends a string to Redis, which replies with the same string
322326
1. [save](#save) - Synchronously save the dataset to disk (wait to complete)
323327
1. [slaveof](#slaveof) - Make the server a slave of another instance, or promote it to master
324328
1. [time](#time) - Return the current server time
329+
1. [slowlog](#slowlog) - Access the Redis slowlog entries
325330

326331
### bgrewriteaof
327332
-----
@@ -539,6 +544,36 @@ the unix timestamp, and element one being microseconds.
539544
$redis->time();
540545
~~~
541546

547+
### slowlog
548+
-----
549+
_**Description**_: Access the Redis slowlog
550+
551+
##### *Parameters*
552+
*Operation* (string): This can be either `GET`, `LEN`, or `RESET`
553+
*Length* (integer), optional: If executing a `SLOWLOG GET` command, you can pass an optional length.
554+
#####
555+
556+
##### *Return value*
557+
The return value of SLOWLOG will depend on which operation was performed.
558+
SLOWLOG GET: Array of slowlog entries, as provided by Redis
559+
SLOGLOG LEN: Integer, the length of the slowlog
560+
SLOWLOG RESET: Boolean, depending on success
561+
#####
562+
563+
##### *Examples*
564+
~~~
565+
// Get ten slowlog entries
566+
$redis->slowlog('get', 10);
567+
// Get the default number of slowlog entries
568+
569+
$redis->slowlog('get');
570+
// Reset our slowlog
571+
$redis->slowlog('reset');
572+
573+
// Retrieve slowlog length
574+
$redis->slowlog('len');
575+
~~~
576+
542577
## Keys and Strings
543578

544579
### Strings
@@ -604,19 +639,30 @@ $redis->get('key');
604639

605640
### set
606641
-----
607-
_**Description**_: Set the string value in argument as value of the key.
642+
_**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
608643

609644
##### *Parameters*
610645
*Key*
611646
*Value*
612-
*Timeout* (optional). Calling `SETEX` is preferred if you want a timeout.
647+
*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
613648

614649
##### *Return value*
615650
*Bool* `TRUE` if the command is successful.
616651

617652
##### *Examples*
618653
~~~
654+
// Simple key -> value set
619655
$redis->set('key', 'value');
656+
657+
// Will redirect, and actually make an SETEX call
658+
$redis->set('key','value', 10);
659+
660+
// Will set the key, if it doesn't exist, with a ttl of 10 seconds
661+
$redis->set('key', 'value', Array('nx', 'ex'=>10);
662+
663+
// Will set a key, if it does exist, with a ttl of 1000 miliseconds
664+
$redis->set('key', 'value', Array('xx', 'px'=>1000);
665+
620666
~~~
621667

622668
### setex, psetex

arrays.markdown

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ There are several ways of creating Redis arrays; they can be pre-defined in red
1717

1818
#### Declaring a new array with a list of nodes
1919
<pre>
20-
$ra = new RedisArray(array("host1", "host2:63792, "host2:6380"));
20+
$ra = new RedisArray(array("host1", "host2:63792", "host2:6380"));
2121
</pre>
2222

2323

@@ -26,15 +26,27 @@ $ra = new RedisArray(array("host1", "host2:63792, "host2:6380"));
2626
function extract_key_part($k) {
2727
return substr($k, 0, 3); // hash only on first 3 characters.
2828
}
29-
$ra = new RedisArray(array("host1", "host2:63792, "host2:6380"), array("function" => "extract_key_part"));
29+
$ra = new RedisArray(array("host1", "host2:63792", "host2:6380"), array("function" => "extract_key_part"));
3030
</pre>
3131

3232
#### Defining a "previous" array when nodes are added or removed.
3333
When a new node is added to an array, phpredis needs to know about it. The old list of nodes becomes the “previous” array, and the new list of nodes is used as a main ring. Right after a node has been added, some read commands will point to the wrong nodes and will need to look up the keys in the previous ring.
3434

3535
<pre>
3636
// adding host3 to a ring containing host1 and host2. Read commands will look in the previous ring if the data is not found in the main ring.
37-
$ra = new RedisArray(array('host1', 'host2', 'host3'), array('previous' => array('host1', 'host2')));
37+
$ra = new RedisArray(array("host1", "host2", "host3"), array("previous" => array("host1", "host2")));
38+
</pre>
39+
40+
#### Specifying the "retry_interval" parameter
41+
The retry_interval is used to specify a delay in milliseconds between reconnection attempts in case the client loses connection with a server
42+
<pre>
43+
$ra = new RedisArray(array("host1", "host2:63792", "host2:6380"), array("retry_timeout" => 100)));
44+
</pre>
45+
46+
#### Specifying the "lazy_connect" parameter
47+
This option is useful when a cluster has many shards but not of them are necessarily used at one time.
48+
<pre>
49+
$ra = new RedisArray(array("host1", "host2:63792", "host2:6380"), array("lazy_connect" => true)));
3850
</pre>
3951

4052
#### Defining arrays in Redis.ini
@@ -76,6 +88,13 @@ In order to control the distribution of keys by hand, you can provide a custom f
7688

7789
For instance, instanciate a RedisArray object with `new RedisArray(array("us-host", "uk-host", "de-host"), array("distributor" => "dist"));` and write a function called "dist" that will return `2` for all the keys that should end up on the "de-host" server.
7890

91+
### Example
92+
<pre>
93+
$ra = new RedisArray(array("host1", "host2", "host3", "host4", "host5", "host6", "host7", "host8"), array("distributor" => array(2, 2)));
94+
</pre>
95+
96+
This declares that we started with 2 shards and moved to 4 then 8 shards. The number of initial shards is 2 and the resharding level (or number of iterations) is 2.
97+
7998
## Migrating keys
8099

81100
When a node is added or removed from a ring, RedisArray instances must be instanciated with a “previous” list of nodes. A single call to `$ra->_rehash()` causes all the keys to be redistributed according to the new list of nodes. Passing a callback function to `_rehash()` makes it possible to track the progress of that operation: the function is called with a node name and a number of keys that will be examined, e.g. `_rehash(function ($host, $count){ ... });`.

common.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
#ifndef REDIS_COMMON_H
66
#define REDIS_COMMON_H
77

8+
/* NULL check so Eclipse doesn't go crazy */
9+
#ifndef NULL
10+
#define NULL ((void *) 0)
11+
#endif
12+
813
#define redis_sock_name "Redis Socket Buffer"
914
#define REDIS_SOCK_STATUS_FAILED 0
1015
#define REDIS_SOCK_STATUS_DISCONNECTED 1
@@ -138,6 +143,15 @@ else if(redis_sock->mode == MULTI) { \
138143

139144
#define REDIS_PROCESS_RESPONSE(function) REDIS_PROCESS_RESPONSE_CLOSURE(function, NULL)
140145

146+
/* Extended SET argument detection */
147+
#define IS_EX_ARG(a) ((a[0]=='e' || a[0]=='E') && (a[1]=='x' || a[1]=='X') && a[2]=='\0')
148+
#define IS_PX_ARG(a) ((a[0]=='p' || a[0]=='P') && (a[1]=='x' || a[1]=='X') && a[2]=='\0')
149+
#define IS_NX_ARG(a) ((a[0]=='n' || a[0]=='N') && (a[1]=='x' || a[1]=='X') && a[2]=='\0')
150+
#define IS_XX_ARG(a) ((a[0]=='x' || a[0]=='X') && (a[1]=='x' || a[1]=='X') && a[2]=='\0')
151+
152+
#define IS_EX_PX_ARG(a) (IS_EX_ARG(a) || IS_PX_ARG(a))
153+
#define IS_NX_XX_ARG(a) (IS_NX_ARG(a) || IS_XX_ARG(a))
154+
141155
typedef enum {ATOMIC, MULTI, PIPELINE} redis_mode;
142156

143157
typedef struct fold_item {
@@ -182,6 +196,7 @@ typedef struct {
182196

183197
char *err;
184198
int err_len;
199+
zend_bool lazy_connect;
185200
} RedisSock;
186201
/* }}} */
187202

config.h

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/* config.h. Generated from config.h.in by configure. */
2+
/* config.h.in. Generated from configure.in by autoheader. */
3+
4+
/* Whether to build redis as dynamic module */
5+
#define COMPILE_DL_REDIS 1
6+
7+
/* Define to 1 if you have the <dlfcn.h> header file. */
8+
#define HAVE_DLFCN_H 1
9+
10+
/* Define to 1 if you have the <inttypes.h> header file. */
11+
#define HAVE_INTTYPES_H 1
12+
13+
/* Define to 1 if you have the <memory.h> header file. */
14+
#define HAVE_MEMORY_H 1
15+
16+
/* Whether redis igbinary serializer is enabled */
17+
/* #undef HAVE_REDIS_IGBINARY */
18+
19+
/* Define to 1 if you have the <stdint.h> header file. */
20+
#define HAVE_STDINT_H 1
21+
22+
/* Define to 1 if you have the <stdlib.h> header file. */
23+
#define HAVE_STDLIB_H 1
24+
25+
/* Define to 1 if you have the <strings.h> header file. */
26+
#define HAVE_STRINGS_H 1
27+
28+
/* Define to 1 if you have the <string.h> header file. */
29+
#define HAVE_STRING_H 1
30+
31+
/* Define to 1 if you have the <sys/stat.h> header file. */
32+
#define HAVE_SYS_STAT_H 1
33+
34+
/* Define to 1 if you have the <sys/types.h> header file. */
35+
#define HAVE_SYS_TYPES_H 1
36+
37+
/* Define to 1 if you have the <unistd.h> header file. */
38+
#define HAVE_UNISTD_H 1
39+
40+
/* Define to the sub-directory in which libtool stores uninstalled libraries.
41+
*/
42+
#define LT_OBJDIR ".libs/"
43+
44+
/* Define to 1 if your C compiler doesn't accept -c and -o together. */
45+
/* #undef NO_MINUS_C_MINUS_O */
46+
47+
/* Define to the address where bug reports for this package should be sent. */
48+
#define PACKAGE_BUGREPORT ""
49+
50+
/* Define to the full name of this package. */
51+
#define PACKAGE_NAME ""
52+
53+
/* Define to the full name and version of this package. */
54+
#define PACKAGE_STRING ""
55+
56+
/* Define to the one symbol short name of this package. */
57+
#define PACKAGE_TARNAME ""
58+
59+
/* Define to the home page for this package. */
60+
#define PACKAGE_URL ""
61+
62+
/* Define to the version of this package. */
63+
#define PACKAGE_VERSION ""
64+
65+
/* redis sessions */
66+
#define PHP_SESSION 1
67+
68+
/* Define to 1 if you have the ANSI C header files. */
69+
#define STDC_HEADERS 1

debian.control

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Package: phpredis
2-
Version: 2.2.2
2+
Version: 2.2.4
33
Section: web
44
Priority: optional
55
Architecture: all

library.c

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,22 @@ int redis_cmd_append_str(char **cmd, int cmd_len, char *append, int append_len)
454454
return buf.len;
455455
}
456456

457+
/*
458+
* Given a smart string, number of arguments, a keyword, and the length of the keyword
459+
* initialize our smart string with the proper Redis header for the command to follow
460+
*/
461+
int redis_cmd_init_sstr(smart_str *str, int num_args, char *keyword, int keyword_len) {
462+
smart_str_appendc(str, '*');
463+
smart_str_append_long(str, num_args + 1);
464+
smart_str_appendl(str, _NL, sizeof(_NL) -1);
465+
smart_str_appendc(str, '$');
466+
smart_str_append_long(str, keyword_len);
467+
smart_str_appendl(str, _NL, sizeof(_NL) - 1);
468+
smart_str_appendl(str, keyword, keyword_len);
469+
smart_str_appendl(str, _NL, sizeof(_NL) - 1);
470+
return str->len;
471+
}
472+
457473
/*
458474
* Append a command sequence to a smart_str
459475
*/
@@ -468,6 +484,44 @@ int redis_cmd_append_sstr(smart_str *str, char *append, int append_len) {
468484
return str->len;
469485
}
470486

487+
/*
488+
* Append an integer to a smart string command
489+
*/
490+
int redis_cmd_append_sstr_int(smart_str *str, int append) {
491+
char int_buf[32];
492+
int int_len = snprintf(int_buf, sizeof(int_buf), "%d", append);
493+
return redis_cmd_append_sstr(str, int_buf, int_len);
494+
}
495+
496+
/*
497+
* Append a long to a smart string command
498+
*/
499+
int redis_cmd_append_sstr_long(smart_str *str, long append) {
500+
char long_buf[32];
501+
int long_len = snprintf(long_buf, sizeof(long_buf), "%ld", append);
502+
return redis_cmd_append_sstr(str, long_buf, long_len);
503+
}
504+
505+
/*
506+
* Append a double to a smart string command
507+
*/
508+
int redis_cmd_append_sstr_dbl(smart_str *str, double value) {
509+
char *dbl_str;
510+
int dbl_len;
511+
512+
/// Convert to double
513+
REDIS_DOUBLE_TO_STRING(dbl_str, dbl_len, value);
514+
515+
// Append the string
516+
int retval = redis_cmd_append_sstr(str, dbl_str, dbl_len);
517+
518+
// Free our double string
519+
efree(dbl_str);
520+
521+
// Return new length
522+
return retval;
523+
}
524+
471525
/*
472526
* Append an integer command to a Redis command
473527
*/
@@ -970,7 +1024,8 @@ PHPAPI void redis_ping_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_s
9701024
*/
9711025
PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short port,
9721026
double timeout, int persistent, char *persistent_id,
973-
long retry_interval)
1027+
long retry_interval,
1028+
zend_bool lazy_connect)
9741029
{
9751030
RedisSock *redis_sock;
9761031

@@ -982,6 +1037,7 @@ PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short por
9821037
redis_sock->dbNumber = 0;
9831038
redis_sock->retry_interval = retry_interval * 1000;
9841039
redis_sock->persistent = persistent;
1040+
redis_sock->lazy_connect = lazy_connect;
9851041

9861042
if(persistent_id) {
9871043
size_t persistent_id_len = strlen(persistent_id);

library.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ int redis_cmd_format(char **ret, char *format, ...);
44
int redis_cmd_format_static(char **ret, char *keyword, char *format, ...);
55
int redis_cmd_format_header(char **ret, char *keyword, int arg_count);
66
int redis_cmd_append_str(char **cmd, int cmd_len, char *append, int append_len);
7+
int redis_cmd_init_sstr(smart_str *str, int num_args, char *keyword, int keyword_len);
78
int redis_cmd_append_sstr(smart_str *str, char *append, int append_len);
9+
int redis_cmd_append_sstr_int(smart_str *str, int append);
10+
int redis_cmd_append_sstr_long(smart_str *str, long append);
811
int redis_cmd_append_int(char **cmd, int cmd_len, int append);
9-
12+
int redis_cmd_append_sstr_dbl(smart_str *str, double value);
1013

1114
PHPAPI char * redis_sock_read(RedisSock *redis_sock, int *buf_len TSRMLS_DC);
1215

@@ -20,7 +23,7 @@ PHPAPI void redis_string_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis
2023
PHPAPI void redis_ping_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
2124
PHPAPI void redis_info_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
2225
PHPAPI void redis_type_response(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx);
23-
PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short port, double timeout, int persistent, char *persistent_id, long retry_interval);
26+
PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short port, double timeout, int persistent, char *persistent_id, long retry_interval, zend_bool lazy_connect);
2427
PHPAPI int redis_sock_connect(RedisSock *redis_sock TSRMLS_DC);
2528
PHPAPI int redis_sock_server_open(RedisSock *redis_sock, int force_connect TSRMLS_DC);
2629
PHPAPI int redis_sock_disconnect(RedisSock *redis_sock TSRMLS_DC);

0 commit comments

Comments
 (0)