mirror of
https://github.com/glowingblue/flarum-ext-redis-setup.git
synced 2026-03-22 15:07:53 +01:00
Conventions & code formating
This commit is contained in:
@@ -11,106 +11,106 @@ use Illuminate\Support\Arr;
|
||||
|
||||
class EnableRedis implements ExtenderInterface
|
||||
{
|
||||
const CACHE_KEY = 'connections.cache';
|
||||
const QUEUE_KEY = 'connections.queue';
|
||||
const SESSION_KEY = 'connections.session';
|
||||
|
||||
public function extend(Container $container, Extension $extension = null)
|
||||
{
|
||||
$config = $this->buildConfig();
|
||||
const CACHE_KEY = 'connections.cache';
|
||||
const QUEUE_KEY = 'connections.queue';
|
||||
const SESSION_KEY = 'connections.session';
|
||||
|
||||
(new Redis($config))
|
||||
->disable($this->getDisabledServices())
|
||||
->extend($container, $extension);
|
||||
}
|
||||
public function extend(Container $container, Extension $extension = null)
|
||||
{
|
||||
$config = $this->buildConfig();
|
||||
|
||||
private function getDisabledServices(): array
|
||||
{
|
||||
/** @var SettingsRepositoryInterface */
|
||||
$settings = resolve(SettingsRepositoryInterface::class);
|
||||
(new Redis($config))
|
||||
->disable($this->getDisabledServices())
|
||||
->extend($container, $extension);
|
||||
}
|
||||
|
||||
$disabled = [];
|
||||
private function getDisabledServices(): array
|
||||
{
|
||||
/** @var SettingsRepositoryInterface */
|
||||
$settings = resolve(SettingsRepositoryInterface::class);
|
||||
|
||||
if (!(bool) $settings->get('glowingblue-redis.enableCache', false)) {
|
||||
$disabled[] = 'cache';
|
||||
}
|
||||
$disabled = [];
|
||||
|
||||
if (!(bool) $settings->get('glowingblue-redis.enableQueue', false)) {
|
||||
$disabled[] = 'queue';
|
||||
}
|
||||
if (!(bool) $settings->get('glowingblue-redis.enableCache', false)) {
|
||||
$disabled[] = 'cache';
|
||||
}
|
||||
|
||||
if (!(bool) $settings->get('glowingblue-redis.redisSessions', false)) {
|
||||
$disabled[] = 'session';
|
||||
}
|
||||
if (!(bool) $settings->get('glowingblue-redis.enableQueue', false)) {
|
||||
$disabled[] = 'queue';
|
||||
}
|
||||
|
||||
return $disabled;
|
||||
}
|
||||
if (!(bool) $settings->get('glowingblue-redis.redisSessions', false)) {
|
||||
$disabled[] = 'session';
|
||||
}
|
||||
|
||||
private function buildConfig($config = []): array
|
||||
{
|
||||
$cache = [
|
||||
'host' => $this->getHost(),
|
||||
'password' => $this->getPassword(),
|
||||
'port' => $this->getPort(),
|
||||
'database' => $this->getCacheDatabase(),
|
||||
'prefix' => $this->getPrefix(),
|
||||
];
|
||||
return $disabled;
|
||||
}
|
||||
|
||||
$queue = [
|
||||
'host' => $this->getHost(),
|
||||
'password' => $this->getPassword(),
|
||||
'port' => $this->getPort(),
|
||||
'database' => $this->getQueueDatabase(),
|
||||
'prefix' => $this->getPrefix(),
|
||||
];
|
||||
private function buildConfig($config = []): array
|
||||
{
|
||||
$cache = [
|
||||
'host' => $this->getHost(),
|
||||
'password' => $this->getPassword(),
|
||||
'port' => $this->getPort(),
|
||||
'database' => $this->getCacheDatabase(),
|
||||
'prefix' => $this->getPrefix(),
|
||||
];
|
||||
|
||||
$session = [
|
||||
'host' => $this->getHost(),
|
||||
'password' => $this->getPassword(),
|
||||
'port' => $this->getPort(),
|
||||
'database' => $this->getSessionDatabase(),
|
||||
'prefix' => $this->getPrefix(),
|
||||
];
|
||||
$queue = [
|
||||
'host' => $this->getHost(),
|
||||
'password' => $this->getPassword(),
|
||||
'port' => $this->getPort(),
|
||||
'database' => $this->getQueueDatabase(),
|
||||
'prefix' => $this->getPrefix(),
|
||||
];
|
||||
|
||||
$config = Arr::add($config, self::CACHE_KEY, $cache);
|
||||
$config = Arr::add($config, self::QUEUE_KEY, $queue);
|
||||
$config = Arr::add($config, self::SESSION_KEY, $session);
|
||||
$session = [
|
||||
'host' => $this->getHost(),
|
||||
'password' => $this->getPassword(),
|
||||
'port' => $this->getPort(),
|
||||
'database' => $this->getSessionDatabase(),
|
||||
'prefix' => $this->getPrefix(),
|
||||
];
|
||||
|
||||
return $config;
|
||||
}
|
||||
$config = Arr::add($config, self::CACHE_KEY, $cache);
|
||||
$config = Arr::add($config, self::QUEUE_KEY, $queue);
|
||||
$config = Arr::add($config, self::SESSION_KEY, $session);
|
||||
|
||||
private function getHost(): string
|
||||
{
|
||||
return getenv('REDIS_HOST') ? getenv('REDIS_HOST') : '127.0.0.1';
|
||||
}
|
||||
return $config;
|
||||
}
|
||||
|
||||
private function getPassword(): ?string
|
||||
{
|
||||
return getenv('REDIS_PASSWORD') ? getenv('REDIS_PASSWORD') : null;
|
||||
}
|
||||
private function getHost(): string
|
||||
{
|
||||
return getenv('REDIS_HOST') ? getenv('REDIS_HOST') : '127.0.0.1';
|
||||
}
|
||||
|
||||
private function getPort(): string
|
||||
{
|
||||
return getenv('REDIS_PORT') ? getenv('REDIS_PORT') : '6379';
|
||||
}
|
||||
private function getPassword(): ?string
|
||||
{
|
||||
return getenv('REDIS_PASSWORD') ? getenv('REDIS_PASSWORD') : null;
|
||||
}
|
||||
|
||||
private function getCacheDatabase(): int
|
||||
{
|
||||
return (int) getenv('REDIS_DATABASE_CACHE') ? getenv('REDIS_DATABASE_CACHE') : 1;
|
||||
}
|
||||
private function getPort(): string
|
||||
{
|
||||
return getenv('REDIS_PORT') ? getenv('REDIS_PORT') : '6379';
|
||||
}
|
||||
|
||||
private function getQueueDatabase(): int
|
||||
{
|
||||
return (int) getenv('REDIS_DATABASE_QUEUE') ? getenv('REDIS_DATABASE_QUEUE') : 2;
|
||||
}
|
||||
private function getCacheDatabase(): int
|
||||
{
|
||||
return (int) getenv('REDIS_DATABASE_CACHE') ? getenv('REDIS_DATABASE_CACHE') : 1;
|
||||
}
|
||||
|
||||
private function getSessionDatabase(): int
|
||||
{
|
||||
return (int) getenv('REDIS_DATABASE_SESSION') ? getenv('REDIS_DATABASE_SESSION') : 3;
|
||||
}
|
||||
private function getQueueDatabase(): int
|
||||
{
|
||||
return (int) getenv('REDIS_DATABASE_QUEUE') ? getenv('REDIS_DATABASE_QUEUE') : 2;
|
||||
}
|
||||
|
||||
private function getPrefix(): string
|
||||
{
|
||||
return getenv('REDIS_PREFIX') ? getenv('REDIS_PREFIX') : 'flarum_';
|
||||
}
|
||||
private function getSessionDatabase(): int
|
||||
{
|
||||
return (int) getenv('REDIS_DATABASE_SESSION') ? getenv('REDIS_DATABASE_SESSION') : 3;
|
||||
}
|
||||
|
||||
private function getPrefix(): string
|
||||
{
|
||||
return getenv('REDIS_PREFIX') ? getenv('REDIS_PREFIX') : 'flarum_';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,59 +15,59 @@ use Illuminate\Contracts\Queue\Queue as QueueContract;
|
||||
|
||||
class QueueProvider extends AbstractServiceProvider
|
||||
{
|
||||
private $connection = 'default';
|
||||
private $connection = 'default';
|
||||
|
||||
public function boot()
|
||||
{
|
||||
/** @var SettingsRepositoryInterface */
|
||||
$settings = resolve(SettingsRepositoryInterface::class);
|
||||
public function boot()
|
||||
{
|
||||
/** @var SettingsRepositoryInterface */
|
||||
$settings = resolve(SettingsRepositoryInterface::class);
|
||||
|
||||
if (!(bool) $settings->get('glowingblue-redis.enableQueue', false)) {
|
||||
return;
|
||||
}
|
||||
if (!(bool) $settings->get('glowingblue-redis.enableQueue', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var ExtensionManager $extensions */
|
||||
$extensions = resolve(ExtensionManager::class);
|
||||
/** @var ExtensionManager $extensions */
|
||||
$extensions = resolve(ExtensionManager::class);
|
||||
|
||||
(new Frontend('admin'))
|
||||
->content([$this, 'adminWidgetAttributes'])
|
||||
->extend($this->container, $extensions->getExtension('glowingblue-redis-setup'));
|
||||
(new Frontend('admin'))
|
||||
->content([$this, 'adminWidgetAttributes'])
|
||||
->extend($this->container, $extensions->getExtension('glowingblue-redis-setup'));
|
||||
|
||||
/** @var Dispatcher $dispatcher */
|
||||
$dispatcher = $this->container->make(Dispatcher::class);
|
||||
$dispatcher->listen(Looping::class, [$this, 'trackQueues']);
|
||||
}
|
||||
/** @var Dispatcher $dispatcher */
|
||||
$dispatcher = $this->container->make(Dispatcher::class);
|
||||
$dispatcher->listen(Looping::class, [$this, 'trackQueues']);
|
||||
}
|
||||
|
||||
public function adminWidgetAttributes(Document $document)
|
||||
{
|
||||
/** @var Store $cache */
|
||||
$cache = resolve('cache.store');
|
||||
/** @var QueueContract $queue */
|
||||
$queue = resolve(QueueContract::class);
|
||||
public function adminWidgetAttributes(Document $document)
|
||||
{
|
||||
/** @var Store $cache */
|
||||
$cache = resolve('cache.store');
|
||||
/** @var QueueContract $queue */
|
||||
$queue = resolve(QueueContract::class);
|
||||
|
||||
$queues = $cache->get('blomstra.queue.queues-seen') ?? [];
|
||||
$queues = $cache->get('blomstra.queue.queues-seen') ?? [];
|
||||
|
||||
if ($queue instanceof RedisQueue) {
|
||||
$load = [];
|
||||
if ($queue instanceof RedisQueue) {
|
||||
$load = [];
|
||||
|
||||
foreach ($queues as $name) {
|
||||
$load[$name] = $queue->getRedis()
|
||||
->connection($this->connection)
|
||||
->llen('queues:' . $name);
|
||||
}
|
||||
}
|
||||
foreach ($queues as $name) {
|
||||
$load[$name] = $queue->getRedis()
|
||||
->connection($this->connection)
|
||||
->llen('queues:' . $name);
|
||||
}
|
||||
}
|
||||
|
||||
$document->payload['blomstraQueuesSeen'] = $queues;
|
||||
$document->payload['blomstraQueuesLoad'] = $load ?? null;
|
||||
}
|
||||
$document->payload['blomstraQueuesSeen'] = $queues;
|
||||
$document->payload['blomstraQueuesLoad'] = $load ?? null;
|
||||
}
|
||||
|
||||
public function trackQueues(Looping $event)
|
||||
{
|
||||
/** @var Store $cache */
|
||||
$cache = resolve('cache.store');
|
||||
public function trackQueues(Looping $event)
|
||||
{
|
||||
/** @var Store $cache */
|
||||
$cache = resolve('cache.store');
|
||||
|
||||
$queues = $cache->get('blomstra.queue.queues-seen') ?? [];
|
||||
$queues = array_merge($queues, (array) explode(',', $event->queue));
|
||||
$cache->put('blomstra.queue.queues-seen', array_unique($queues), 60);
|
||||
}
|
||||
$queues = $cache->get('blomstra.queue.queues-seen') ?? [];
|
||||
$queues = array_merge($queues, (array) explode(',', $event->queue));
|
||||
$cache->put('blomstra.queue.queues-seen', array_unique($queues), 60);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user