8000 Merge nicolasff:b9a16b5ad5 in, fixing for Win32 · jrtkcoder/phpredis@9c12c40 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9c12c40

Browse files
committed
Merge nicolasff:b9a16b5ad5 in, fixing for Win32
Now we should be up to master with upstream for an easier merge.
1 parent 978fbcf commit 9c12c40

14 files changed

+1352
-86
lines changed

CREDITS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ Redis client extension for PHP
22
Alfonso Jimenez (yo@alfonsojimenez.com)
33
Nasreddine Bouafif (n.bouafif@owlient.eu)
44
Nicolas Favre-Felix (n.favre-felix@owlient.eu)
5+
Michael Grunder (michael.grunder@gmail.com)

README.markdown

Lines changed: 172 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
The phpredis extension provides an API for communicating with the [Redis](http://redis.io/) key-value store. It is released under the [PHP License, version 3.01](http://www.php.net/license/3_01.txt).
44
This code has been developed and maintained by Owlient from November 2009 to March 2011.
55

6-
You can send comments, patches, questions [here on github](https://github.com/nicolasff/phpredis/issues) or to n.favrefelix@gmail.com ([@yowgi](http://twitter.com/yowgi)).
6+
You can send comments, patches, questions [here on github](https://github.com/nicolasff/phpredis/issues), to n.favrefelix@gmail.com ([@yowgi](http://twitter.com/yowgi)), or to michael.grunder@gmail.com ([@grumi78](http://twitter.com/grumi78)).
77

88

99
# Table of contents
@@ -69,6 +69,10 @@ Taken from [Compiling phpredis on Zend Server CE/OSX ](http://www.tumblr.com/tag
6969

7070
See also: [Install Redis & PHP Extension PHPRedis with Macports](http://www.lecloud.net/post/3378834922/install-redis-php-extension-phpredis-with-macports).
7171

72+
You can install install it using Homebrew:
73+
74+
- [Get homebrew-php](https://github.com/josegonzalez/homebrew-php)
75+
- `brew install php55-redis` (or php53-redis, php54-redis)
7276

7377
## PHP Session handler
7478

@@ -268,6 +272,15 @@ $redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP); // use built-in
268272
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY); // use igBinary serialize/unserialize
269273
270274
$redis->setOption(Redis::OPT_PREFIX, 'myAppName:'); // use custom prefix on all keys
275+
276+
/* Options for the SCAN family of commands, indicating whether to abstract
277+
empty results from the user. If set to SCAN_NORETRY (the default), phpredis
278+
will just issue one SCAN command at a time, sometimes returning an empty
279+
array of results. If set to SCAN_RETRY, phpredis will retry the scan command
280+
until keys come back OR Redis returns an iterator of zero
281+
*/
282+
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_NORETRY);
283+
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
271284
~~~
272285

273286

@@ -607,6 +620,7 @@ $redis->slowlog('len');
607620
* [expire, setTimeout, pexpire](#expire-settimeout-pexpire) - Set a key's time to live in seconds
608621
* [expireAt, pexpireAt](#expireat-pexpireat) - Set the expiration for a key as a UNIX timestamp
609622
* [keys, getKeys](#keys-getkeys) - Find all keys matching the given pattern
623+
* [scan](#scan) - Scan for keys in the keyspace (Redis >= 2.8.0)
610624
* [migrate](#migrate) - Atomically transfer a key from a Redis instance to another one
611625
* [move](#move) - Move a key to another database
612626
* [object](#object) - Inspect the internals of Redis objects
@@ -658,10 +672,10 @@ $redis->set('key', 'value');
658672
$redis->set('key','value', 10);
659673
660674
// Will set the key, if it doesn't exist, with a ttl of 10 seconds
661-
$redis->set('key', 'value', Array('nx', 'ex'=>10);
675+
$redis->set('key', 'value', Array('nx', 'ex'=>10));
662676
663677
// Will set a key, if it does exist, with a ttl of 1000 miliseconds
664-
$redis->set('key', 'value', Array('xx', 'px'=>1000);
678+
$redis->set('key', 'value', Array('xx', 'px'=>1000));
665679
666680
~~~
667681

@@ -780,7 +794,7 @@ $redis->incrByFloat('key1', 1.5); /* key1 didn't exist, so it will now be 1.5 */
780794
781795
$redis->incrByFloat('key1', 1.5); /* 3 */
782796
$redis->incrByFloat('key1', -1.5); /* 1.5 */
783-
$redis->incrByFloat('key1', 2.5); /* 3.5 */
797+
$redis->incrByFloat('key1', 2.5); /* 4 */
784798
~~~
785799

786800
### decr, decrBy
@@ -953,7 +967,29 @@ $allKeys = $redis->keys('*'); // all keys will match this.
953967
$keyWithUserPrefix = $redis->keys('user*');
954968
~~~
955969

970+
### scan
971+
-----
972+
_**Description**_: Scan the keyspace for keys
973+
974+
##### *Parameters*
975+
*LONG (reference)*: Iterator, initialized to NULL
976+
*STRING, Optional*: Pattern to match
977+
*LONG, Optional*: Count of keys per iteration (only a suggestion to Redis)
956978

979+
##### *Return value*
980+
*Array, boolean*: This function will return an array of keys or FALSE if there are no more keys
981+
982+
##### *Example*
983+
~~~
984+
$it = NULL; /* Initialize our iterator to NULL */
985+
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY); /* retry when we get no keys back */
986+
while($arr_keys = $redis->scan($it)) {
987+
foreach($arr_keys as $str_key) {
988+
echo "Here is a key: $str_key\n";
989+
}
990+
echo "No more keys to scan!\n";
991+
}
992+
~~~
957993

958994
### object
959995
-----
@@ -1261,9 +1297,13 @@ _**Description**_: Migrates a key to a different Redis instance.
12611297
*key* string. The key to migrate.
12621298
*destination-db* integer. The target DB.
12631299
*timeout* integer. The maximum amount of time given to this transfer.
1300+
*copy* boolean, optional. Should we send the COPY flag to redis
1301+
*replace* boolean, optional. Should we send the REPLACE flag to redis
12641302
##### *Examples*
12651303
~~~
12661304
$redis->migrate('backup', 6379, 'foo', 0, 3600);
1305+
$redis->migrate('backup', 6379, 'foo', 0, 3600, true, true); /* copy and replace */
1306+
$redis->migrate('backup', 6379, 'foo', 0, 3600, false, true); /* just REPLACE flag */
12671307
~~~
12681308

12691309

@@ -1283,6 +1323,7 @@ $redis->migrate('backup', 6379, 'foo', 0, 3600);
12831323
* [hSet](#hset) - Set the string value of a hash field
12841324
* [hSetNx](#hsetnx) - Set the value of a hash field, only if the field does not exist
12851325
* [hVals](#hvals) - Get all the values in a hash
1326+
* [hScan](#hscan) - Scan a hash key for members
12861327

12871328
### hSet
12881329
-----
@@ -1542,7 +1583,28 @@ $redis->hSet('h', 'field2', 'value2');
15421583
$redis->hmGet('h', array('field1', 'field2')); /* returns array('field1' => 'value1', 'field2' => 'value 10000 2') */
15431584
~~~
15441585

1586+
### hScan
1587+
-----
1588+
_**Description**_: Scan a HASH value for members, with an optional pattern and count
1589+
##### *Parameters*
1590+
*key*: String
1591+
*iterator*: Long (reference)
1592+
*pattern*: Optional pattern to match against
1593+
*count*: How many keys to return in a go (only a sugestion to Redis)
1594+
##### *Return value*
1595+
*Array* An array of members that match our pattern
15451596

1597+
##### *Examples*
1598+
~~~
1599+
$it = NULL;
1600+
/* Don't ever return an empty array until we're done iterating */
1601+
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
1602+
while($arr_keys = $redis->hscan('hash', $it)) {
1603+
foreach($arr_keys as $str_field => $str_value) {
1604+
echo "$str_field => $str_value\n"; /* Print the hash member and value */
1605+
}
1606+
}
1607+
~~~
15461608

15471609
## Lists
15481610

@@ -1981,6 +2043,7 @@ $redis->lSize('key1');/* 2 */
19812043
* [sRem, sRemove](#srem-sremove) - Remove one or more members from a set
19822044
* [sUnion](#sunion) - Add multiple sets
19832045
* [sUnionStore](#sunionstore) - Add multiple sets and store the resulting set in a key
2046+
* [sScan](#sscan) - Scan a set for members
19842047

19852048
### sAdd
19862049
-----
@@ -2380,6 +2443,41 @@ array(4) {
23802443
}
23812444
~~~
23822445

2446+
### sScan
2447+
-----
2448+
_**Description**_: Scan a set for members
2449+
2450+
##### *Parameters*
2451+
*Key*: The set to search
2452+
*iterator*: LONG (reference) to the iterator as we go
2453+
*pattern*: String, optional pattern to match against
2454+
*count*: How many members to return at a time (Redis might return a different amount)
2455+
2456+
##### *Return value*
2457+
*Array, boolean*: PHPRedis will return an array of keys or FALSE when we're done iterating
2458+
2459+
##### *Example*
2460+
~~~
2461+
$it = NULL;
2462+
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY); /* don't return empty results until we're done */
2463+
while($arr_mems = $redis->sscan('set', $it, "*pattern*")) {
2464+
foreach($arr_mems as $str_mem) {
2465+
echo "Member: $str_mem\n";
2466+
}
2467+
}
2468+
2469+
$it = NULL;
2470+
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_NORETRY); /* return after each iteration, even if empty */
2471+
while(($arr_mems = $redis->sscan('set', $it, "*pattern*"))!==FALSE) {
2472+
if(count($arr_mems) > 0) {
2473+
foreach($arr_mems as $str_mem) {
2474+
echo "Member found: $str_mem\n";
2475+
}
2476+
} else {
2477+
echo "No members in this iteration, iterator value: $it\n";
2478+
}
2479+
}
2480+
~~~
23832481

23842482
## Sorted sets
23852483

@@ -2397,6 +2495,7 @@ array(4) {
23972495
* [zRevRange](#zrevrange) - Return a range of members in a sorted set, by index, with scores ordered from high to low
23982496
* [zScore](#zscore) - Get the score associated with the given member in a sorted set
23992497
* [zUnion](#zunion) - Add multiple sorted sets and store the resulting sorted set in a new key
2498+
* [zScan](#zscan) - Scan a sorted set for members
24002499

24012500
### zAdd
24022501
-----
@@ -2736,11 +2835,36 @@ $redis->zUnion('ko2', array('k1', 'k2'), array(1, 1)); /* 4, 'ko2' => array('val
27362835
$redis->zUnion('ko3', array('k1', 'k2'), array(5, 1)); /* 4, 'ko3' => array('val0', 'val2', 'val3', 'val1') */
27372836
~~~
27382837

2838+
### zScan
2839+
-----
2840+
_**Description**_: Scan a sorted set for members, with optional pattern and count
2841+
2842+
##### *Parameters*
2843+
*key*: String, the set to scan
2844+
*iterator*: Long (reference), initialized to NULL
2845+
*pattern*: String (optional), the pattern to match
2846+
*count*: How many keys to return per iteration (Redis might return a different number)
2847+
2848+
##### *Return value*
2849+
*Array, boolean* PHPRedis will return matching keys from Redis, or FALSE when iteration is complete
2850+
2851+
##### *Example*
2852+
~~~
2853+
$it = NULL;
2854+
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
2855+
while($arr_matches = $redis->zscan('zset', $it, '*pattern*')) {
2856+
foreach($arr_matches as $str_mem => $f_score) {
2857+
echo "Key: $str_mem, Score: $f_score\n";
2858+
}
2859+
}
2860+
~~~
2861+
27392862
## Pub/sub
27402863

27412864
* [psubscribe](#psubscribe) - Subscribe to channels by pattern
27422865
* [publish](#publish) - Post a message to a channel
27432866
* [subscribe](#subscribe) - Subscribe to channels
2867+
* [pubsub](#pubsub) - Introspection into the pub/sub subsystem
27442868

27452869
### psubscribe
27462870
-----
@@ -2801,6 +2925,26 @@ function f($redis, $chan, $msg) {
28012925
$redis->subscribe(array('chan-1', 'chan-2', 'chan-3'), 'f'); // subscribe to 3 chans
28022926
~~~
28032927

2928+
### pubsub
2929+
-----
2930+
_**Description**_: A command allowing you to get information on the Redis pub/sub system.
2931+
2932+
##### *Parameters*
2933+
*keyword*: String, which can be: "channels", "numsub", or "numpat"
2934+
*argument*: Optional, variant. For the "channels" subcommand, you can pass a string pattern. For "numsub" an array of channel names.
2935+
2936+
##### *Return value*
2937+
*CHANNELS*: Returns an array where the members are the matching channels.
2938+
*NUMSUB*: Returns a key/value array where the keys are channel names and values are their counts.
2939+
*NUMPAT*: Integer return containing the number active pattern subscriptions
2940+
2941+
##### *Example*
2942+
~~~
2943+
$redis->pubsub("channels"); /*All channels */
2944+
$redis->pubsub("channels", "*pattern*"); /* Just channels matching your pattern */
2945+
$redis->pubsub("numsub", Array("chan1", "chan2")); /*Get subscriber counts for 'chan1' and 'chan2'*/
2946+
$redsi->pubsub("numpat"); /* Get the number of pattern subscribers */
2947+
```
28042948
28052949
## Transactions
28062950
@@ -2867,6 +3011,7 @@ $ret = FALSE if x has been modified between the call to WATCH and the call to EX
28673011
* [clearLastError](#) - Clear the last error message
28683012
* [_prefix](#) - A utility method to prefix the value with the prefix setting for phpredis
28693013
* [_unserialize](#) - A utility method to unserialize data with whatever serializer is set up
3014+
* [_serialize](#) - A utility method to serialize data with whatever serializer is set up
28703015
28713016
### eval
28723017
-----
@@ -3016,6 +3161,28 @@ $redis->setOption(Redis::OPT_PREFIX, 'my-prefix:');
30163161
$redis->_prefix('my-value'); // Will return 'my-prefix:my-value'
30173162
~~~
30183163
3164+
### _serialize
3165+
-----
3166+
_**Description**_: A utility method to serialize values manually.
3167+
3168+
This method allows you to serialize a value with whatever serializer is configured, manually.
3169+
This can be useful for serialization/unserialization of data going in and out of EVAL commands
3170+
as phpredis can't automatically do this itself. Note that if no serializer is set, phpredis
3171+
will change Array values to 'Array', and Objects to 'Object'.
3172+
3173+
##### *Parameters*
3174+
*value*: Mixed. The value to be serialized
3175+
3176+
##### *Examples*
3177+
~~~
3178+
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE);
3179+
$redis->_serialize("foo"); // returns "foo"
3180+
$redis->_serialize(Array()); // Returns "Array"
3181+
$redis->_serialize(new stdClass()); // Returns "Object"
3182+
3183+
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
3184+
$redis->_serialize("foo"); // Returns 's:3:"foo";'
3185+
30193186
### _unserialize
30203187
-----
30213188
_**Description**_: A utility method to unserialize data with whatever serializer is set up.
@@ -3080,7 +3247,7 @@ None
30803247

30813248
### GetTimeout
30823249
-----
3083-
_**Description**_: Get the (write) timeout in use for phpreids
3250+
_**Description**_: Get the (write) timeout in use for phpredis
30843251

30853252
##### *Parameters*
30863253
None

common.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,48 @@ typedef enum _REDIS_REPLY_TYPE {
3737
TYPE_MULTIBULK = '*'
3838
} REDIS_REPLY_TYPE;
3939

40+
/* SCAN variants */
41+
typedef enum _REDIS_SCAN_TYPE {
42+
TYPE_SCAN,
43+
TYPE_SSCAN,
44+
TYPE_HSCAN,
45+
TYPE_ZSCAN
46+
} REDIS_SCAN_TYPE;
47+
48+
/* PUBSUB subcommands */
49+
typedef enum _PUBSUB_TYPE {
50+
PUBSUB_CHANNELS,
51+
PUBSUB_NUMSUB,
52+
PUBSUB_NUMPAT
53+
} PUBSUB_TYPE;
54+
4055
/* options */
4156
#define REDIS_OPT_SERIALIZER 1
4257
#define REDIS_OPT_PREFIX 2
4358
#define REDIS_OPT_READ_TIMEOUT 3
59+
#define REDIS_OPT_SCAN 4
4460

4561
/* serializers */
4662
#define REDIS_SERIALIZER_NONE 0
4763
#define REDIS_SERIALIZER_PHP 1
4864
#define REDIS_SERIALIZER_IGBINARY 2
4965

66+
/* SCAN options */
67+
68+
#define REDIS_SCAN_NORETRY 0
69+
#define REDIS_SCAN_RETRY 1
70+
71+
/* GETBIT/SETBIT offset range limits */
72+
#define BITOP_MIN_OFFSET 0
73+
#define BITOP_MAX_OFFSET 4294967295
74+
5075
#define IF_MULTI() if(redis_sock->mode == MULTI)
5176
#define IF_MULTI_OR_ATOMIC() if(redis_sock->mode == MULTI || redis_sock->mode == ATOMIC)\
5277

5378
#define IF_MULTI_OR_PIPELINE() if(redis_sock->mode == MULTI || redis_sock->mode == PIPELINE)
5479
#define IF_PIPELINE() if(redis_sock->mode == PIPELINE)
5580
#define IF_NOT_MULTI() if(redis_sock->mode != MULTI)
81+
#define IF_NOT_ATOMIC() if(redis_sock->mode != ATOMIC)
5682
#define IF_ATOMIC() if(redis_sock->mode == ATOMIC)
5783
#define ELSE_IF_MULTI() else if(redis_sock->mode == MULTI) { \
5884
if(redis_response_enqueued(redis_sock TSRMLS_CC) == 1) {\
@@ -197,6 +223,8 @@ typedef struct {
197223
char *err;
198224
int err_len;
199225
zend_bool lazy_connect;
226+
227+
int scan;
200228
} RedisSock;
201229
/* }}} */
202230

0 commit comments

Comments
 (0)
0