From 352ebb4105325ed1a30c63e41f02aa9478a6b3df Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Wed, 6 Apr 2016 12:11:07 -0400 Subject: [PATCH] Add some naive cache invalidation to update methods --- src/Aviat/AnimeClient/Controller/Anime.php | 21 ++++++++------------ src/Aviat/Ion/Cache/CacheDriverInterface.php | 7 +++++++ src/Aviat/Ion/Cache/CacheInterface.php | 19 +++++++++++++++++- src/Aviat/Ion/Cache/CacheManager.php | 14 +++++++++++-- src/Aviat/Ion/Cache/Driver/SQLDriver.php | 16 ++++++++++++--- 5 files changed, 58 insertions(+), 19 deletions(-) diff --git a/src/Aviat/AnimeClient/Controller/Anime.php b/src/Aviat/AnimeClient/Controller/Anime.php index 270c6108..8c1c0a8f 100644 --- a/src/Aviat/AnimeClient/Controller/Anime.php +++ b/src/Aviat/AnimeClient/Controller/Anime.php @@ -91,15 +91,10 @@ class Anime extends BaseController { 'completed' => AnimeWatchingStatus::COMPLETED ]; - if (array_key_exists($type, $type_title_map)) - { - $title = $this->config->get('whose_list') . - "'s Anime List · {$type_title_map[$type]}"; - } - else - { - $title = ''; - } + $title = (array_key_exists($type, $type_title_map)) + ? $this->config->get('whose_list') . + "'s Anime List · {$type_title_map[$type]}" + : ''; $view_map = [ '' => '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_all_lists', []); - /*$data = ($type != 'all') - ? $this->model->get_list($model_map[$type]) - : $this->model->get_all_lists();*/ - $this->outputHTML('anime/' . $view_map[$view], [ 'title' => $title, 'sections' => $data @@ -166,6 +157,7 @@ class Anime extends BaseController { if ($result['statusCode'] == 201) { $this->set_flash_message('Added new anime to list', 'success'); + $this->cache->purge(); } else { @@ -245,6 +237,7 @@ class Anime extends BaseController { : "{$result['anime']['title']}"; $this->set_flash_message("Successfully updated {$title}.", 'success'); + $this->cache->purge(); } else { @@ -260,6 +253,7 @@ class Anime extends BaseController { public function update() { $response = $this->model->update($this->request->getParsedBody()); + $this->cache->purge(); $this->outputJSON($response['body'], $response['statusCode']); } @@ -269,6 +263,7 @@ class Anime extends BaseController { public function delete() { $response = $this->model->update($this->request->getParsedBody()); + $this->cache->purge(); $this->outputJSON($response['body'], $response['statusCode']); } diff --git a/src/Aviat/Ion/Cache/CacheDriverInterface.php b/src/Aviat/Ion/Cache/CacheDriverInterface.php index add6cef0..ed64151c 100644 --- a/src/Aviat/Ion/Cache/CacheDriverInterface.php +++ b/src/Aviat/Ion/Cache/CacheDriverInterface.php @@ -40,5 +40,12 @@ interface CacheDriverInterface { * @return CacheDriverInterface */ public function invalidate($key); + + /** + * Clear the contents of the cache + * + * @return void + */ + public function invalidateAll(); } // End of CacheDriverInterface.php \ No newline at end of file diff --git a/src/Aviat/Ion/Cache/CacheInterface.php b/src/Aviat/Ion/Cache/CacheInterface.php index d4f43815..94957384 100644 --- a/src/Aviat/Ion/Cache/CacheInterface.php +++ b/src/Aviat/Ion/Cache/CacheInterface.php @@ -16,7 +16,7 @@ namespace Aviat\Ion\Cache; * Interface for retrieving values from cache */ interface CacheInterface { - + /** * Retreive a cached value if it exists, otherwise, get the value * from the passed arguments @@ -27,5 +27,22 @@ interface CacheInterface { * @return mixed - the cached or fresh data */ 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 \ No newline at end of file diff --git a/src/Aviat/Ion/Cache/CacheManager.php b/src/Aviat/Ion/Cache/CacheManager.php index 62e33d40..bf7f69da 100644 --- a/src/Aviat/Ion/Cache/CacheManager.php +++ b/src/Aviat/Ion/Cache/CacheManager.php @@ -76,7 +76,17 @@ class CacheManager implements CacheInterface { $this->driver->set($hash, $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 * @@ -85,7 +95,7 @@ class CacheManager implements CacheInterface { * @param array $args * @return string */ - public function generateHashForMethod($object, $method, array $args) + protected function generateHashForMethod($object, $method, array $args) { $classname = get_class($object); $keyObj = [ diff --git a/src/Aviat/Ion/Cache/Driver/SQLDriver.php b/src/Aviat/Ion/Cache/Driver/SQLDriver.php index e5ac36f7..8495907f 100644 --- a/src/Aviat/Ion/Cache/Driver/SQLDriver.php +++ b/src/Aviat/Ion/Cache/Driver/SQLDriver.php @@ -26,7 +26,7 @@ class SQLDriver extends DB implements \Aviat\Ion\Cache\CacheDriverInterface { * @var object $db */ protected $db; - + /** * Create the driver object */ @@ -66,7 +66,7 @@ class SQLDriver extends DB implements \Aviat\Ion\Cache\CacheDriverInterface { return NULL; } - + /** * Set a cached value * @@ -92,7 +92,7 @@ class SQLDriver extends DB implements \Aviat\Ion\Cache\CacheDriverInterface { return $this; } - + /** * Invalidate a cached value * @@ -106,5 +106,15 @@ class SQLDriver extends DB implements \Aviat\Ion\Cache\CacheDriverInterface { return $this; } + + /** + * Clear the contents of the cache + * + * @return void + */ + public function invalidateAll() + { + $this->db->truncate('cache'); + } } // End of SQLDriver.php \ No newline at end of file