ServerSideScripting.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. local cmd = redis.call
  27. if cmd('exists', KEYS[1]) == 1 then
  28. return cmd('incr', KEYS[1])
  29. end
  30. LUA;
  31. }
  32. }
  33. $client = new Predis\Client($single_server, 'dev');
  34. $client->getProfile()->defineCommand('increx', 'IncrementExistingKey');
  35. $client->set('foo', 10);
  36. var_dump($client->increx('foo')); // int(11)
  37. var_dump($client->increx('bar')); // NULL