ServerSideScripting.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. // This example will not work with versions of Redis < 2.6.
  12. //
  13. // Additionally to the EVAL command defined in the current development profile, the new
  14. // Predis\Command\ScriptedCommand base class can be used to build an higher abstraction
  15. // for our "scripted" commands so that they will appear just like any other command on
  16. // the client-side. This is a quick example used to implement INCREX.
  17. use Predis\Command\ScriptedCommand;
  18. class IncrementExistingKey extends ScriptedCommand
  19. {
  20. public function getKeysCount()
  21. {
  22. return 1;
  23. }
  24. public function getScript()
  25. {
  26. return
  27. <<<LUA
  28. local cmd = redis.call
  29. if cmd('exists', KEYS[1]) == 1 then
  30. return cmd('incr', KEYS[1])
  31. end
  32. LUA;
  33. }
  34. }
  35. $client = new Predis\Client($single_server, '2.6');
  36. $client->getProfile()->defineCommand('increx', 'IncrementExistingKey');
  37. $client->set('foo', 10);
  38. var_dump($client->increx('foo')); // int(11)
  39. var_dump($client->increx('bar')); // NULL