ServerSideScripting.php 1.2 KB

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