Add caching to Manga views

This commit is contained in:
Timothy Warren 2016-04-07 13:11:45 -04:00
parent 0f6c998109
commit 83a200871d
3 changed files with 21 additions and 28 deletions

3
.gitignore vendored
View File

@ -8,6 +8,7 @@ vendor
**/logs/** **/logs/**
**/coverage/** **/coverage/**
**/docs/** **/docs/**
**/node_modules/**
public/images/* public/images/*
composer.lock composer.lock
*.sqlite *.sqlite
@ -21,3 +22,5 @@ build/**
!build/*.xml !build/*.xml
!build/*.php !build/*.php
app/config/*.toml app/config/*.toml
!app/config/*.toml.example
phinx.yml

View File

@ -26,6 +26,12 @@ class Manga extends Controller {
use \Aviat\Ion\StringWrapper; use \Aviat\Ion\StringWrapper;
/**
* The cache manager
* @var \Aviat\Ion\Cache\CacheInterface
*/
protected $cache;
/** /**
* The manga model * The manga model
* @var object $model * @var object $model
@ -47,6 +53,7 @@ class Manga extends Controller {
{ {
parent::__construct($container); parent::__construct($container);
$this->cache = $container->get('cache');
$this->model = $container->get('manga-model'); $this->model = $container->get('manga-model');
$this->base_data = array_merge($this->base_data, [ $this->base_data = array_merge($this->base_data, [
'menu_name' => 'manga_list', 'menu_name' => 'manga_list',
@ -82,8 +89,8 @@ class Manga extends Controller {
]; ];
$data = ($status !== 'all') $data = ($status !== 'all')
? [$map[$status] => $this->model->get_list($map[$status])] ? [$map[$status] => $this->cache->get($this->model, 'get_list', ['status' => $map[$status]]) ]
: $this->model->get_all_lists(); : $this->cache->get($this->model, 'get_all_lists');
$this->outputHTML('manga/' . $view_map[$view], [ $this->outputHTML('manga/' . $view_map[$view], [
'title' => $title, 'title' => $title,
@ -137,6 +144,7 @@ class Manga extends Controller {
if ($result['statusCode'] >= 200 && $result['statusCode'] < 300) if ($result['statusCode'] >= 200 && $result['statusCode'] < 300)
{ {
$this->set_flash_message('Added new manga to list', 'success'); $this->set_flash_message('Added new manga to list', 'success');
$this->cache->purge();
} }
else else
{ {
@ -204,6 +212,7 @@ class Manga extends Controller {
: "{$m['romaji_title']}"; : "{$m['romaji_title']}";
$this->set_flash_message("Successfully updated {$title}.", 'success'); $this->set_flash_message("Successfully updated {$title}.", 'success');
$this->cache->purge();
} }
else else
{ {
@ -221,6 +230,7 @@ class Manga extends Controller {
public function update() public function update()
{ {
$result = $this->model->update($this->request->getParsedBody()); $result = $this->model->update($this->request->getParsedBody());
$this->cache->purge();
$this->outputJSON($result['body'], $result['statusCode']); $this->outputJSON($result['body'], $result['statusCode']);
} }
} }

View File

@ -215,7 +215,7 @@ class Manga extends API {
]; ];
$response = $this->get('manga_library_entries', $config); $response = $this->get('manga_library_entries', $config);
$data = $this->_check_cache($response); $data = $this->transform($response);
$output = $this->map_by_status($data); $output = $this->map_by_status($data);
return (array_key_exists($status, $output)) return (array_key_exists($status, $output))
@ -230,7 +230,7 @@ class Manga extends API {
* @codeCoverageIgnore * @codeCoverageIgnore
* @return array * @return array
*/ */
private function _check_cache($response) private function transform($response)
{ {
// Bail out early if there isn't any manga data // Bail out early if there isn't any manga data
$api_data = Json::decode($response->getBody(), TRUE); $api_data = Json::decode($response->getBody(), TRUE);
@ -239,31 +239,11 @@ class Manga extends API {
return []; return [];
} }
$cache_file = _dir($this->config->get('data_cache_path'), 'manga.json');
$transformed_cache_file = _dir(
$this->config->get('data_cache_path'),
'manga-transformed.json'
);
$cached_data = file_exists($cache_file)
? Json::decodeFile($cache_file)
: [];
if ($cached_data === $api_data && file_exists($transformed_cache_file))
{
return Json::decodeFile($transformed_cache_file);
}
else
{
Json::encodeFile($cache_file, $api_data);
$zippered_data = $this->zipper_lists($api_data); $zippered_data = $this->zipper_lists($api_data);
$transformer = new Transformer\MangaListTransformer(); $transformer = new Transformer\MangaListTransformer();
$transformed_data = $transformer->transform_collection($zippered_data); $transformed_data = $transformer->transform_collection($zippered_data);
Json::encodeFile($transformed_cache_file, $transformed_data);
return $transformed_data; return $transformed_data;
} }
}
/** /**
* Map transformed anime data to be organized by reading status * Map transformed anime data to be organized by reading status