From d3f962e19c53011308049f5404dba99137caaf13 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Wed, 29 Apr 2020 22:20:01 +0100 Subject: [PATCH] Cache RSVPs from Meetup per event Introduce the Cache component and use a filesystem adapter to cache the results from the query to the Meetup API. This will prevent duplicate API calls being made if we need to run it multiple times in a short space of time - i.e. if we want to pick more than one winner during the same event. Fixes #1. --- composer.json | 1 + composer.lock | 2 +- src/Command/GetRaffleWinnerCommand.php | 40 +++++++++++++++++++------- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/composer.json b/composer.json index 4568ed5..780cae1 100644 --- a/composer.json +++ b/composer.json @@ -6,6 +6,7 @@ "ext-ctype": "*", "ext-iconv": "*", "illuminate/support": "^7.9", + "symfony/cache": "5.0.*", "symfony/console": "5.0.*", "symfony/dotenv": "5.0.*", "symfony/flex": "^1.3.1", diff --git a/composer.lock b/composer.lock index 6348fba..f5997aa 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "6d0b74794bebe7f33633550e9c54acb7", + "content-hash": "f4b5fb227f3c56e949f202aaa1cf8000", "packages": [ { "name": "doctrine/inflector", diff --git a/src/Command/GetRaffleWinnerCommand.php b/src/Command/GetRaffleWinnerCommand.php index d2035b5..524d928 100644 --- a/src/Command/GetRaffleWinnerCommand.php +++ b/src/Command/GetRaffleWinnerCommand.php @@ -2,6 +2,7 @@ namespace App\Command; +use DateInterval; use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Symfony\Component\Console\Command\Command; @@ -9,6 +10,7 @@ use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; +use Symfony\Contracts\Cache\CacheInterface; use Symfony\Contracts\HttpClient\HttpClientInterface; class GetRaffleWinnerCommand extends Command @@ -20,6 +22,11 @@ class GetRaffleWinnerCommand extends Command */ private $client; + /** + * @var \Symfony\Contracts\Cache\CacheInterface + */ + private $cache; + /** * All of the RSVPs for this event. * @@ -43,11 +50,13 @@ class GetRaffleWinnerCommand extends Command public function __construct( HttpClientInterface $client, + CacheInterface $cache, string $name = null ) { parent::__construct($name); $this->client = $client; + $this->cache = $cache; $this->rsvps = new Collection(); $this->yesRsvps = new Collection(); } @@ -94,18 +103,27 @@ class GetRaffleWinnerCommand extends Command */ private function retrieveRsvps(InputInterface $input): void { - $response = $this->client->request( - 'GET', - vsprintf( - 'https://api.meetup.com/%s/events/%d/rsvps', - [ - 'php-south-wales', - $input->getArgument('event_id'), - ] - ) - ); + $eventId = $input->getArgument('event_id'); + $rsvps = $this->cache->getItem(sprintf('rsvps.%d', $eventId)); - $this->rsvps = new Collection($response->toArray()); + if (!$rsvps->isHit()) { + $response = $this->client->request( + 'GET', + vsprintf( + 'https://api.meetup.com/%s/events/%d/rsvps', + [ + 'php-south-wales', + $eventId, + ] + ) + ); + + $rsvps->expiresAfter(DateInterval::createFromDateString('1 hour')); + $this->rsvps = new Collection($response->toArray()); + $this->cache->save($rsvps->set($this->rsvps)); + } else { + $this->rsvps = $rsvps->get(); + } $this->yesRsvps = $this->rsvps->filter(function (array $rsvp): bool { return $rsvp['response'] == 'yes';