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/**
**/coverage/**
**/docs/**
**/node_modules/**
public/images/*
composer.lock
*.sqlite
@ -21,3 +22,5 @@ build/**
!build/*.xml
!build/*.php
app/config/*.toml
!app/config/*.toml.example
phinx.yml

View File

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

View File

@ -215,7 +215,7 @@ class Manga extends API {
];
$response = $this->get('manga_library_entries', $config);
$data = $this->_check_cache($response);
$data = $this->transform($response);
$output = $this->map_by_status($data);
return (array_key_exists($status, $output))
@ -230,7 +230,7 @@ class Manga extends API {
* @codeCoverageIgnore
* @return array
*/
private function _check_cache($response)
private function transform($response)
{
// Bail out early if there isn't any manga data
$api_data = Json::decode($response->getBody(), TRUE);
@ -239,30 +239,10 @@ class Manga extends API {
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);
$transformer = new Transformer\MangaListTransformer();
$transformed_data = $transformer->transform_collection($zippered_data);
Json::encodeFile($transformed_cache_file, $transformed_data);
return $transformed_data;
}
$zippered_data = $this->zipper_lists($api_data);
$transformer = new Transformer\MangaListTransformer();
$transformed_data = $transformer->transform_collection($zippered_data);
return $transformed_data;
}
/**