2016-04-05 13:19:35 -04:00
|
|
|
<?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;
|
|
|
|
|
2016-07-27 13:18:52 -04:00
|
|
|
use Aviat\Ion\ConfigInterface;
|
2016-07-22 17:48:13 -04:00
|
|
|
use Aviat\Ion\Cache\CacheDriverInterface;
|
2016-07-28 10:44:13 -04:00
|
|
|
use Aviat\Ion\Exception\ConfigException;
|
2016-07-27 13:18:52 -04:00
|
|
|
use Aviat\Ion\Model\DB;
|
2016-04-05 13:19:35 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Driver for caching via a traditional SQL database
|
|
|
|
*/
|
2016-07-22 17:48:13 -04:00
|
|
|
class SQLDriver extends DB implements CacheDriverInterface {
|
2016-04-05 13:19:35 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The query builder object
|
|
|
|
* @var object $db
|
|
|
|
*/
|
|
|
|
protected $db;
|
2016-04-06 12:11:07 -04:00
|
|
|
|
2016-04-05 13:19:35 -04:00
|
|
|
/**
|
|
|
|
* Create the driver object
|
2016-07-27 13:18:52 -04:00
|
|
|
*
|
|
|
|
* @param ConfigInterface $config
|
2016-07-28 10:44:13 -04:00
|
|
|
* @throws ConfigException
|
2016-04-05 13:19:35 -04:00
|
|
|
*/
|
2016-07-27 13:18:52 -04:00
|
|
|
public function __construct(ConfigInterface $config)
|
2016-04-05 13:19:35 -04:00
|
|
|
{
|
2016-07-27 13:18:52 -04:00
|
|
|
parent::__construct($config);
|
2016-07-28 10:44:13 -04:00
|
|
|
|
|
|
|
if ( ! array_key_exists('cache', $this->db_config))
|
|
|
|
{
|
|
|
|
throw new ConfigException("Missing '[cache]' section in database config.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->db = \Query($this->db_config['cache']);
|
2016-04-05 13:19:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-27 13:18:52 -04:00
|
|
|
* Retrieve a value from the cache backend
|
2016-04-05 13:19:35 -04:00
|
|
|
*
|
|
|
|
* @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))
|
|
|
|
{
|
2016-07-28 10:44:13 -04:00
|
|
|
return json_decode($row['value']);
|
2016-04-05 13:19:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-04-06 12:11:07 -04:00
|
|
|
|
2016-04-05 13:19:35 -04:00
|
|
|
/**
|
|
|
|
* Set a cached value
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @param mixed $value
|
|
|
|
* @return CacheDriverInterface
|
|
|
|
*/
|
|
|
|
public function set($key, $value)
|
|
|
|
{
|
|
|
|
$this->db->set([
|
|
|
|
'key' => $key,
|
2016-07-28 10:44:13 -04:00
|
|
|
'value' => json_encode($value),
|
2016-04-05 13:19:35 -04:00
|
|
|
]);
|
|
|
|
|
2016-04-08 14:25:45 -04:00
|
|
|
$this->db->insert('cache');
|
2016-04-05 13:19:35 -04:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2016-04-06 12:11:07 -04:00
|
|
|
|
2016-04-05 13:19:35 -04:00
|
|
|
/**
|
2016-04-07 12:34:57 -04:00
|
|
|
* Invalidate a cached value
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return CacheDriverInterface
|
|
|
|
*/
|
2016-04-05 13:19:35 -04:00
|
|
|
public function invalidate($key)
|
|
|
|
{
|
|
|
|
$this->db->where('key', $key)
|
|
|
|
->delete('cache');
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2016-04-06 12:11:07 -04:00
|
|
|
|
|
|
|
/**
|
2016-04-07 12:34:57 -04:00
|
|
|
* Clear the contents of the cache
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-04-06 12:11:07 -04:00
|
|
|
public function invalidateAll()
|
|
|
|
{
|
|
|
|
$this->db->truncate('cache');
|
|
|
|
}
|
2016-04-05 13:19:35 -04:00
|
|
|
}
|
|
|
|
// End of SQLDriver.php
|