RedisCollectionsIterators.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\KeyspaceIterator;
  12. use Predis\Collection\Iterator\SetIterator;
  13. use Predis\Collection\Iterator\SortedSetIterator;
  14. use Predis\Collection\Iterator\HashIterator;
  15. // Redis 2.8 features new commands allowing clients to incrementally
  16. // iterate over collections without blocking the server like it happens
  17. // when a command such as KEYS is executed on a Redis instance storing
  18. // millions of keys. These commands are SCAN (iterates over the keyspace),
  19. // SSCAN (iterates over members of a set), ZSCAN (iterates over members
  20. // and ranks of a sorted set) and HSCAN (iterates over fields and values
  21. // of an hash). Predis provides a specialized abstraction for each command
  22. // based on SPL iterators making it possible to easily consume SCAN-based
  23. // iterations in your PHP code.
  24. //
  25. // See http://redis.io/commands/scan for more details.
  26. //
  27. // Create a client using `2.8` as a server profile (needs Redis 2.8!)
  28. $client = new Predis\Client($single_server, array('profile' => '2.8'));
  29. // Prepare some keys for our example
  30. $client->del('predis:set', 'predis:zset', 'predis:hash');
  31. for ($i = 0; $i < 5; $i++) {
  32. $client->sadd('predis:set', "member:$i");
  33. $client->zadd('predis:zset', -$i, "member:$i");
  34. $client->hset('predis:hash', "field:$i", "value:$i");
  35. }
  36. // === KeyspaceIterator based on SCAN ===
  37. echo 'Scan the keyspace matching only our prefixed keys:' . PHP_EOL;
  38. foreach (new KeyspaceIterator($client, 'predis:*') as $key) {
  39. echo " - $key" . PHP_EOL;
  40. }
  41. /* OUTPUT
  42. Scan the keyspace matching only our prefixed keys:
  43. - predis:zset
  44. - predis:set
  45. - predis:hash
  46. */
  47. // === SetIterator class based on SSCAN ===
  48. echo 'Scan members of `predis:set`:' . PHP_EOL;
  49. foreach (new SetIterator($client, 'predis:set') as $member) {
  50. echo " - $member" . PHP_EOL;
  51. }
  52. /* OUTPUT
  53. Scan members of `predis:set`:
  54. - member:1
  55. - member:4
  56. - member:0
  57. - member:3
  58. - member:2
  59. */
  60. // === SortedSetIterator class based on ZSCAN ===
  61. echo 'Scan members and ranks of `predis:zset`:' . PHP_EOL;
  62. foreach (new SortedSetIterator($client, 'predis:zset') as $member => $rank) {
  63. echo " - $member [rank: $rank]" . PHP_EOL;
  64. }
  65. /* OUTPUT
  66. Scan members and ranks of `predis:zset`:
  67. - member:4 [rank: -4]
  68. - member:3 [rank: -3]
  69. - member:2 [rank: -2]
  70. - member:1 [rank: -1]
  71. - member:0 [rank: 0]
  72. */
  73. // === HashIterator class based on HSCAN ===
  74. echo 'Scan fields and values of `predis:hash`:' . PHP_EOL;
  75. foreach (new HashIterator($client, 'predis:hash') as $field => $value) {
  76. echo " - $field => $value" . PHP_EOL;
  77. }
  78. /* OUTPUT
  79. Scan fields and values of `predis:hash`:
  80. - field:0 => value:0
  81. - field:1 => value:1
  82. - field:2 => value:2
  83. - field:3 => value:3
  84. - field:4 => value:4
  85. */