redis_collections_iterators.php 2.7 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 __DIR__.'/shared.php';
  11. use Predis\Collection\Iterator;
  12. // Starting from 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. // Create a client using `2.8` as a server profile (needs Redis 2.8!)
  27. $client = new Predis\Client($single_server, array('profile' => '2.8'));
  28. // Prepare some keys for our example
  29. $client->del('predis:set', 'predis:zset', 'predis:hash');
  30. for ($i = 0; $i < 5; ++$i) {
  31. $client->sadd('predis:set', "member:$i");
  32. $client->zadd('predis:zset', -$i, "member:$i");
  33. $client->hset('predis:hash', "field:$i", "value:$i");
  34. }
  35. // === Keyspace iterator based on SCAN ===
  36. echo 'Scan the keyspace matching only our prefixed keys:', PHP_EOL;
  37. foreach (new Iterator\Keyspace($client, 'predis:*') as $key) {
  38. echo " - $key", PHP_EOL;
  39. }
  40. /* OUTPUT
  41. Scan the keyspace matching only our prefixed keys:
  42. - predis:zset
  43. - predis:set
  44. - predis:hash
  45. */
  46. // === Set iterator based on SSCAN ===
  47. echo 'Scan members of `predis:set`:', PHP_EOL;
  48. foreach (new Iterator\SetKey($client, 'predis:set') as $member) {
  49. echo " - $member", PHP_EOL;
  50. }
  51. /* OUTPUT
  52. Scan members of `predis:set`:
  53. - member:1
  54. - member:4
  55. - member:0
  56. - member:3
  57. - member:2
  58. */
  59. // === Sorted set iterator based on ZSCAN ===
  60. echo 'Scan members and ranks of `predis:zset`:', PHP_EOL;
  61. foreach (new Iterator\SortedSetKey($client, 'predis:zset') as $member => $rank) {
  62. echo " - $member [rank: $rank]", PHP_EOL;
  63. }
  64. /* OUTPUT
  65. Scan members and ranks of `predis:zset`:
  66. - member:4 [rank: -4]
  67. - member:3 [rank: -3]
  68. - member:2 [rank: -2]
  69. - member:1 [rank: -1]
  70. - member:0 [rank: 0]
  71. */
  72. // === Hash iterator based on HSCAN ===
  73. echo 'Scan fields and values of `predis:hash`:', PHP_EOL;
  74. foreach (new Iterator\HashKey($client, 'predis:hash') as $field => $value) {
  75. echo " - $field => $value", PHP_EOL;
  76. }
  77. /* OUTPUT
  78. Scan fields and values of `predis:hash`:
  79. - field:0 => value:0
  80. - field:1 => value:1
  81. - field:2 => value:2
  82. - field:3 => value:3
  83. - field:4 => value:4
  84. */