From 535de1cf508e5cea1f231d1710fced395aed62a5 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Wed, 22 Mar 2017 11:41:25 -0400 Subject: [PATCH] Add command to prime cache, see #19 --- console | 4 +- src/API/Kitsu/KitsuTrait.php | 15 ++++- .../{ClearCache.php => CacheClear.php} | 4 +- src/Command/CachePrime.php | 55 +++++++++++++++++++ 4 files changed, 74 insertions(+), 4 deletions(-) rename src/Command/{ClearCache.php => CacheClear.php} (95%) create mode 100644 src/Command/CachePrime.php diff --git a/console b/console index 638f9bcc..cd89d05d 100755 --- a/console +++ b/console @@ -34,7 +34,9 @@ unset($CONF_DIR); // Start console script // --------------------------------------------------------------------------------------------------------------------- $console = new \ConsoleKit\Console([ - 'clear-cache' => '\Aviat\AnimeClient\Command\ClearCache', + 'cache-prime' => '\Aviat\AnimeClient\Command\CachePrime', + 'cache-clear' => '\Aviat\AnimeClient\Command\CacheClear' + 'clear-cache' => '\Aviat\AnimeClient\Command\CacheClear', 'sync-lists' => '\Aviat\AnimeClient\Command\SyncKitsuWithMal' ]); diff --git a/src/API/Kitsu/KitsuTrait.php b/src/API/Kitsu/KitsuTrait.php index 75233ee6..e9949bef 100644 --- a/src/API/Kitsu/KitsuTrait.php +++ b/src/API/Kitsu/KitsuTrait.php @@ -61,12 +61,25 @@ trait KitsuTrait { ->get('session') ->getSegment(SESSION_SEGMENT); + $cache = $this->getContainer()->get('cache'); + $cacheItem = $cache->getItem('kitsu-auth-token'); + $token = null; + + if ($sessionSegment->get('auth_token') !== NULL && $url !== K::AUTH_URL) { $token = $sessionSegment->get('auth_token'); + } + else if ($sessionSegment->get('auth_token') === NULL && $cacheItem->isHit()) + { + $token = $cacheItem->get(); + } + + if ( ! is_null($token)) + { $request = $request->setAuth('bearer', $token); } - + if (array_key_exists('form_params', $options)) { $request->setFormFields($options['form_params']); diff --git a/src/Command/ClearCache.php b/src/Command/CacheClear.php similarity index 95% rename from src/Command/ClearCache.php rename to src/Command/CacheClear.php index 3e4d8dbc..6029e72c 100644 --- a/src/Command/ClearCache.php +++ b/src/Command/CacheClear.php @@ -19,7 +19,7 @@ namespace Aviat\AnimeClient\Command; /** * Clears the API Cache */ -class ClearCache extends BaseCommand { +class CacheClear extends BaseCommand { /** * Clear the API cache * @@ -33,7 +33,7 @@ class ClearCache extends BaseCommand { $this->setContainer($this->setupContainer()); $cache = $this->container->get('cache'); $cache->clear(); - + $this->echoBox('API Cache has been cleared.'); } } diff --git a/src/Command/CachePrime.php b/src/Command/CachePrime.php new file mode 100644 index 00000000..12ccabd2 --- /dev/null +++ b/src/Command/CachePrime.php @@ -0,0 +1,55 @@ + + * @copyright 2015 - 2017 Timothy J. Warren + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @version 4.0 + * @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient + */ + +namespace Aviat\AnimeClient\Command; + +/** + * Clears the API Cache + */ +class CachePrime extends BaseCommand { + /** + * Clear, then prime the API cache + * + * @param array $args + * @param array $options + * @return void + * @throws \ConsoleKit\ConsoleException + */ + public function execute(array $args, array $options = []) + { + $this->setContainer($this->setupContainer()); + + $cache = $container->get('cache'); + + // Save the user id, if it exists, for priming the cache + $userIdItem = $cache->getItem('kitsu-auth-token'); + $userId = $userIdItem->isHit() ? $userIdItem->get : null; + + $cache->clear(); + + if ( ! is_null($userId)) + { + $userIdItem = $cache->getItem('kitsu-auth-token'); + $userIdItem->set($userId); + $userIdItem->save(); + } + + $kitsuModel = $container->get('kitsu-model'); + $kitsuModel->getFullOrganizedAnimeList(); + + $this->echoBox('API Cache has been primed.'); + } +}