executing_redis_commands.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. $client = new Predis\Client($single_server);
  12. // Plain old SET and GET example...
  13. $client->set('library', 'predis');
  14. $response = $client->get('library');
  15. var_export($response); echo PHP_EOL;
  16. /* OUTPUT: 'predis' */
  17. // Redis has the MSET and MGET commands to set or get multiple keys in one go,
  18. // cases like this Predis accepts arguments for variadic commands both as a list
  19. // of arguments or an array containing all of the keys and/or values.
  20. $mkv = array(
  21. 'uid:0001' => '1st user',
  22. 'uid:0002' => '2nd user',
  23. 'uid:0003' => '3rd user',
  24. );
  25. $client->mset($mkv);
  26. $response = $client->mget(array_keys($mkv));
  27. var_export($response); echo PHP_EOL;
  28. /* OUTPUT:
  29. array (
  30. 0 => '1st user',
  31. 1 => '2nd user',
  32. 2 => '3rd user',
  33. ) */
  34. // Predis can also send "raw" commands to Redis. The difference between sending
  35. // commands to Redis the usual way and the "raw" way is that in the latter case
  36. // their arguments are not filtered nor responses coming from Redis are parsed.
  37. $response = $client->executeRaw(array(
  38. 'MGET', 'uid:0001', 'uid:0002', 'uid:0003',
  39. ));
  40. var_export($response); echo PHP_EOL;
  41. /* OUTPUT:
  42. array (
  43. 0 => '1st user',
  44. 1 => '2nd user',
  45. 2 => '3rd user',
  46. ) */