HIncrByFloatIfExists.php 867 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Redis\Lua\Hash;
  12. use Hyperf\Redis\Lua\Script;
  13. class HIncrByFloatIfExists extends Script
  14. {
  15. public function getScript(): string
  16. {
  17. return <<<'LUA'
  18. if(redis.call('type', KEYS[1]).ok == 'hash') then
  19. return redis.call('HINCRBYFLOAT', KEYS[1], ARGV[1], ARGV[2]);
  20. end
  21. return "";
  22. LUA;
  23. }
  24. /**
  25. * @param null|float $data
  26. */
  27. public function format($data): ?float
  28. {
  29. if (is_numeric($data)) {
  30. return (float) $data;
  31. }
  32. return null;
  33. }
  34. protected function getKeyNumber(array $arguments): int
  35. {
  36. return 1;
  37. }
  38. }