Browse Source

Better caching

imwald
Nuša Pukšič 9 months ago
parent
commit
cbf942c48e
  1. 3
      src/Controller/AuthorController.php
  2. 15
      src/Service/CacheService.php
  3. 11
      src/Service/MetadataRetrievalException.php
  4. 14
      src/Service/NostrClient.php
  5. 4
      templates/components/Molecules/Card.html.twig
  6. 3
      templates/components/Organisms/CardList.html.twig

3
src/Controller/AuthorController.php

@ -52,7 +52,8 @@ class AuthorController extends AbstractController
return $this->render('pages/author.html.twig', [ return $this->render('pages/author.html.twig', [
'author' => $author, 'author' => $author,
'npub' => $npub, 'npub' => $npub,
'articles' => $articles 'articles' => $articles,
'is_author_profile' => true,
]); ]);
} }

15
src/Service/CacheService.php

@ -26,21 +26,18 @@ readonly class CacheService
{ {
$cacheKey = '0_' . $npub; $cacheKey = '0_' . $npub;
try { try {
return $this->cache->get($cacheKey, function (ItemInterface $item) use ($npub) { return $this->cache->get($cacheKey, function (ItemInterface $item) use ($npub, $cacheKey) {
$item->expiresAfter(3600); // 1 hour, adjust as needed $item->expiresAfter(3600); // 1 hour, adjust as needed
try { try {
$meta = $this->nostrClient->getNpubMetadata($npub); $meta = $this->nostrClient->getNpubMetadata($npub);
$this->logger->info('Metadata:', ['meta' => json_encode($meta)]);
return json_decode($meta->content);
} catch (\Exception $e) { } catch (\Exception $e) {
$this->logger->error('Error getting user data.', ['exception' => $e]); $this->logger->error('Error getting user data.', ['exception' => $e]);
$meta = new \stdClass(); throw new MetadataRetrievalException('Failed to retrieve metadata', 0, $e);
$content = new \stdClass();
$meta->name = substr($npub, 0, 8) . '…' . substr($npub, -4);
$meta->content = json_encode($content);
} }
$this->logger->info('Metadata:', ['meta' => json_encode($meta)]);
return json_decode($meta->content);
}); });
} catch (InvalidArgumentException $e) { } catch (\Exception|InvalidArgumentException $e) {
$this->logger->error('Error getting user data.', ['exception' => $e]); $this->logger->error('Error getting user data.', ['exception' => $e]);
$content = new \stdClass(); $content = new \stdClass();
$content->name = substr($npub, 0, 8) . '…' . substr($npub, -4); $content->name = substr($npub, 0, 8) . '…' . substr($npub, -4);
@ -55,7 +52,7 @@ readonly class CacheService
return $this->cache->get($cacheKey, function (ItemInterface $item) use ($npub) { return $this->cache->get($cacheKey, function (ItemInterface $item) use ($npub) {
$item->expiresAfter(3600); // 1 hour $item->expiresAfter(3600); // 1 hour
try { try {
return $this->nostrClient->getRelaysForNpub($npub); return $this->nostrClient->getNpubRelays($npub);
} catch (\Exception $e) { } catch (\Exception $e) {
$this->logger->error('Error getting relays.', ['exception' => $e]); $this->logger->error('Error getting relays.', ['exception' => $e]);
return []; return [];

11
src/Service/MetadataRetrievalException.php

@ -0,0 +1,11 @@
<?php
namespace App\Service;
class MetadataRetrievalException extends \Exception
{
public function __construct(string $message = "Failed to retrieve metadata", int $code = 0, ?\Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
}

14
src/Service/NostrClient.php

@ -72,13 +72,19 @@ class NostrClient
return array_slice($authorRelays, 0, $limit); return array_slice($authorRelays, 0, $limit);
} }
/**
* @throws \Exception
*/
public function getNpubMetadata($npub): \stdClass public function getNpubMetadata($npub): \stdClass
{ {
$relaySet = $this->defaultRelaySet;
$relaySet->addRelay(new Relay('wss://profiles.nostr1.com')); // profile aggregator
$this->logger->info('Getting metadata for npub', ['npub' => $npub]); $this->logger->info('Getting metadata for npub', ['npub' => $npub]);
// Npubs are converted to hex for the request down the line // Npubs are converted to hex for the request down the line
$request = $this->createNostrRequest( $request = $this->createNostrRequest(
kinds: [KindsEnum::METADATA], kinds: [KindsEnum::METADATA],
filters: ['authors' => [$npub]] filters: ['authors' => [$npub]],
relaySet: $relaySet
); );
$events = $this->processResponse($request->send(), function($received) { $events = $this->processResponse($request->send(), function($received) {
@ -89,11 +95,7 @@ class NostrClient
$this->logger->info('Getting metadata for npub', ['response' => $events]); $this->logger->info('Getting metadata for npub', ['response' => $events]);
if (empty($events)) { if (empty($events)) {
$meta = new \stdClass(); throw new \Exception('No metadata found for npub: ' . $npub);
$content = new \stdClass();
$content->name = substr($npub, 0, 8) . '…' . substr($npub, -4);
$meta->content = json_encode($content);
return $meta;
} }
// Sort by date and return newest // Sort by date and return newest

4
templates/components/Molecules/Card.html.twig

@ -4,7 +4,9 @@
{% if category %} {% if category %}
<small>{{ category }}</small> <small>{{ category }}</small>
{% else %} {% else %}
<p>by <twig:Molecules:UserFromNpub ident="{{ article.pubkey }}" /></p> {% if not is_author_profile %}
<p>by <twig:Molecules:UserFromNpub ident="{{ article.pubkey }}" /></p>
{% endif %}
<small>{{ article.createdAt|date('F j Y') }}</small> <small>{{ article.createdAt|date('F j Y') }}</small>
{% endif %} {% endif %}
</div> </div>

3
templates/components/Organisms/CardList.html.twig

@ -1,7 +1,8 @@
<div {{ attributes }}> <div {{ attributes }}>
{% set is_author_profile = is_author_profile|default(false) %}
{% for item in list %} {% for item in list %}
{% if item.slug is not empty and item.title is not empty %} {% if item.slug is not empty and item.title is not empty %}
<twig:Molecules:Card :article="item"></twig:Molecules:Card> <twig:Molecules:Card :article="item" :is_author_profile="is_author_profile"></twig:Molecules:Card>
{% endif %} {% endif %}
{% endfor %} {% endfor %}
</div> </div>

Loading…
Cancel
Save