Create a cached talk counter

Create a cached version of the talk counter service that returns a
cached result of the talk count for that day.

This uses the Decorator design pattern to decorate the existing
`TalkCounter` service and works as they both implement the same
`TalkCounterInterface`.
This commit is contained in:
Oliver Davies 2024-05-11 15:32:07 +02:00
parent 6d2a12c2de
commit 0c91825c16
5 changed files with 58 additions and 7 deletions

View file

@ -10,6 +10,7 @@ declare(strict_types=1);
use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Render\BubbleableMetadata; use Drupal\Core\Render\BubbleableMetadata;
use Drupal\opdavies_talks\Service\TalkCounter; use Drupal\opdavies_talks\Service\TalkCounter;
use Drupal\opdavies_talks\Service\TalkCounterInterface;
use Drupal\opdavies_talks\Service\TalkDateUpdater; use Drupal\opdavies_talks\Service\TalkDateUpdater;
/** /**
@ -57,8 +58,8 @@ function opdavies_talks_tokens(string $type, array $tokens, array $data, array $
$replacements = []; $replacements = [];
if ($type == 'opdavies_talks') { if ($type == 'opdavies_talks') {
/** @var TalkCounter $talkCounter */ /** @var TalkCounterInterface $talkCounter */
$talkCounter = Drupal::service(TalkCounter::class); $talkCounter = Drupal::service(TalkCounterInterface::class);
foreach ($tokens as $name => $original) { foreach ($tokens as $name => $original) {
switch ($name) { switch ($name) {

View file

@ -1,10 +1,21 @@
services: services:
Drupal\Core\Cache\CacheBackendInterface:
alias: cache.default
private: true
Drupal\opdavies_talks\Repository\TalkRepository: Drupal\opdavies_talks\Repository\TalkRepository:
autowire: true autowire: true
Drupal\opdavies_talks\Service\CachedTalkCounter:
autowire: true
Drupal\opdavies_talks\Service\TalkCounter: Drupal\opdavies_talks\Service\TalkCounter:
autowire: true autowire: true
Drupal\opdavies_talks\Service\TalkCounterInterface:
autowire: true
class: Drupal\opdavies_talks\Service\CachedTalkCounter
Drupal\opdavies_talks\Service\TalkDateUpdater: Drupal\opdavies_talks\Service\TalkDateUpdater:
autowire: true autowire: true

View file

@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace Drupal\opdavies_talks\Service;
use Drupal\Core\Cache\CacheBackendInterface;
final class CachedTalkCounter {
public function __construct(
private TalkCounter $talkCounter,
private CacheBackendInterface $cache,
) {}
public function getCount(): int {
if ($cacheData = $this->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;
}
}

View file

@ -10,11 +10,7 @@ use Drupal\paragraphs\ParagraphInterface;
final class TalkCounter { final class TalkCounter {
private TalkRepository $talkRepository; public function __construct(private TalkRepository $talkRepository) {}
public function __construct(TalkRepository $talkRepository) {
$this->talkRepository = $talkRepository;
}
public function getCount(): int { public function getCount(): int {
$today = Carbon::today()->format('Y-m-d H:i:s'); $today = Carbon::today()->format('Y-m-d H:i:s');

View file

@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace Drupal\opdavies_talks\Service;
interface TalkCounterInterface {
public function getCount(): int;
}