redis_collections_iterators.php 2.7 KB

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