From d196e35deb35a82aaa2ec7eef5b9a9389054b55b Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Wed, 13 Feb 2019 22:30:09 +0000 Subject: [PATCH] Cache the event data from joind.in --- src/Command/PickWinnerCommand.php | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/Command/PickWinnerCommand.php b/src/Command/PickWinnerCommand.php index 64bdad2..e6e750b 100644 --- a/src/Command/PickWinnerCommand.php +++ b/src/Command/PickWinnerCommand.php @@ -3,6 +3,7 @@ namespace App\Command; use GuzzleHttp\Client; +use Symfony\Component\Cache\Simple\FilesystemCache; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -14,12 +15,15 @@ class PickWinnerCommand extends Command { protected static $defaultName = 'app:pick-winner'; - private $guzzle; + /** + * @var \GuzzleHttp\Client + */ + private $client; public function __construct() { parent::__construct(); - $this->guzzle = new Client(); + $this->client = new Client(); } protected function configure() @@ -47,5 +51,20 @@ class PickWinnerCommand extends Command ]); // $io->success('You have a new command! Now make it your own! Pass --help to see your options.'); + $cache = new FilesystemCache(); + $cacheKey = md5(collect([$tag, $startDate, $endDate])->implode('_')); + + if (!$events = $cache->get($cacheKey)) { + $response = $this->client->get('http://api.joind.in/v2.1/events', [ + 'query' => [ + 'tags' => [$tag], + 'startdate' => $startDate, + 'enddate' => $endDate, + 'verbose' => 'yes', + ] + ]); + + $cache->set($cacheKey, json_decode($response->getBody())->events, 3600); + } } }