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:
parent
f823514a73
commit
d3f962e19c
|
@ -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",
|
||||
|
|
2
composer.lock
generated
2
composer.lock
generated
|
@ -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",
|
||||
|
|
|
@ -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';
|
||||
|
|
Loading…
Reference in a new issue