Add command to prime cache, see #19

This commit is contained in:
Timothy Warren 2017-03-22 11:41:25 -04:00
parent e84b837dce
commit 535de1cf50
4 changed files with 74 additions and 4 deletions

View File

@ -34,7 +34,9 @@ unset($CONF_DIR);
// Start console script // Start console script
// --------------------------------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------------------------------
$console = new \ConsoleKit\Console([ $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' 'sync-lists' => '\Aviat\AnimeClient\Command\SyncKitsuWithMal'
]); ]);

View File

@ -61,12 +61,25 @@ trait KitsuTrait {
->get('session') ->get('session')
->getSegment(SESSION_SEGMENT); ->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) if ($sessionSegment->get('auth_token') !== NULL && $url !== K::AUTH_URL)
{ {
$token = $sessionSegment->get('auth_token'); $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); $request = $request->setAuth('bearer', $token);
} }
if (array_key_exists('form_params', $options)) if (array_key_exists('form_params', $options))
{ {
$request->setFormFields($options['form_params']); $request->setFormFields($options['form_params']);

View File

@ -19,7 +19,7 @@ namespace Aviat\AnimeClient\Command;
/** /**
* Clears the API Cache * Clears the API Cache
*/ */
class ClearCache extends BaseCommand { class CacheClear extends BaseCommand {
/** /**
* Clear the API cache * Clear the API cache
* *
@ -33,7 +33,7 @@ class ClearCache extends BaseCommand {
$this->setContainer($this->setupContainer()); $this->setContainer($this->setupContainer());
$cache = $this->container->get('cache'); $cache = $this->container->get('cache');
$cache->clear(); $cache->clear();
$this->echoBox('API Cache has been cleared.'); $this->echoBox('API Cache has been cleared.');
} }
} }

View File

@ -0,0 +1,55 @@
<?php declare(strict_types=1);
/**
* Hummingbird Anime List Client
*
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
*
* PHP version 7
*
* @package HummingbirdAnimeClient
* @author Timothy J. Warren <tim@timshomepage.net>
* @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.');
}
}