ServerSideScripting.php 939 B

1234567891011121314151617181920212223242526272829303132
  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. protected function keysCount() {
  10. return 1;
  11. }
  12. public function getScript() {
  13. return
  14. <<<LUA
  15. if redis('exists', KEYS[1]) == 1 then
  16. return redis('incr', KEYS[1])
  17. end
  18. LUA;
  19. }
  20. }
  21. $client = new Predis\Client($single_server, 'dev');
  22. $client->getProfile()->defineCommand('increx', 'IncrementExistingKey');
  23. $client->set('foo', 10);
  24. var_dump($client->increx('foo')); // int(11)
  25. var_dump($client->increx('bar')); // NULL