SendingRedisCommands.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. $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->raw(array('MGET', 'uid:0001', 'uid:0002', 'uid:0003'));
  38. var_export($response); echo PHP_EOL;
  39. /* OUTPUT:
  40. array (
  41. 0 => '1st user',
  42. 1 => '2nd user',
  43. 2 => '3rd user',
  44. ) */