From 56e4905635321a47803a8c140491791542daab4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nu=C5=A1a=20Puk=C5=A1i=C4=8D?= Date: Sat, 22 Mar 2025 21:02:17 +0100 Subject: [PATCH] Optimize page load --- .env | 9 + composer.json | 6 +- composer.lock | 312 +++++++++++++++++- src/Controller/DefaultController.php | 16 +- .../Components/Molecules/UserFromNpub.php | 16 +- symfony.lock | 3 + 6 files changed, 344 insertions(+), 18 deletions(-) diff --git a/.env b/.env index 74b141d..4bcb2ff 100644 --- a/.env +++ b/.env @@ -48,3 +48,12 @@ ELASTICSEARCH_PORT=9200 ELASTICSEARCH_USERNAME=elastic ELASTICSEARCH_PASSWORD=your_password ###< elastic ### +###> redis ### +REDIS_HOST=localhost +REDIS_PASSWORD=r_password +###< redis ### +###> LNBits ### +LNBITS_URL=https://legend.lnbits.com +LNBITS_API_KEY= +###< LNBits ### + diff --git a/composer.json b/composer.json index 4197a17..4b57e7f 100644 --- a/composer.json +++ b/composer.json @@ -10,10 +10,13 @@ "ext-ctype": "*", "ext-iconv": "*", "ext-openssl": "*", + "ext-redis": "*", "doctrine/dbal": "^4.2", "doctrine/doctrine-bundle": "^2.13", "doctrine/doctrine-migrations-bundle": "^3.3", "doctrine/orm": "^3.3", + "endroid/qr-code": "^6.0", + "endroid/qr-code-bundle": "^6.0", "friendsofsymfony/elastica-bundle": "^6.5", "league/commonmark": "^2.6", "league/html-to-markdown": "*", @@ -51,7 +54,8 @@ "allow-plugins": { "php-http/discovery": true, "symfony/flex": true, - "symfony/runtime": true + "symfony/runtime": true, + "endroid/installer": true }, "sort-packages": true }, diff --git a/composer.lock b/composer.lock index 4f6ad99..b7b0e3f 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,62 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "5cfb4f5535c24bf4cbec6db7c20c469e", + "content-hash": "8df0c7139f6ed328ea1abb8d715e68db", "packages": [ + { + "name": "bacon/bacon-qr-code", + "version": "v3.0.1", + "source": { + "type": "git", + "url": "https://github.com/Bacon/BaconQrCode.git", + "reference": "f9cc1f52b5a463062251d666761178dbdb6b544f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Bacon/BaconQrCode/zipball/f9cc1f52b5a463062251d666761178dbdb6b544f", + "reference": "f9cc1f52b5a463062251d666761178dbdb6b544f", + "shasum": "" + }, + "require": { + "dasprid/enum": "^1.0.3", + "ext-iconv": "*", + "php": "^8.1" + }, + "require-dev": { + "phly/keep-a-changelog": "^2.12", + "phpunit/phpunit": "^10.5.11 || 11.0.4", + "spatie/phpunit-snapshot-assertions": "^5.1.5", + "squizlabs/php_codesniffer": "^3.9" + }, + "suggest": { + "ext-imagick": "to generate QR code images" + }, + "type": "library", + "autoload": { + "psr-4": { + "BaconQrCode\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-2-Clause" + ], + "authors": [ + { + "name": "Ben Scholzen 'DASPRiD'", + "email": "mail@dasprids.de", + "homepage": "https://dasprids.de/", + "role": "Developer" + } + ], + "description": "BaconQrCode is a QR code generator for PHP.", + "homepage": "https://github.com/Bacon/BaconQrCode", + "support": { + "issues": "https://github.com/Bacon/BaconQrCode/issues", + "source": "https://github.com/Bacon/BaconQrCode/tree/v3.0.1" + }, + "time": "2024-10-01T13:55:55+00:00" + }, { "name": "bitwasp/bech32", "version": "v0.0.1", @@ -133,6 +187,56 @@ ], "time": "2024-09-19T14:15:21+00:00" }, + { + "name": "dasprid/enum", + "version": "1.0.6", + "source": { + "type": "git", + "url": "https://github.com/DASPRiD/Enum.git", + "reference": "8dfd07c6d2cf31c8da90c53b83c026c7696dda90" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/DASPRiD/Enum/zipball/8dfd07c6d2cf31c8da90c53b83c026c7696dda90", + "reference": "8dfd07c6d2cf31c8da90c53b83c026c7696dda90", + "shasum": "" + }, + "require": { + "php": ">=7.1 <9.0" + }, + "require-dev": { + "phpunit/phpunit": "^7 || ^8 || ^9 || ^10 || ^11", + "squizlabs/php_codesniffer": "*" + }, + "type": "library", + "autoload": { + "psr-4": { + "DASPRiD\\Enum\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-2-Clause" + ], + "authors": [ + { + "name": "Ben Scholzen 'DASPRiD'", + "email": "mail@dasprids.de", + "homepage": "https://dasprids.de/", + "role": "Developer" + } + ], + "description": "PHP 7.1 enum implementation", + "keywords": [ + "enum", + "map" + ], + "support": { + "issues": "https://github.com/DASPRiD/Enum/issues", + "source": "https://github.com/DASPRiD/Enum/tree/1.0.6" + }, + "time": "2024-08-09T14:30:48+00:00" + }, { "name": "dflydev/dot-access-data", "version": "v3.0.3", @@ -1484,6 +1588,209 @@ }, "time": "2023-04-21T15:31:12+00:00" }, + { + "name": "endroid/installer", + "version": "1.5.0", + "source": { + "type": "git", + "url": "https://github.com/endroid/installer.git", + "reference": "39109e825057b2e7436123d5ad5dc8c2467a4cf6" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/endroid/installer/zipball/39109e825057b2e7436123d5ad5dc8c2467a4cf6", + "reference": "39109e825057b2e7436123d5ad5dc8c2467a4cf6", + "shasum": "" + }, + "require": { + "composer-plugin-api": "^2.0", + "php": "^8.2" + }, + "require-dev": { + "composer/composer": "^2.0", + "endroid/quality": "dev-main" + }, + "suggest": { + "roave/security-advisories": "Avoids installation of package versions with vulnerabilities" + }, + "type": "composer-plugin", + "extra": { + "class": "Endroid\\Installer\\Installer", + "branch-alias": { + "dev-main": "1.x-dev" + } + }, + "autoload": { + "psr-4": { + "Endroid\\Installer\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jeroen van den Enden", + "email": "info@endroid.nl" + } + ], + "description": "Composer plugin for installing configuration files", + "support": { + "issues": "https://github.com/endroid/installer/issues", + "source": "https://github.com/endroid/installer/tree/1.5.0" + }, + "funding": [ + { + "url": "https://github.com/endroid", + "type": "github" + } + ], + "time": "2024-10-20T18:31:36+00:00" + }, + { + "name": "endroid/qr-code", + "version": "6.0.6", + "source": { + "type": "git", + "url": "https://github.com/endroid/qr-code.git", + "reference": "11e6a94458dab8dd18736c11892130ec788b5028" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/endroid/qr-code/zipball/11e6a94458dab8dd18736c11892130ec788b5028", + "reference": "11e6a94458dab8dd18736c11892130ec788b5028", + "shasum": "" + }, + "require": { + "bacon/bacon-qr-code": "^3.0", + "php": "^8.2" + }, + "require-dev": { + "endroid/quality": "dev-main", + "ext-gd": "*", + "khanamiryan/qrcode-detector-decoder": "^2.0.2", + "setasign/fpdf": "^1.8.2" + }, + "suggest": { + "ext-gd": "Enables you to write PNG images", + "khanamiryan/qrcode-detector-decoder": "Enables you to use the image validator", + "roave/security-advisories": "Makes sure package versions with known security issues are not installed", + "setasign/fpdf": "Enables you to use the PDF writer" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "6.x-dev" + } + }, + "autoload": { + "psr-4": { + "Endroid\\QrCode\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jeroen van den Enden", + "email": "info@endroid.nl" + } + ], + "description": "Endroid QR Code", + "homepage": "https://github.com/endroid/qr-code", + "keywords": [ + "code", + "endroid", + "php", + "qr", + "qrcode" + ], + "support": { + "issues": "https://github.com/endroid/qr-code/issues", + "source": "https://github.com/endroid/qr-code/tree/6.0.6" + }, + "funding": [ + { + "url": "https://github.com/endroid", + "type": "github" + } + ], + "time": "2025-03-14T23:29:08+00:00" + }, + { + "name": "endroid/qr-code-bundle", + "version": "6.0.0", + "source": { + "type": "git", + "url": "https://github.com/endroid/qr-code-bundle.git", + "reference": "b1e6972cf7ec154f646f0b0d9d12ca901bc0b9d1" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/endroid/qr-code-bundle/zipball/b1e6972cf7ec154f646f0b0d9d12ca901bc0b9d1", + "reference": "b1e6972cf7ec154f646f0b0d9d12ca901bc0b9d1", + "shasum": "" + }, + "require": { + "endroid/installer": "^1.2.2", + "endroid/qr-code": "^6.0.1", + "php": "^8.2", + "symfony/framework-bundle": "^5.4||^6.4||^7.0", + "symfony/twig-bundle": "^5.4||^6.4||^7.0", + "symfony/yaml": "^5.4||^6.4||^7.0" + }, + "require-dev": { + "endroid/quality": "dev-main" + }, + "suggest": { + "roave/security-advisories": "Avoids installation of package versions with vulnerabilities" + }, + "type": "symfony-bundle", + "extra": { + "branch-alias": { + "dev-main": "6.x-dev" + } + }, + "autoload": { + "psr-4": { + "Endroid\\QrCodeBundle\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jeroen van den Enden", + "email": "info@endroid.nl" + } + ], + "description": "Endroid QR Code Bundle", + "homepage": "https://github.com/endroid/qr-code-bundle", + "keywords": [ + "bundle", + "code", + "endroid", + "php", + "qr", + "symfony" + ], + "support": { + "issues": "https://github.com/endroid/qr-code-bundle/issues", + "source": "https://github.com/endroid/qr-code-bundle/tree/6.0.0" + }, + "funding": [ + { + "url": "https://github.com/endroid", + "type": "github" + } + ], + "time": "2024-10-21T23:18:20+00:00" + }, { "name": "ezimuel/guzzlestreams", "version": "3.1.0", @@ -11513,7 +11820,8 @@ "php": ">=8.3.13", "ext-ctype": "*", "ext-iconv": "*", - "ext-openssl": "*" + "ext-openssl": "*", + "ext-redis": "*" }, "platform-dev": {}, "plugin-api-version": "2.6.0" diff --git a/src/Controller/DefaultController.php b/src/Controller/DefaultController.php index 8e1f3b0..effd91d 100644 --- a/src/Controller/DefaultController.php +++ b/src/Controller/DefaultController.php @@ -6,7 +6,6 @@ namespace App\Controller; use App\Entity\Article; use App\Enum\IndexStatusEnum; -use App\Service\NostrClient; use Doctrine\ORM\EntityManagerInterface; use FOS\ElasticaBundle\Finder\FinderInterface; use Psr\Cache\InvalidArgumentException; @@ -20,8 +19,7 @@ class DefaultController extends AbstractController { public function __construct( private readonly FinderInterface $esFinder, - private readonly EntityManagerInterface $entityManager, - private readonly NostrClient $nostrClient) + private readonly EntityManagerInterface $entityManager) { } @@ -31,7 +29,7 @@ class DefaultController extends AbstractController #[Route('/', name: 'default')] public function index(): Response { - $list = $this->entityManager->getRepository(Article::class)->findBy(['indexStatus' => IndexStatusEnum::INDEXED], ['createdAt' => 'DESC'], 5); + $list = $this->entityManager->getRepository(Article::class)->findBy(['indexStatus' => IndexStatusEnum::INDEXED], ['createdAt' => 'DESC'], 10); // deduplicate by slugs $deduplicated = []; @@ -41,14 +39,10 @@ class DefaultController extends AbstractController } } - $npubs = array_map(function($obj) { - return $obj->getPubkey(); - }, $list); - - // $this->nostrClient->getMetadata(array_unique($npubs)); - return $this->render('home.html.twig', [ - 'list' => array_values($deduplicated) + 'list' => array_values(array_filter($deduplicated, function($item) { + return !empty($item->getImage()); + })) ]); } diff --git a/src/Twig/Components/Molecules/UserFromNpub.php b/src/Twig/Components/Molecules/UserFromNpub.php index 97862c3..6433ea3 100644 --- a/src/Twig/Components/Molecules/UserFromNpub.php +++ b/src/Twig/Components/Molecules/UserFromNpub.php @@ -4,6 +4,7 @@ namespace App\Twig\Components\Molecules; use App\Service\NostrClient; use Psr\Cache\InvalidArgumentException; +use Symfony\Contracts\Cache\CacheInterface; use Symfony\UX\TwigComponent\Attribute\AsTwigComponent; #[AsTwigComponent] @@ -12,17 +13,24 @@ final class UserFromNpub public string $npub; public ?array $user = null; - public function __construct(private readonly NostrClient $nostrClient) + public function __construct(private readonly NostrClient $nostrClient, private readonly CacheInterface $redisCache) { } public function mount(string $npub): void { $this->npub = $npub; + try { - $meta = $this->nostrClient->getNpubMetadata($npub); - $this->user = (array) json_decode($meta->content); - } catch (InvalidArgumentException|\Exception) { + $this->user = $this->redisCache->get('user_' . $npub, function () use ($npub) { + try { + $meta = $this->nostrClient->getNpubMetadata($npub); + return (array) json_decode($meta->content); + } catch (InvalidArgumentException|\Exception) { + return null; + } + }); + } catch (InvalidArgumentException $e) { $this->user = null; } } diff --git a/symfony.lock b/symfony.lock index cb73f23..b93e9b6 100644 --- a/symfony.lock +++ b/symfony.lock @@ -26,6 +26,9 @@ "./migrations/.gitignore" ] }, + "endroid/qr-code-bundle": { + "version": "6.0.0" + }, "friendsofsymfony/elastica-bundle": { "version": "6.5", "recipe": {