Start of caching implementation
This commit is contained in:
parent
44e4aaa732
commit
3127e06a47
@ -15,6 +15,7 @@ use Zend\Diactoros\ServerRequestFactory;
|
||||
use Zend\Diactoros\Response;
|
||||
|
||||
use Aviat\Ion\Di\Container;
|
||||
use Aviat\Ion\Cache\CacheManager;
|
||||
use Aviat\AnimeClient\Auth\HummingbirdAuth;
|
||||
use Aviat\AnimeClient\Model;
|
||||
|
||||
@ -30,7 +31,6 @@ return function(array $config_array = []) {
|
||||
|
||||
$app_logger = new Logger('animeclient');
|
||||
$app_logger->pushHandler(new RotatingFileHandler(__DIR__ . '/logs/app.log', Logger::NOTICE));
|
||||
$app_logger->pushHandler(new BrowserConsoleHandler(Logger::INFO));
|
||||
$container->setLogger($app_logger, 'default');
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
@ -84,6 +84,8 @@ return function(array $config_array = []) {
|
||||
|
||||
$container->set('auth', new HummingbirdAuth($container));
|
||||
|
||||
$container->set('cache', new CacheManager($container));
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Dispatcher
|
||||
// -------------------------------------------------------------------------
|
||||
|
@ -14,5 +14,8 @@ show_anime_collection = true
|
||||
# do you wish to show the manga collection?
|
||||
show_manga_collection = false
|
||||
|
||||
# cache driver for api calls (SQLDriver, RedisDriver, MemcacheDriver)
|
||||
cache_driver = SQLDriver
|
||||
|
||||
# path to public directory on the server
|
||||
asset_dir = "/../../public"
|
@ -27,8 +27,8 @@ class CacheMigration extends AbstractMigration
|
||||
*/
|
||||
public function change()
|
||||
{
|
||||
$this->table('cache', ['id' => FALSE, 'primary_key' => ['key']])
|
||||
->addColumn('key', 'text')
|
||||
$cacheTable = $this->table('cache', ['id' => FALSE, 'primary_key' => ['key']]);
|
||||
$cacheTable->addColumn('key', 'text')
|
||||
->addColumn('value', 'text')
|
||||
->create();
|
||||
}
|
||||
|
@ -38,6 +38,11 @@ class Anime extends BaseController {
|
||||
*/
|
||||
protected $base_data;
|
||||
|
||||
/**
|
||||
* Data cache
|
||||
*/
|
||||
protected $cache;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
@ -55,6 +60,8 @@ class Anime extends BaseController {
|
||||
'other_type' => 'manga',
|
||||
'config' => $this->config,
|
||||
]);
|
||||
|
||||
$this->cache = $container->get('cache');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -100,8 +107,12 @@ class Anime extends BaseController {
|
||||
];
|
||||
|
||||
$data = ($type != 'all')
|
||||
? $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->model->get_all_lists();*/
|
||||
|
||||
$this->outputHTML('anime/' . $view_map[$view], [
|
||||
'title' => $title,
|
||||
|
@ -32,5 +32,13 @@ interface CacheDriverInterface {
|
||||
* @return CacheDriverInterface
|
||||
*/
|
||||
public function set($key, $value);
|
||||
|
||||
/**
|
||||
* Invalidate a cached value
|
||||
*
|
||||
* @param string $key
|
||||
* @return CacheDriverInterface
|
||||
*/
|
||||
public function invalidate($key);
|
||||
}
|
||||
// End of CacheDriverInterface.php
|
@ -23,9 +23,9 @@ interface CacheInterface {
|
||||
*
|
||||
* @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
|
||||
* @param [array] $args - the arguments to pass to the retrieval method
|
||||
* @return mixed - the cached or fresh data
|
||||
*/
|
||||
public function get($object, $method, array $args);
|
||||
public function get($object, $method, array $args=[]);
|
||||
}
|
||||
// End of CacheInterface.php
|
100
src/Aviat/Ion/Cache/CacheManager.php
Normal file
100
src/Aviat/Ion/Cache/CacheManager.php
Normal file
@ -0,0 +1,100 @@
|
||||
<?php
|
||||
/**
|
||||
* Ion
|
||||
*
|
||||
* Building blocks for web development
|
||||
*
|
||||
* @package Ion
|
||||
* @author Timothy J. Warren
|
||||
* @copyright Copyright (c) 2015 - 2016
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
namespace Aviat\Ion\Cache;
|
||||
|
||||
use \Aviat\Ion\Di\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Class proxying cached and fresh values from the selected cache driver
|
||||
*/
|
||||
class CacheManager implements CacheInterface {
|
||||
|
||||
/**
|
||||
* @var CacheDriverInterface
|
||||
*/
|
||||
protected $driver;
|
||||
|
||||
/**
|
||||
* Retreive the appropriate driver from the container
|
||||
*/
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
$config = $container->get('config');
|
||||
$driverConf = $config->get('cache_driver');
|
||||
|
||||
$driverClass = __NAMESPACE__ . "\\Driver\\{$driverConf}";
|
||||
$driver = new $driverClass($container);
|
||||
|
||||
$this->driver = $driver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retreive a cached value if it exists, otherwise, get the value
|
||||
* from the passed arguments
|
||||
*
|
||||
* @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 cached or fresh data
|
||||
*/
|
||||
public function get($object, $method, array $args=[])
|
||||
{
|
||||
$hash = $this->generateHashForMethod($object, $method, $args);
|
||||
|
||||
$data = $this->driver->get($hash);
|
||||
|
||||
if (empty($data))
|
||||
{
|
||||
$data = call_user_func_array([$object, $method], $args);
|
||||
$this->driver->set($hash, $data);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retreive a fresh value from the method, 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=[])
|
||||
{
|
||||
$hash = $this->generateHashForMethod($object, $method, $args);
|
||||
$data = call_user_func_array([$object, $method], $args);
|
||||
$this->driver->set($hash, $data);
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a hash as a cache key from the current method call
|
||||
*
|
||||
* @param object $object
|
||||
* @param string $method
|
||||
* @param array $args
|
||||
* @return string
|
||||
*/
|
||||
public function generateHashForMethod($object, $method, array $args)
|
||||
{
|
||||
$classname = get_class($object);
|
||||
$keyObj = [
|
||||
'class' => $classname,
|
||||
'method' => $method,
|
||||
'args' => $args,
|
||||
];
|
||||
$hash = sha1(json_encode($keyObj));
|
||||
return $hash;
|
||||
}
|
||||
}
|
||||
// End of CacheManager.php
|
110
src/Aviat/Ion/Cache/Driver/SQLDriver.php
Normal file
110
src/Aviat/Ion/Cache/Driver/SQLDriver.php
Normal file
@ -0,0 +1,110 @@
|
||||
<?php
|
||||
/**
|
||||
* Ion
|
||||
*
|
||||
* Building blocks for web development
|
||||
*
|
||||
* @package Ion
|
||||
* @author Timothy J. Warren
|
||||
* @copyright Copyright (c) 2015 - 2016
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
namespace Aviat\Ion\Cache\Driver;
|
||||
|
||||
use Aviat\Ion\Di\ContainerInterface;
|
||||
use Aviat\Ion\Json;
|
||||
use Aviat\AnimeClient\Model\DB;
|
||||
|
||||
/**
|
||||
* Driver for caching via a traditional SQL database
|
||||
*/
|
||||
class SQLDriver extends DB implements \Aviat\Ion\Cache\CacheDriverInterface {
|
||||
|
||||
/**
|
||||
* The query builder object
|
||||
* @var object $db
|
||||
*/
|
||||
protected $db;
|
||||
|
||||
/**
|
||||
* Create the driver object
|
||||
*/
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
parent::__construct($container);
|
||||
|
||||
try
|
||||
{
|
||||
$this->db = \Query($this->db_config['collection']);
|
||||
}
|
||||
catch (\PDOException $e)
|
||||
{
|
||||
$this->valid_database = FALSE;
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retreive a value from the cache backend
|
||||
*
|
||||
* @param string $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($key)
|
||||
{
|
||||
$query = $this->db->select('value')
|
||||
->from('cache')
|
||||
->where('key', $key)
|
||||
->get();
|
||||
|
||||
$row = $query->fetch(\PDO::FETCH_ASSOC);
|
||||
if ( ! empty($row))
|
||||
{
|
||||
return unserialize($row['value']);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a cached value
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @return CacheDriverInterface
|
||||
*/
|
||||
public function set($key, $value)
|
||||
{
|
||||
$this->db->set([
|
||||
'key' => $key,
|
||||
'value' => serialize($value),
|
||||
]);
|
||||
|
||||
/*if ( ! empty($this->get($key)))
|
||||
{
|
||||
$this->db->update('cache');
|
||||
}
|
||||
else*/
|
||||
{
|
||||
$this->db->insert('cache');
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Invalidate a cached value
|
||||
*
|
||||
* @param string $key
|
||||
* @return CacheDriverInterface
|
||||
*/
|
||||
public function invalidate($key)
|
||||
{
|
||||
$this->db->where('key', $key)
|
||||
->delete('cache');
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
// End of SQLDriver.php
|
Loading…
Reference in New Issue
Block a user