Add some naive cache invalidation to update methods

This commit is contained in:
Timothy Warren 2016-04-06 12:11:07 -04:00
parent 3127e06a47
commit 352ebb4105
5 changed files with 58 additions and 19 deletions

View File

@ -91,15 +91,10 @@ class Anime extends BaseController {
'completed' => AnimeWatchingStatus::COMPLETED 'completed' => AnimeWatchingStatus::COMPLETED
]; ];
if (array_key_exists($type, $type_title_map)) $title = (array_key_exists($type, $type_title_map))
{ ? $this->config->get('whose_list') .
$title = $this->config->get('whose_list') . "'s Anime List · {$type_title_map[$type]}"
"'s Anime List · {$type_title_map[$type]}"; : '';
}
else
{
$title = '';
}
$view_map = [ $view_map = [
'' => 'cover', '' => 'cover',
@ -110,10 +105,6 @@ class Anime extends BaseController {
? $this->cache->get($this->model, 'get_list', ['status' => $model_map[$type]]) ? $this->cache->get($this->model, 'get_list', ['status' => $model_map[$type]])
: $this->cache->get($this->model, 'get_all_lists', []); : $this->cache->get($this->model, 'get_all_lists', []);
/*$data = ($type != 'all')
? $this->model->get_list($model_map[$type])
: $this->model->get_all_lists();*/
$this->outputHTML('anime/' . $view_map[$view], [ $this->outputHTML('anime/' . $view_map[$view], [
'title' => $title, 'title' => $title,
'sections' => $data 'sections' => $data
@ -166,6 +157,7 @@ class Anime extends BaseController {
if ($result['statusCode'] == 201) if ($result['statusCode'] == 201)
{ {
$this->set_flash_message('Added new anime to list', 'success'); $this->set_flash_message('Added new anime to list', 'success');
$this->cache->purge();
} }
else else
{ {
@ -245,6 +237,7 @@ class Anime extends BaseController {
: "{$result['anime']['title']}"; : "{$result['anime']['title']}";
$this->set_flash_message("Successfully updated {$title}.", 'success'); $this->set_flash_message("Successfully updated {$title}.", 'success');
$this->cache->purge();
} }
else else
{ {
@ -260,6 +253,7 @@ class Anime extends BaseController {
public function update() public function update()
{ {
$response = $this->model->update($this->request->getParsedBody()); $response = $this->model->update($this->request->getParsedBody());
$this->cache->purge();
$this->outputJSON($response['body'], $response['statusCode']); $this->outputJSON($response['body'], $response['statusCode']);
} }
@ -269,6 +263,7 @@ class Anime extends BaseController {
public function delete() public function delete()
{ {
$response = $this->model->update($this->request->getParsedBody()); $response = $this->model->update($this->request->getParsedBody());
$this->cache->purge();
$this->outputJSON($response['body'], $response['statusCode']); $this->outputJSON($response['body'], $response['statusCode']);
} }

View File

@ -40,5 +40,12 @@ interface CacheDriverInterface {
* @return CacheDriverInterface * @return CacheDriverInterface
*/ */
public function invalidate($key); public function invalidate($key);
/**
* Clear the contents of the cache
*
* @return void
*/
public function invalidateAll();
} }
// End of CacheDriverInterface.php // End of CacheDriverInterface.php

View File

@ -16,7 +16,7 @@ namespace Aviat\Ion\Cache;
* Interface for retrieving values from cache * Interface for retrieving values from cache
*/ */
interface CacheInterface { interface CacheInterface {
/** /**
* Retreive a cached value if it exists, otherwise, get the value * Retreive a cached value if it exists, otherwise, get the value
* from the passed arguments * from the passed arguments
@ -27,5 +27,22 @@ interface CacheInterface {
* @return mixed - the cached or fresh data * @return mixed - the cached or fresh data
*/ */
public function get($object, $method, array $args=[]); public function get($object, $method, array $args=[]);
/**
* Retreive a fresh value, and update the cache
*
* @param object $object - object to retrieve fresh value from
* @param string $method - method name to call
* @param [array] $args - the arguments to pass to the retrieval method
* @return mixed - the fresh data
*/
public function getFresh($object, $method, array $args=[]);
/**
* Clear the entire cache
*
* @return void
*/
public function purge();
} }
// End of CacheInterface.php // End of CacheInterface.php

View File

@ -76,7 +76,17 @@ class CacheManager implements CacheInterface {
$this->driver->set($hash, $data); $this->driver->set($hash, $data);
return $data; return $data;
} }
/**
* Clear the entire cache
*
* @return void
*/
public function purge()
{
$this->driver->invalidateAll();
}
/** /**
* Generate a hash as a cache key from the current method call * Generate a hash as a cache key from the current method call
* *
@ -85,7 +95,7 @@ class CacheManager implements CacheInterface {
* @param array $args * @param array $args
* @return string * @return string
*/ */
public function generateHashForMethod($object, $method, array $args) protected function generateHashForMethod($object, $method, array $args)
{ {
$classname = get_class($object); $classname = get_class($object);
$keyObj = [ $keyObj = [

View File

@ -26,7 +26,7 @@ class SQLDriver extends DB implements \Aviat\Ion\Cache\CacheDriverInterface {
* @var object $db * @var object $db
*/ */
protected $db; protected $db;
/** /**
* Create the driver object * Create the driver object
*/ */
@ -66,7 +66,7 @@ class SQLDriver extends DB implements \Aviat\Ion\Cache\CacheDriverInterface {
return NULL; return NULL;
} }
/** /**
* Set a cached value * Set a cached value
* *
@ -92,7 +92,7 @@ class SQLDriver extends DB implements \Aviat\Ion\Cache\CacheDriverInterface {
return $this; return $this;
} }
/** /**
* Invalidate a cached value * Invalidate a cached value
* *
@ -106,5 +106,15 @@ class SQLDriver extends DB implements \Aviat\Ion\Cache\CacheDriverInterface {
return $this; return $this;
} }
/**
* Clear the contents of the cache
*
* @return void
*/
public function invalidateAll()
{
$this->db->truncate('cache');
}
} }
// End of SQLDriver.php // End of SQLDriver.php