diff --git a/web/modules/custom/talks/opdavies_talks.module b/web/modules/custom/talks/opdavies_talks.module index d28e2ec..dfd90b7 100644 --- a/web/modules/custom/talks/opdavies_talks.module +++ b/web/modules/custom/talks/opdavies_talks.module @@ -10,6 +10,7 @@ declare(strict_types=1); use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Render\BubbleableMetadata; use Drupal\opdavies_talks\Service\TalkCounter; +use Drupal\opdavies_talks\Service\TalkCounterInterface; use Drupal\opdavies_talks\Service\TalkDateUpdater; /** @@ -57,8 +58,8 @@ function opdavies_talks_tokens(string $type, array $tokens, array $data, array $ $replacements = []; if ($type == 'opdavies_talks') { - /** @var TalkCounter $talkCounter */ - $talkCounter = Drupal::service(TalkCounter::class); + /** @var TalkCounterInterface $talkCounter */ + $talkCounter = Drupal::service(TalkCounterInterface::class); foreach ($tokens as $name => $original) { switch ($name) { diff --git a/web/modules/custom/talks/opdavies_talks.services.yml b/web/modules/custom/talks/opdavies_talks.services.yml index 2cd3e9a..4e541e0 100644 --- a/web/modules/custom/talks/opdavies_talks.services.yml +++ b/web/modules/custom/talks/opdavies_talks.services.yml @@ -1,10 +1,21 @@ services: + Drupal\Core\Cache\CacheBackendInterface: + alias: cache.default + private: true + Drupal\opdavies_talks\Repository\TalkRepository: autowire: true + Drupal\opdavies_talks\Service\CachedTalkCounter: + autowire: true + Drupal\opdavies_talks\Service\TalkCounter: autowire: true + Drupal\opdavies_talks\Service\TalkCounterInterface: + autowire: true + class: Drupal\opdavies_talks\Service\CachedTalkCounter + Drupal\opdavies_talks\Service\TalkDateUpdater: autowire: true diff --git a/web/modules/custom/talks/src/Service/CachedTalkCounter.php b/web/modules/custom/talks/src/Service/CachedTalkCounter.php new file mode 100644 index 0000000..acc845e --- /dev/null +++ b/web/modules/custom/talks/src/Service/CachedTalkCounter.php @@ -0,0 +1,32 @@ +cache->get(cid: 'talk_count')) { + return $cacheData->data; + } + + $count = $this->talkCounter->getCount(); + + $this->cache->set( + cid: 'talk_count', + data: $count, + expire: strtotime('tomorrow'), + ); + + return $count; + } + +} diff --git a/web/modules/custom/talks/src/Service/TalkCounter.php b/web/modules/custom/talks/src/Service/TalkCounter.php index 4d132b0..12d9a0c 100644 --- a/web/modules/custom/talks/src/Service/TalkCounter.php +++ b/web/modules/custom/talks/src/Service/TalkCounter.php @@ -10,11 +10,7 @@ use Drupal\paragraphs\ParagraphInterface; final class TalkCounter { - private TalkRepository $talkRepository; - - public function __construct(TalkRepository $talkRepository) { - $this->talkRepository = $talkRepository; - } + public function __construct(private TalkRepository $talkRepository) {} public function getCount(): int { $today = Carbon::today()->format('Y-m-d H:i:s'); diff --git a/web/modules/custom/talks/src/Service/TalkCounterInterface.php b/web/modules/custom/talks/src/Service/TalkCounterInterface.php new file mode 100644 index 0000000..403a3fb --- /dev/null +++ b/web/modules/custom/talks/src/Service/TalkCounterInterface.php @@ -0,0 +1,11 @@ +