3
3
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 ) .
4
4
This code has been developed and maintained by Owlient from November 2009 to March 2011.
5
5
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 ) ).
7
7
8
8
9
9
# Table of contents
@@ -69,6 +69,10 @@ Taken from [Compiling phpredis on Zend Server CE/OSX ](http://www.tumblr.com/tag
69
69
70
70
See also: [ Install Redis & PHP Extension PHPRedis with Macports] ( http://www.lecloud.net/post/3378834922/install-redis-php-extension-phpredis-with-macports ) .
71
71
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)
72
76
73
77
## PHP Session handler
74
78
@@ -268,6 +272,15 @@ $redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP); // use built-in
268
272
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY); // use igBinary serialize/unserialize
269
273
270
274
$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);
271
284
~~~
272
285
273
286
@@ -607,6 +620,7 @@ $redis->slowlog('len');
607
620
* [ expire, setTimeout, pexpire] ( #expire-settimeout-pexpire ) - Set a key's time to live in seconds
608
621
* [ expireAt, pexpireAt] ( #expireat-pexpireat ) - Set the expiration for a key as a UNIX timestamp
609
622
* [ keys, getKeys] ( #keys-getkeys ) - Find all keys matching the given pattern
623
+ * [ scan] ( #scan ) - Scan for keys in the keyspace (Redis >= 2.8.0)
610
624
* [ migrate] ( #migrate ) - Atomically transfer a key from a Redis instance to another one
611
625
* [ move] ( #move ) - Move a key to another database
612
626
* [ object] ( #object ) - Inspect the internals of Redis objects
@@ -658,10 +672,10 @@ $redis->set('key', 'value');
658
672
$redis->set('key','value', 10);
659
673
660
674
// 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)) ;
662
676
663
677
// 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)) ;
665
679
666
680
~~~
667
681
@@ -780,7 +794,7 @@ $redis->incrByFloat('key1', 1.5); /* key1 didn't exist, so it will now be 1.5 */
780
794
781
795
$redis->incrByFloat('key1', 1.5); /* 3 */
782
796
$redis->incrByFloat('key1', -1.5); /* 1.5 */
783
- $redis->incrByFloat('key1', 2.5); /* 3.5 */
797
+ $redis->incrByFloat('key1', 2.5); /* 4 */
784
798
~~~
785
799
786
800
### decr, decrBy
@@ -953,7 +967,29 @@ $allKeys = $redis->keys('*'); // all keys will match this.
953
967
$keyWithUserPrefix = $redis->keys('user*');
954
968
~~~
955
969
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)
956
978
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
+ ~~~
957
993
958
994
### object
959
995
-----
@@ -1261,9 +1297,13 @@ _**Description**_: Migrates a key to a different Redis instance.
1261
1297
* key* string. The key to migrate.
1262
1298
* destination-db* integer. The target DB.
1263
1299
* 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
1264
1302
##### * Examples*
1265
1303
~~~
1266
1304
$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 */
1267
1307
~~~
1268
1308
1269
1309
@@ -1283,6 +1323,7 @@ $redis->migrate('backup', 6379, 'foo', 0, 3600);
1283
1323
* [ hSet] ( #hset ) - Set the string value of a hash field
1284
1324
* [ hSetNx] ( #hsetnx ) - Set the value of a hash field, only if the field does not exist
1285
1325
* [ hVals] ( #hvals ) - Get all the values in a hash
1326
+ * [ hScan] ( #hscan ) - Scan a hash key for members
1286
1327
1287
1328
### hSet
1288
1329
-----
@@ -1542,7 +1583,28 @@ $redis->hSet('h', 'field2', 'value2');
1542
1583
$redis->hmGet('h', array('field1', 'field2')); /* returns array('field1' => 'value1', 'field2' => 'value
10000
2') */
1543
1584
~~~
1544
1585
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
1545
1596
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
+ ~~~
1546
1608
1547
1609
## Lists
1548
1610
@@ -1981,6 +2043,7 @@ $redis->lSize('key1');/* 2 */
1981
2043
* [ sRem, sRemove] ( #srem-sremove ) - Remove one or more members from a set
1982
2044
* [ sUnion] ( #sunion ) - Add multiple sets
1983
2045
* [ sUnionStore] ( #sunionstore ) - Add multiple sets and store the resulting set in a key
2046
+ * [ sScan] ( #sscan ) - Scan a set for members
1984
2047
1985
2048
### sAdd
1986
2049
-----
@@ -2380,6 +2443,41 @@ array(4) {
2380
2443
}
2381
2444
~~~
2382
2445
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
+ ~~~
2383
2481
2384
2482
## Sorted sets
2385
2483
@@ -2397,6 +2495,7 @@ array(4) {
2397
2495
* [ zRevRange] ( #zrevrange ) - Return a range of members in a sorted set, by index, with scores ordered from high to low
2398
2496
* [ zScore] ( #zscore ) - Get the score associated with the given member in a sorted set
2399
2497
* [ 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
2400
2499
2401
2500
### zAdd
2402
2501
-----
@@ -2736,11 +2835,36 @@ $redis->zUnion('ko2', array('k1', 'k2'), array(1, 1)); /* 4, 'ko2' => array('val
2736
2835
$redis->zUnion('ko3', array('k1', 'k2'), array(5, 1)); /* 4, 'ko3' => array('val0', 'val2', 'val3', 'val1') */
2737
2836
~~~
2738
2837
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
+
2739
2862
## Pub/sub
2740
2863
2741
2864
* [ psubscribe] ( #psubscribe ) - Subscribe to channels by pattern
2742
2865
* [ publish] ( #publish ) - Post a message to a channel
2743
2866
* [ subscribe] ( #subscribe ) - Subscribe to channels
2867
+ * [ pubsub] ( #pubsub ) - Introspection into the pub/sub subsystem
2744
2868
2745
2869
### psubscribe
2746
2870
-----
@@ -2801,6 +2925,26 @@ function f($redis, $chan, $msg) {
2801
2925
$redis->subscribe(array('chan-1', 'chan-2', 'chan-3'), 'f'); // subscribe to 3 chans
2802
2926
~~~
2803
2927
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
+ ```
2804
2948
2805
2949
## Transactions
2806
2950
@@ -2867,6 +3011,7 @@ $ret = FALSE if x has been modified between the call to WATCH and the call to EX
2867
3011
* [clearLastError](#) - Clear the last error message
2868
3012
* [_prefix](#) - A utility method to prefix the value with the prefix setting for phpredis
2869
3013
* [_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
2870
3015
2871
3016
### eval
2872
3017
-----
@@ -3016,6 +3161,28 @@ $redis->setOption(Redis::OPT_PREFIX, 'my-prefix:');
3016
3161
$redis->_ prefix('my-value'); // Will return 'my-prefix: my-value '
3017
3162
~~~
3018
3163
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
+
3019
3186
### _ unserialize
3020
3187
-----
3021
3188
_ ** Description** _ : A utility method to unserialize data with whatever serializer is set up.
@@ -3080,7 +3247,7 @@ None
3080
3247
3081
3248
### GetTimeout
3082
3249
-----
3083
- _ ** Description** _ : Get the (write) timeout in use for phpreids
3250
+ _ ** Description** _ : Get the (write) timeout in use for phpredis
3084
3251
3085
3252
##### * Parameters*
3086
3253
None
0 commit comments