diff --git a/bin/queue-status b/bin/queue-status old mode 100644 new mode 100755 diff --git a/bin/worker b/bin/worker old mode 100644 new mode 100755 index fc192ff..daaa44d --- a/bin/worker +++ b/bin/worker @@ -6,7 +6,6 @@ use Benzine\Workers\AbstractQueueWorker; use duncan3dc\Forker\Fork; use Benzine\App; - $args = CommandLine::parseArgs($_SERVER['argv']); if(!isset($args['worker'])){ die("You must pass a --worker= argument with this script\n"); diff --git a/src/App.php b/src/App.php index f6f5272..700d436 100644 --- a/src/App.php +++ b/src/App.php @@ -4,6 +4,7 @@ namespace Benzine; use Benzine\ORM\Connection\Databases; use Benzine\ORM\Laminator; +use Benzine\Redis\Redis; use Benzine\Services\ConfigurationService; use Benzine\Services\EnvironmentService; use Benzine\Services\SessionService; @@ -30,6 +31,7 @@ use Slim; use Slim\Factory\AppFactory; use Twig; use Twig\Loader\FilesystemLoader; +use Benzine\Redis\Lua; class App { @@ -207,14 +209,14 @@ class App return $monolog; }); - //$container->set(DebugBar::class, function (ContainerInterface $container) { - // $debugBar = new StandardDebugBar(); - // /** @var Logger $logger */ - // $logger = $container->get(Logger::class); - // $debugBar->addCollector(new MonologCollector($logger)); -// -// return $debugBar; -// }); + $container->set(DebugBar::class, function (ContainerInterface $container) { + $debugBar = new StandardDebugBar(); + /** @var Logger $logger */ + $logger = $container->get(Logger::class); + $debugBar->addCollector(new MonologCollector($logger)); + + return $debugBar; + }); $container->set(\Middlewares\Debugbar::class, function (ContainerInterface $container) { $debugBar = $container->get(DebugBar::class); @@ -225,12 +227,13 @@ class App $container->set(\Redis::class, function (ContainerInterface $container) { $environmentService = $container->get(EnvironmentService::class); - $redis = new \Redis(); + $redis = new Redis(); $redis->connect( $environmentService->get('REDIS_HOST', 'redis'), $environmentService->get('REDIS_PORT', 6379) ); + return $redis; }); diff --git a/src/Redis/Lua/LuaExtension.php b/src/Redis/Lua/LuaExtension.php new file mode 100644 index 0000000..04f3512 --- /dev/null +++ b/src/Redis/Lua/LuaExtension.php @@ -0,0 +1,40 @@ +redis = $redis; + + $this->load(); + } + + public function getFunctionNames() : array { + $name = explode("\\", get_called_class()); + return [ + end($name) + ]; + } + + public function getHash() : string { + return $this->hash; + } + + protected function load(){ + if(!$this->hash){ + $exists = $this->redis->script('exists', $this->getScript()); + if(!$exists[0]){ + $this->hash = $this->redis->script('load', $this->getScript()); + } + } + #printf("Loaded \"%s\" as \"%s\"\n", $this->getFunctionNames()[0], $this->hash); + } + + abstract protected function getScript() : string; + +} \ No newline at end of file diff --git a/src/Redis/Lua/SetIfHigher.php b/src/Redis/Lua/SetIfHigher.php new file mode 100644 index 0000000..2faf3a7 --- /dev/null +++ b/src/Redis/Lua/SetIfHigher.php @@ -0,0 +1,23 @@ + c then + redis.call('set', KEYS[1], ARGV[1]) + return tonumber(ARGV[1]) - c + else + return 0 + end + else + return redis.call('set', KEYS[1], ARGV[1]) + end + LUA; + } +} diff --git a/src/Redis/Redis.php b/src/Redis/Redis.php new file mode 100644 index 0000000..2cfced4 --- /dev/null +++ b/src/Redis/Redis.php @@ -0,0 +1,32 @@ +scripts[] = new Lua\SetIfHigher($this); + } + + public function connect($host, $port = 6379, $timeout = 0.0, $reserved = null, $retryInterval = 0, $readTimeout = 0.0) + { + parent::connect($host, $port, $timeout, $reserved, $retryInterval, $readTimeout); + $this->initialiseExtensions(); + } + + public function __call($name, $arguments) + { + foreach($this->scripts as $script){ + foreach($script->getFunctionNames() as $functionName){ + if(strtolower($name) == strtolower($functionName)){ + return $this->evalSha($script->getHash(), $arguments); + } + } + } + } +} \ No newline at end of file