diff --git a/src/Redis/Lua/LuaExtension.php b/src/Redis/Lua/LuaExtension.php index 04f3512..dd951a6 100644 --- a/src/Redis/Lua/LuaExtension.php +++ b/src/Redis/Lua/LuaExtension.php @@ -10,8 +10,6 @@ abstract class LuaExtension { public function __construct(\Redis $redis) { $this->redis = $redis; - - $this->load(); } public function getFunctionNames() : array { @@ -25,7 +23,7 @@ abstract class LuaExtension { return $this->hash; } - protected function load(){ + public function load(){ if(!$this->hash){ $exists = $this->redis->script('exists', $this->getScript()); if(!$exists[0]){ diff --git a/src/Redis/Lua/SetIfLower.php b/src/Redis/Lua/SetIfLower.php new file mode 100644 index 0000000..4a5631e --- /dev/null +++ b/src/Redis/Lua/SetIfLower.php @@ -0,0 +1,23 @@ + c then + redis.call('zadd', KEYS[1], KEYS[2], ARGV[1]) + return tonumber(KEYS[2]) - c + else + return 0 + end + else + redis.call('zadd', KEYS[1], KEYS[2], ARGV[1]) + return 'OK' + end + LUA; + } +} diff --git a/src/Redis/Lua/ZAddIfLower.php b/src/Redis/Lua/ZAddIfLower.php new file mode 100644 index 0000000..c7f126d --- /dev/null +++ b/src/Redis/Lua/ZAddIfLower.php @@ -0,0 +1,24 @@ +scripts[] = new Lua\SetIfHigher($this); + $this->scripts[] = new Lua\SetIfLower($this); + $this->scripts[] = new Lua\ZAddIfHigher($this); + $this->scripts[] = new Lua\ZAddIfLower($this); } public function connect($host, $port = 6379, $timeout = 0.0, $reserved = null, $retryInterval = 0, $readTimeout = 0.0) @@ -24,6 +27,7 @@ class Redis extends \Redis foreach($this->scripts as $script){ foreach($script->getFunctionNames() as $functionName){ if(strtolower($name) == strtolower($functionName)){ + $script->load(); return $this->evalSha($script->getHash(), $arguments); } }