From 269b9b27c26ca63da5835f82bb4fee619701cf50 Mon Sep 17 00:00:00 2001 From: Binyamin Yawitz <316103+byawitz@users.noreply.github.com> Date: Thu, 6 Jun 2024 20:54:08 -0400 Subject: [PATCH] feat: Adding cache --- app/init2.php | 102 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 61 insertions(+), 41 deletions(-) diff --git a/app/init2.php b/app/init2.php index bccee1b107..74b5bd2c83 100644 --- a/app/init2.php +++ b/app/init2.php @@ -3,22 +3,10 @@ require_once __DIR__ . '/../vendor/autoload.php'; use PHPMailer\PHPMailer\PHPMailer; +use Redis; +use Utopia\Cache\Adapter\Redis as CacheRedis; +use Utopia\Cache\Adapter\Sharding; use Utopia\VCS\Adapter\Git\GitHub; - -require_once __DIR__ . '/init/constants.php'; -require_once __DIR__ . '/init/config.php'; -require_once __DIR__ . '/init/locale.php'; -require_once __DIR__ . '/init/database/filters.php'; -require_once __DIR__ . '/init/database/formats.php'; - -ini_set('memory_limit', '-1'); -ini_set('display_errors', 1); -ini_set('display_startup_errors', 1); -ini_set('default_socket_timeout', -1); -error_reporting(E_ALL); - -global $http, $container; - use Ahc\Jwt\JWT; use Ahc\Jwt\JWTException; use Appwrite\Auth\Auth; @@ -45,7 +33,6 @@ use Swoole\Database\PDOConfig; use Swoole\Database\PDOPool; use Swoole\Database\RedisConfig; use Swoole\Database\RedisPool; -use Utopia\Cache\Adapter\None; use Utopia\Cache\Cache; use Utopia\CLI\Console; use Utopia\Config\Config; @@ -80,6 +67,20 @@ use Utopia\Storage\Device\Wasabi; use Utopia\Storage\Storage; use Utopia\System\System; +require_once __DIR__ . '/init/constants.php'; +require_once __DIR__ . '/init/config.php'; +require_once __DIR__ . '/init/locale.php'; +require_once __DIR__ . '/init/database/filters.php'; +require_once __DIR__ . '/init/database/formats.php'; + +ini_set('memory_limit', '-1'); +ini_set('display_errors', 1); +ini_set('display_startup_errors', 1); +ini_set('default_socket_timeout', -1); +error_reporting(E_ALL); + +global $http, $container; + Http::setMode(System::getEnv('_APP_ENV', Http::MODE_TYPE_PRODUCTION)); if (!Http::isProduction()) { @@ -206,20 +207,20 @@ $global->set( 'pools', (function () { $fallbackForDB = 'db_main=' . URL::unparse([ - 'scheme' => 'mariadb', - 'host' => System::getEnv('_APP_DB_HOST', 'mariadb'), - 'port' => System::getEnv('_APP_DB_PORT', '3306'), - 'user' => System::getEnv('_APP_DB_USER', ''), - 'pass' => System::getEnv('_APP_DB_PASS', ''), - 'path' => System::getEnv('_APP_DB_SCHEMA', ''), - ]); + 'scheme' => 'mariadb', + 'host' => System::getEnv('_APP_DB_HOST', 'mariadb'), + 'port' => System::getEnv('_APP_DB_PORT', '3306'), + 'user' => System::getEnv('_APP_DB_USER', ''), + 'pass' => System::getEnv('_APP_DB_PASS', ''), + 'path' => System::getEnv('_APP_DB_SCHEMA', ''), + ]); $fallbackForRedis = 'redis_main=' . URL::unparse([ - 'scheme' => 'redis', - 'host' => System::getEnv('_APP_REDIS_HOST', 'redis'), - 'port' => System::getEnv('_APP_REDIS_PORT', '6379'), - 'user' => System::getEnv('_APP_REDIS_USER', ''), - 'pass' => System::getEnv('_APP_REDIS_PASS', ''), - ]); + 'scheme' => 'redis', + 'host' => System::getEnv('_APP_REDIS_HOST', 'redis'), + 'port' => System::getEnv('_APP_REDIS_PORT', '6379'), + 'user' => System::getEnv('_APP_REDIS_USER', ''), + 'pass' => System::getEnv('_APP_REDIS_PASS', ''), + ]); $connections = [ 'console' => [ @@ -323,7 +324,8 @@ $global->set( (new RedisConfig()) ->withHost($dsnHost) ->withPort((int)$dsnPort) - ->withAuth($dsnPass), $poolSize + ->withAuth($dsnPass), + $poolSize ); break; @@ -428,7 +430,7 @@ $queueForCertificates = new Dependency(); $plan ->setName('plan') - ->setCallback(fn() => []); + ->setCallback(fn () => []); $mode ->setName('mode') @@ -637,7 +639,7 @@ $project return $console; } - $project = $authorization->skip(fn() => $dbForConsole->getDocument('projects', $projectId)); + $project = $authorization->skip(fn () => $dbForConsole->getDocument('projects', $projectId)); return $project; }); @@ -741,8 +743,26 @@ $dbForConsole $cache ->setName('cache') - ->setCallback(function (): Cache { - return new Cache(new None()); + ->inject('pools') + ->inject('connections') + ->setCallback(function (array $pools, Connections $connections) { + $adapters = []; + $databases = Config::getParam('pools-cache'); + + foreach ($databases as $database) { + $pool = $pools['pools-cache-' . $database]['pool']; + $dsn = $pools['pools-cache-' . $database]['dsn']; + + $connection = $pool->get(); + $connections->add($connection, $pool); + + $redis = new Redis(); + $redis->connect($dsn->getHost(), $dsn->getPort()); + + $adapters[] = new CacheRedis($redis); + } + + return new Cache(new Sharding($adapters)); }); $authorization @@ -785,11 +805,11 @@ $connections $locale ->setName('locale') - ->setCallback(fn() => new Locale(System::getEnv('_APP_LOCALE', 'en'))); + ->setCallback(fn () => new Locale(System::getEnv('_APP_LOCALE', 'en'))); $localeCodes ->setName('localeCodes') - ->setCallback(fn() => array_map(fn($locale) => $locale['code'], Config::getParam('locale-codes', []))); + ->setCallback(fn () => array_map(fn ($locale) => $locale['code'], Config::getParam('locale-codes', []))); $queue ->setName('queue') @@ -941,10 +961,10 @@ $clients * + Filter for duplicated entries */ $clientsConsole = \array_map( - fn($node) => $node['hostname'], + fn ($node) => $node['hostname'], \array_filter( $console->getAttribute('platforms', []), - fn($node) => (isset($node['type']) && ($node['type'] === Origin::CLIENT_TYPE_WEB) && isset($node['hostname']) && !empty($node['hostname'])) + fn ($node) => (isset($node['type']) && ($node['type'] === Origin::CLIENT_TYPE_WEB) && isset($node['hostname']) && !empty($node['hostname'])) ) ); @@ -952,10 +972,10 @@ $clients \array_merge( $clientsConsole, \array_map( - fn($node) => $node['hostname'], + fn ($node) => $node['hostname'], \array_filter( $project->getAttribute('platforms', []), - fn($node) => (isset($node['type']) && ($node['type'] === Origin::CLIENT_TYPE_WEB || $node['type'] === Origin::CLIENT_TYPE_FLUTTER_WEB) && isset($node['hostname']) && !empty($node['hostname'])) + fn ($node) => (isset($node['type']) && ($node['type'] === Origin::CLIENT_TYPE_WEB || $node['type'] === Origin::CLIENT_TYPE_FLUTTER_WEB) && isset($node['hostname']) && !empty($node['hostname'])) ) ) ) @@ -1118,7 +1138,7 @@ $schema }; $attributes = function (int $limit, int $offset) use ($dbForProject, $authorization) { - $attrs = $authorization->skip(fn() => $dbForProject->find('attributes', [ + $attrs = $authorization->skip(fn () => $dbForProject->find('attributes', [ Query::limit($limit), Query::offset($offset), ]));