Redis wrapper and lua extension loader.
This commit is contained in:
parent
4dd66a67c5
commit
a5129c629c
6 changed files with 107 additions and 10 deletions
0
bin/queue-status
Normal file → Executable file
0
bin/queue-status
Normal file → Executable file
1
bin/worker
Normal file → Executable file
1
bin/worker
Normal file → Executable file
|
|
@ -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");
|
||||
|
|
|
|||
21
src/App.php
21
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;
|
||||
});
|
||||
|
||||
|
|
|
|||
40
src/Redis/Lua/LuaExtension.php
Normal file
40
src/Redis/Lua/LuaExtension.php
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace Benzine\Redis\Lua;
|
||||
|
||||
abstract class LuaExtension {
|
||||
|
||||
protected \Redis $redis;
|
||||
protected ?string $hash = null;
|
||||
|
||||
public function __construct(\Redis $redis)
|
||||
{
|
||||
$this->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;
|
||||
|
||||
}
|
||||
23
src/Redis/Lua/SetIfHigher.php
Normal file
23
src/Redis/Lua/SetIfHigher.php
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
namespace Benzine\Redis\Lua;
|
||||
|
||||
class SetIfHigher extends LuaExtension
|
||||
{
|
||||
protected function getScript() : string
|
||||
{
|
||||
return <<<'LUA'
|
||||
local c = tonumber(redis.call('get', KEYS[1]));
|
||||
if c then
|
||||
if tonumber(ARGV[1]) > 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;
|
||||
}
|
||||
}
|
||||
32
src/Redis/Redis.php
Normal file
32
src/Redis/Redis.php
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
namespace Benzine\Redis;
|
||||
use Benzine\Redis\Lua;
|
||||
|
||||
class Redis extends \Redis
|
||||
{
|
||||
/** @var Lua\LuaExtension[] */
|
||||
private array $scripts;
|
||||
|
||||
public function initialiseExtensions() : void
|
||||
{
|
||||
$this->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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue