RedisCollectionsIterators.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. require 'SharedConfigurations.php';
  11. use Predis\Collection\Iterator;
  12. // Redis 2.8 features new commands allowing clients to incrementally
  13. // iterate over collections without blocking the server like it happens
  14. // when a command such as KEYS is executed on a Redis instance storing
  15. // millions of keys. These commands are SCAN (iterates over the keyspace),
  16. // SSCAN (iterates over members of a set), ZSCAN (iterates over members
  17. // and ranks of a sorted set) and HSCAN (iterates over fields and values
  18. // of an hash). Predis provides a specialized abstraction for each command
  19. // based on SPL iterators making it possible to easily consume SCAN-based
  20. // iterations in your PHP code.
  21. //
  22. // See http://redis.io/commands/scan for more details.
  23. //
  24. // Create a client using `2.8` as a server profile (needs Redis 2.8!)
  25. $client = new Predis\Client($single_server, array('profile' => '2.8'));
  26. // Prepare some keys for our example
  27. $client->del('predis:set', 'predis:zset', 'predis:hash');
  28. for ($i = 0; $i < 5; $i++) {
  29. $client->sadd('predis:set', "member:$i");
  30. $client->zadd('predis:zset', -$i, "member:$i");
  31. $client->hset('predis:hash', "field:$i", "value:$i");
  32. }
  33. // === Keyspace iterator based on SCAN ===
  34. echo 'Scan the keyspace matching only our prefixed keys:', PHP_EOL;
  35. foreach (new Iterator\Keyspace($client, 'predis:*') as $key) {
  36. echo " - $key", PHP_EOL;
  37. }
  38. /* OUTPUT
  39. Scan the keyspace matching only our prefixed keys:
  40. - predis:zset
  41. - predis:set
  42. - predis:hash
  43. */
  44. // === Set iterator based on SSCAN ===
  45. echo 'Scan members of `predis:set`:', PHP_EOL;
  46. foreach (new Iterator\SetKey($client, 'predis:set') as $member) {
  47. echo " - $member", PHP_EOL;
  48. }
  49. /* OUTPUT
  50. Scan members of `predis:set`:
  51. - member:1
  52. - member:4
  53. - member:0
  54. - member:3
  55. - member:2
  56. */
  57. // === Sorted set iterator based on ZSCAN ===
  58. echo 'Scan members and ranks of `predis:zset`:', PHP_EOL;
  59. foreach (new Iterator\SortedSetKey($client, 'predis:zset') as $member => $rank) {
  60. echo " - $member [rank: $rank]", PHP_EOL;
  61. }
  62. /* OUTPUT
  63. Scan members and ranks of `predis:zset`:
  64. - member:4 [rank: -4]
  65. - member:3 [rank: -3]
  66. - member:2 [rank: -2]
  67. - member:1 [rank: -1]
  68. - member:0 [rank: 0]
  69. */
  70. // === Hash iterator based on HSCAN ===
  71. echo 'Scan fields and values of `predis:hash`:', PHP_EOL;
  72. foreach (new Iterator\HashKey($client, 'predis:hash') as $field => $value) {
  73. echo " - $field => $value", PHP_EOL;
  74. }
  75. /* OUTPUT
  76. Scan fields and values of `predis:hash`:
  77. - field:0 => value:0
  78. - field:1 => value:1
  79. - field:2 => value:2
  80. - field:3 => value:3
  81. - field:4 => value:4
  82. */