Browse Source

Better caching

(cherry picked from commit cbf942c48e17e13d6f64468bdbd88dba5918c5d7)
imwald
Nuša Pukšič 5 months ago
parent
commit
21a88b4433
  1. 3
      src/Controller/AuthorController.php
  2. 66
      src/Service/CacheService.php
  3. 11
      src/Service/MetadataRetrievalException.php
  4. 14
      src/Service/NostrClient.php
  5. 2
      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 @@ -52,7 +52,8 @@ class AuthorController extends AbstractController
return $this->render('pages/author.html.twig', [
'author' => $author,
'npub' => $npub,
'articles' => $articles
'articles' => $articles,
'is_author_profile' => true,
]);
}

66
src/Service/CacheService.php

@ -0,0 +1,66 @@ @@ -0,0 +1,66 @@
<?php
namespace App\Service;
use Psr\Cache\InvalidArgumentException;
use Psr\Log\LoggerInterface;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
readonly class CacheService
{
public function __construct(
private NostrClient $nostrClient,
private CacheInterface $cache,
private LoggerInterface $logger
)
{
}
/**
* @param string $npub
* @return \stdClass
*/
public function getMetadata(string $npub): \stdClass
{
$cacheKey = '0_' . $npub;
try {
return $this->cache->get($cacheKey, function (ItemInterface $item) use ($npub, $cacheKey) {
$item->expiresAfter(3600); // 1 hour, adjust as needed
try {
$meta = $this->nostrClient->getNpubMetadata($npub);
$this->logger->info('Metadata:', ['meta' => json_encode($meta)]);
return json_decode($meta->content);
} catch (\Exception $e) {
$this->logger->error('Error getting user data.', ['exception' => $e]);
throw new MetadataRetrievalException('Failed to retrieve metadata', 0, $e);
}
});
} catch (\Exception|InvalidArgumentException $e) {
$this->logger->error('Error getting user data.', ['exception' => $e]);
$content = new \stdClass();
$content->name = substr($npub, 0, 8) . '…' . substr($npub, -4);
return $content;
}
}
public function getRelays($npub)
{
$cacheKey = '3_' . $npub;
try {
return $this->cache->get($cacheKey, function (ItemInterface $item) use ($npub) {
$item->expiresAfter(3600); // 1 hour
try {
return $this->nostrClient->getNpubRelays($npub);
} catch (\Exception $e) {
$this->logger->error('Error getting relays.', ['exception' => $e]);
return [];
}
});
} catch (InvalidArgumentException $e) {
$this->logger->error('Error getting relay data.', ['exception' => $e]);
return [];
}
}
}

11
src/Service/MetadataRetrievalException.php

@ -0,0 +1,11 @@ @@ -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

@ -98,13 +98,19 @@ class NostrClient @@ -98,13 +98,19 @@ class NostrClient
return $reputableAuthorRelays;
}
/**
* @throws \Exception
*/
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]);
// Npubs are converted to hex for the request down the line
$request = $this->createNostrRequest(
kinds: [KindsEnum::METADATA],
filters: ['authors' => [$npub]]
filters: ['authors' => [$npub]],
relaySet: $relaySet
);
$events = $this->processResponse($request->send(), function($received) {
@ -115,11 +121,7 @@ class NostrClient @@ -115,11 +121,7 @@ class NostrClient
$this->logger->info('Getting metadata for npub', ['response' => $events]);
if (empty($events)) {
$meta = new \stdClass();
$content = new \stdClass();
$content->name = substr($npub, 0, 8) . '…' . substr($npub, -4);
$meta->content = json_encode($content);
return $meta;
throw new \Exception('No metadata found for npub: ' . $npub);
}
// Sort by date and return newest

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

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

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

@ -1,7 +1,8 @@ @@ -1,7 +1,8 @@
<div {{ attributes }}>
{% set is_author_profile = is_author_profile|default(false) %}
{% for item in list %}
{% 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 %}
{% endfor %}
</div>

Loading…
Cancel
Save