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:
parent
6d2a12c2de
commit
0c91825c16
|
@ -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) {
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
32
web/modules/custom/talks/src/Service/CachedTalkCounter.php
Normal file
32
web/modules/custom/talks/src/Service/CachedTalkCounter.php
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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');
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Drupal\opdavies_talks\Service;
|
||||||
|
|
||||||
|
interface TalkCounterInterface {
|
||||||
|
|
||||||
|
public function getCount(): int;
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue