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.
This commit is contained in:
Oliver Davies 2020-04-29 22:20:01 +01:00
parent f823514a73
commit d3f962e19c
3 changed files with 31 additions and 12 deletions

View file

@ -6,6 +6,7 @@
"ext-ctype": "*", "ext-ctype": "*",
"ext-iconv": "*", "ext-iconv": "*",
"illuminate/support": "^7.9", "illuminate/support": "^7.9",
"symfony/cache": "5.0.*",
"symfony/console": "5.0.*", "symfony/console": "5.0.*",
"symfony/dotenv": "5.0.*", "symfony/dotenv": "5.0.*",
"symfony/flex": "^1.3.1", "symfony/flex": "^1.3.1",

2
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "6d0b74794bebe7f33633550e9c54acb7", "content-hash": "f4b5fb227f3c56e949f202aaa1cf8000",
"packages": [ "packages": [
{ {
"name": "doctrine/inflector", "name": "doctrine/inflector",

View file

@ -2,6 +2,7 @@
namespace App\Command; namespace App\Command;
use DateInterval;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Symfony\Component\Console\Command\Command; 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\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\HttpClientInterface;
class GetRaffleWinnerCommand extends Command class GetRaffleWinnerCommand extends Command
@ -20,6 +22,11 @@ class GetRaffleWinnerCommand extends Command
*/ */
private $client; private $client;
/**
* @var \Symfony\Contracts\Cache\CacheInterface
*/
private $cache;
/** /**
* All of the RSVPs for this event. * All of the RSVPs for this event.
* *
@ -43,11 +50,13 @@ class GetRaffleWinnerCommand extends Command
public function __construct( public function __construct(
HttpClientInterface $client, HttpClientInterface $client,
CacheInterface $cache,
string $name = null string $name = null
) { ) {
parent::__construct($name); parent::__construct($name);
$this->client = $client; $this->client = $client;
$this->cache = $cache;
$this->rsvps = new Collection(); $this->rsvps = new Collection();
$this->yesRsvps = new Collection(); $this->yesRsvps = new Collection();
} }
@ -94,18 +103,27 @@ class GetRaffleWinnerCommand extends Command
*/ */
private function retrieveRsvps(InputInterface $input): void private function retrieveRsvps(InputInterface $input): void
{ {
$response = $this->client->request( $eventId = $input->getArgument('event_id');
'GET', $rsvps = $this->cache->getItem(sprintf('rsvps.%d', $eventId));
vsprintf(
'https://api.meetup.com/%s/events/%d/rsvps',
[
'php-south-wales',
$input->getArgument('event_id'),
]
)
);
$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 { $this->yesRsvps = $this->rsvps->filter(function (array $rsvp): bool {
return $rsvp['response'] == 'yes'; return $rsvp['response'] == 'yes';