@@ -268,6 +268,15 @@ $redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP); // use built-in
268
268
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY); // use igBinary serialize/unserialize
269
269
270
270
$redis->setOption(Redis::OPT_PREFIX, 'myAppName:'); // use custom prefix on all keys
271
+
272
+ /* Options for the SCAN family of commands, indicating whether to abstract
273
+ empty results from the user. If set to SCAN_NORETRY (the default), phpredis
274
+ will just issue one SCAN command at a time, sometimes returning an empty
275
+ array of results. If set to SCAN_RETRY, phpredis will retry the scan command
276
+ until keys come back OR Redis returns an iterator of zero
277
+ */
278
+ $redis->setOption(Redis::OPT_SCAN, Redis::SCAN_NORETRY);
279
+ $redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
271
280
~~~
272
281
273
282
@@ -607,6 +616,7 @@ $redis->slowlog('len');
607
616
* [ expire, setTimeout, pexpire] ( #expire-settimeout-pexpire ) - Set a key's time to live in seconds
608
617
* [ expireAt, pexpireAt] ( #expireat-pexpireat ) - Set the expiration for a key as a UNIX timestamp
609
618
* [ keys, getKeys] ( #keys-getkeys ) - Find all keys matching the given pattern
619
+ * [ scan] ( #scan ) - Scan for keys in the keyspace (Redis >= 2.8.0)
610
620
* [ migrate] ( #migrate ) - Atomically transfer a key from a Redis instance to another one
611
621
* [ move] ( #move ) - Move a key to another database
612
622
* [ object] ( #object ) - Inspect the internals of Redis objects
@@ -953,7 +963,29 @@ $allKeys = $redis->keys('*'); // all keys will match this.
953
963
$keyWithUserPrefix = $redis->keys('user*');
954
964
~~~
955
965
966
+ ### scan
967
+ -----
968
+ _ ** Description** _ : Scan the keyspace for keys
969
+
970
+ ##### * Parameters*
971
+ * LONG (reference)* : Iterator, initialized to NULL
972
+ * STRING, Optional* : Pattern to match
973
+ * LONG, Optional)* : Count of keys per iteration (only a suggestion to Redis)
956
974
975
+ ##### * Return value*
976
+ * Array, boolean* : This function will return an array of keys or FALSE if there are no more keys
977
+
978
+ ##### * Example*
979
+ ~~~
980
+ $it = NULL; /* Initialize our iterator to NULL */
981
+ $redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY); /* retry when we get no keys back */
982
+ while($arr_keys = $redis->scan($it)) {
983
+ foreach($arr_keys as $str_key) {
984
+ echo "Here is a key: $str_key\n";
985
+ }
986
+ echo "No more keys to scan!\n";
987
+ }
988
+ ~~~
957
989
958
990
### object
959
991
-----
@@ -1283,7 +1315,7 @@ $redis->migrate('backup', 6379, 'foo', 0, 3600);
1283
1315
* [ hSet] ( #hset ) - Set the string value of a hash field
1284
1316
* [ hSetNx] ( #hsetnx ) - Set the value of a hash field, only if the field does not exist
1285
1317
* [ hVals] ( #hvals ) - Get all the values in a hash
1286
-
1318
+ * [ hScan ] ( #hscan ) - Scan a hash key for members
1287
1319
### hSet
1288
1320
-----
1289
1321
_ ** Description** _ : Adds a value to the hash stored at key. If this value is already in the hash, ` FALSE ` is returned.
@@ -1542,7 +1574,28 @@ $redis->hSet('h', 'field2', 'value2');
1542
1574
$redis->hmGet('h', array('field1', 'field2')); /* returns array('field1' => 'value1', 'field2' => 'value2') */
1543
1575
~~~
1544
1576
1577
+ ### hScan
1578
+ -----
1579
+ _ ** Description** _ : Scan a HASH value for members, with an optional pattern and count
1580
+ ##### * Parameters*
1581
+ * key* : String
1582
+ * iterator* : Long (reference)
1583
+ * pattern* : Optional pattern to match against
1584
+ * count* : How many keys to return in a go (only a sugestion to Redis)
1585
+ ##### * Return value*
1586
+ * Array* An array of members that match our pattern
1545
1587
1588
+ ##### * Examples*
1589
+ ~~~
1590
+ $it = NULL;
1591
+ /* Don't ever return an empty array until we're done iterating */
1592
+ $redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
1593
+ while($arr_keys = $redis->hscan('hash', $it)) {
1594
+ foreach($arr_keys as $str_field => $str_value) {
1595
+ echo "$str_field => $str_value\n"; /* Print the hash member and value */
1596
+ }
1597
+ }
1598
+ ~~~
1546
1599
1547
1600
## Lists
1548
1601
@@ -1981,6 +2034,7 @@ $redis->lSize('key1');/* 2 */
1981
2034
* [ sRem, sRemove] ( #srem-sremove ) - Remove one or more members from a set
1982
2035
* [ sUnion] ( #sunion ) - Add multiple sets
1983
2036
* [ sUnionStore] ( #sunionstore ) - Add multiple sets and store the resulting set in a key
2037
+ * [ sScan] ( #sscan ) - Scan a set for members
1984
2038
1985
2039
### sAdd
1986
2040
-----
@@ -2380,6 +2434,41 @@ array(4) {
2380
2434
}
2381
2435
~~~
2382
2436
2437
+ ### sScan
2438
+ -----
2439
+ _ ** Description** _ : Scan a set for members
2440
+
2441
+ ##### * Parameters*
2442
+ * Key* : The set to search
2443
+ * iterator* : LONG (reference) to the iterator as we go
2444
+ * pattern* : String, optional pattern to match against
2445
+ * count* : How many members to return at a time (Redis might return a different amount)
2446
+
2447
+ ##### * Retur value*
2448
+ * Array, boolean* : PHPRedis will return an array of keys or FALSE when we're done iterating
2449
+
2450
+ ##### * Example*
2451
+ ~~~
2452
+ $it = NULL;
2453
+ $redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY); /* don't return empty results until we're done */
2454
+ while($arr_mems = $redis->sscan('set', $it, "*pattern*")) {
2455
+ foreach($arr_mems as $str_mem) {
2456
+ echo "Member: $str_mem\n";
2457
+ }
2458
+ }
2459
+
2460
+ $it = NULL;
2461
+ $redis->setOption(Redis::OPT_SCAN, Redis::SCAN_NORETRY); /* return after each iteration, even if empty */
2462
+ while(($arr_mems = $redis->sscan('set', $it, "*pattern*"))!==FALSE) {
2463
+ if(count($arr_mems) > 0) {
2464
+ foreach($arr_mems as $str_mem) {
2465
+ echo "Member found: $str_mem\n";
2466
+ }
2467
+ } else {
2468
+ echo "No members in this iteration, iterator value: $it\n");
2469
+ }
2470
+ }
2471
+ ~~~
2383
2472
2384
2473
## Sorted sets
2385
2474
@@ -2397,6 +2486,7 @@ array(4) {
2397
2486
* [ zRevRange] ( #zrevrange ) - Return a range of members in a sorted set, by index, with scores ordered from high to low
2398
2487
* [ zScore] ( #zscore ) - Get the score associated with the given member in a sorted set
2399
2488
* [ zUnion] ( #zunion ) - Add multiple sorted sets and store the resulting sorted set in a new key
2489
+ * [ zScan] ( #zscan ) - Scan a sorted set for members
2400
2490
2401
2491
### zAdd
2402
2492
-----
@@ -2736,6 +2826,30 @@ $redis->zUnion('ko2', array('k1', 'k2'), array(1, 1)); /* 4, 'ko2' => array('val
2736
2826
$redis->zUnion('ko3', array('k1', 'k2'), array(5, 1)); /* 4, 'ko3' => array('val0', 'val2', 'val3', 'val1') */
2737
2827
~~~
2738
2828
2829
+ ### zScan
2830
+ -----
2831
+ _ ** Description* _ : Scan a sorted set for members, with optional pattern and count
2832
+
2833
+ ##### * Parameters*
2834
+ * key* : String, the set to scan
2835
+ * iterator* : Long (reference), initialized to NULL
2836
+ * pattern* : String (optional), the pattern to match
2837
+ * count* : How many keys to return per iteration (Redis might return a different number)
2838
+
2839
+ ##### * Return value*
2840
+ * Array, boolean* PHPReids will return matching keys from Redis, or FALSE when iteration is complete
2841
+
2842
+ ##### * Example*
2843
+ ~~~
2844
+ $it = NULL;
2845
+ $redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
2846
+ while($arr_matches = $redis->zscan('zset', $it, '*pattern*')) {
2847
+ foreach($arr_matches as $str_mem => $f_score) {
2848
+ echo "Key: $str_mem, Score: $f_score\n";
2849
+ }
2850
+ }
2851
+ ~~~
2852
+
2739
2853
## Pub/sub
2740
2854
2741
2855
* [ psubscribe] ( #psubscribe ) - Subscribe to channels by pattern
0 commit comments