ZaddCommand.php 881 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace Limen\Redisun\Commands;
  3. class ZaddCommand extends Command
  4. {
  5. public function getScript()
  6. {
  7. $luaSetTtl = $this->luaSetTtl($this->getTtl());
  8. $setTtl = $luaSetTtl ? 1 : 0;
  9. $checkScript = $this->existenceScript;
  10. $delScript = $this->deleteScript;
  11. $script = <<<LUA
  12. $checkScript
  13. local values = {};
  14. local setTtl = '$setTtl';
  15. for i,v in ipairs(KEYS) do
  16. local ttl = redis.call('ttl', v)
  17. $delScript
  18. local rs1
  19. local j=1
  20. while j<#ARGV do
  21. rs1=redis.call('zadd',v,ARGV[j],ARGV[j+1]);
  22. j=j+2
  23. end
  24. if rs1 then
  25. if setTtl=='1' then
  26. $luaSetTtl
  27. elseif ttl > 0 then
  28. redis.call('expire', v, ttl)
  29. end
  30. values[#values+1] = rs1;
  31. else
  32. values[#values+1] = nil;
  33. end
  34. end
  35. return {KEYS,values};
  36. LUA;
  37. return $script;
  38. }
  39. }