ServerSideScripting.php 949 B

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