Update Teller to use cache driver directly, rather than wrapping the pool

This commit is contained in:
Timothy Warren 2020-05-07 19:42:27 -04:00
parent 247aedaf5a
commit 1853119188
7 changed files with 101 additions and 31 deletions

View File

@ -86,7 +86,7 @@ class ApcuDriver extends AbstractDriver {
* @param int $expires
* @return bool
*/
public function set(string $key, $value, int $expires = 0): bool
public function set(string $key, $value, ?int $expires = 0): bool
{
$ttl = $this->getTTLFromExpiration($expires);

View File

@ -36,7 +36,7 @@ interface DriverInterface {
* @param int $expires
* @return bool
*/
public function set(string $key, $value, int $expires = 0): bool;
public function set(string $key, $value, ?int $expires = 0): bool;
/**
* Get the value for the selected cache key

View File

@ -121,7 +121,7 @@ class MemcachedDriver extends AbstractDriver {
* @param int $expires
* @return bool
*/
public function set(string $key, $value, int $expires = 0): bool
public function set(string $key, $value, ?int $expires = 0): bool
{
return $this->conn->set($key, $value, $expires);
}

View File

@ -72,7 +72,7 @@ class NullDriver extends AbstractDriver {
* @param int $expires
* @return bool
*/
public function set(string $key, $value, int $expires = 0): bool
public function set(string $key, $value, ?int $expires = 0): bool
{
$this->store[$key] = $value;
return $this->store[$key] === $value;

View File

@ -88,7 +88,7 @@ class RedisDriver extends AbstractDriver {
* @param int $expires
* @return bool
*/
public function set(string $key, $value, int $expires = 0): bool
public function set(string $key, $value, ?int $expires = 0): bool
{
$value = serialize($value);

View File

@ -15,12 +15,16 @@
*/
namespace Aviat\Banker;
use Aviat\Banker\Driver\DriverInterface;
use Aviat\Banker\Exception\InvalidArgumentException;
use Psr\Log\LoggerAwareInterface;
use Psr\SimpleCache;
use Psr\Log\LoggerInterface;
class Teller implements SimpleCache\CacheInterface {
private Pool $pool;
class Teller implements SimpleCache\CacheInterface, LoggerAwareInterface {
use LoggerTrait;
private DriverInterface $driver;
/**
* Set up the cache backend
@ -30,7 +34,12 @@ class Teller implements SimpleCache\CacheInterface {
*/
public function __construct(array $config, ?LoggerInterface $logger = NULL)
{
$this->pool = new Pool($config, $logger);
$this->driver = $this->loadDriver($config);
if ($logger !== NULL)
{
$this->setLogger($logger);
}
}
/**
@ -48,8 +57,7 @@ class Teller implements SimpleCache\CacheInterface {
{
$this->validateKey($key);
$item = $this->pool->getItem($key);
return ($item->isHit()) ? $item->get() : $default;
return ($this->driver->exists($key)) ? $this->driver->get($key) : $default;
}
/**
@ -70,15 +78,7 @@ class Teller implements SimpleCache\CacheInterface {
{
$this->validateKey($key);
$item = $this->pool->getItem($key);
$item->set($value);
if ($ttl !== NULL)
{
$item->expiresAfter($ttl);
}
return $this->pool->save($item);
return $this->driver->set($key, $value, $ttl);
}
/**
@ -95,7 +95,7 @@ class Teller implements SimpleCache\CacheInterface {
{
$this->validateKey($key);
return $this->pool->deleteItem($key);
return $this->driver->delete($key);
}
/**
@ -105,7 +105,7 @@ class Teller implements SimpleCache\CacheInterface {
*/
public function clear(): bool
{
return $this->pool->clear();
return $this->driver->flush();
}
/**
@ -124,14 +124,7 @@ class Teller implements SimpleCache\CacheInterface {
{
$this->validateKeys($keys);
$output = [];
foreach ($keys as $k)
{
$output[$k] = $this->get($k, $default);
}
return $output;
return $this->driver->getMultiple((array)$keys);
}
/**
@ -177,7 +170,7 @@ class Teller implements SimpleCache\CacheInterface {
{
$this->validateKeys($keys);
return $this->pool->deleteItems((array)$keys);
return $this->driver->deleteMultiple((array)$keys);
}
/**
@ -199,7 +192,7 @@ class Teller implements SimpleCache\CacheInterface {
{
$this->validateKey($key);
return $this->pool->hasItem($key);
return $this->driver->exists($key);
}
/**
@ -237,4 +230,21 @@ class Teller implements SimpleCache\CacheInterface {
throw new InvalidArgumentException('Invalid characters in cache key');
}
}
/**
* Instantiate the appropriate cache backend based on the config
*
* @param array $driverConfig
* @return DriverInterface
*/
protected function loadDriver(array $driverConfig = []): DriverInterface
{
$driver = ucfirst(strtolower($driverConfig['driver'] ?? 'null'));
$class = __NAMESPACE__ . "\\Driver\\${driver}Driver";
$driverConfig['connection'] = $driverConfig['connection'] ?? [];
$driverConfig['options'] = $driverConfig['options'] ?? [];
return new $class($driverConfig['connection'], $driverConfig['options']);
}
}

View File

@ -15,9 +15,14 @@
*/
namespace Aviat\Banker\Tests;
use Aviat\Banker\Pool;
use Aviat\Banker\Teller;
use Aviat\Banker\Exception\InvalidArgumentException;
use Monolog\Handler\SyslogHandler;
use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
class TellerTest extends TestCase {
@ -51,6 +56,61 @@ class TellerTest extends TestCase {
$this->teller->clear();
}
public function testGetDefaultLogger(): void
{
$friend = new Friend($this->teller);
$driverFriend = new Friend($friend->driver);
// Check that a valid logger is set
$this->assertInstanceOf(LoggerInterface::class, $friend->getLogger(), "Logger exists after being set");
$this->assertInstanceOf(LoggerInterface::class, $driverFriend->getLogger(), "Logger exists on driver after being set");
// Make sure we get the default Null logger
$this->assertTrue(is_a($friend->getLogger(), NullLogger::class));
$this->assertTrue(is_a($driverFriend->getLogger(), NullLogger::class));
}
public function testSetLoggerInConstructor(): void
{
$logger = new Logger('test');
$logger->pushHandler(new SyslogHandler('warning', LOG_USER, Logger::WARNING));
$teller = new Teller([
'driver' => 'null',
'connection' => [],
], $logger);
$friend = new Friend($teller);
$driverFriend = new Friend($friend->driver);
// Check that a valid logger is set
$this->assertInstanceOf(LoggerInterface::class, $friend->getLogger(), "Logger exists after being set");
$this->assertInstanceOf(LoggerInterface::class, $driverFriend->getLogger(), "Logger exists on driver after being set");
// Make sure we aren't just getting the default Null logger
$this->assertFalse(is_a($friend->getLogger(), NullLogger::class));
$this->assertFalse(is_a($driverFriend->getLogger(), NullLogger::class));
}
public function testGetSetLogger(): void
{
$logger = new Logger('test');
$logger->pushHandler(new SyslogHandler('warning2',LOG_USER, Logger::WARNING));
$this->teller->setLogger($logger);
$friend = new Friend($this->teller);
$driverFriend = new Friend($friend->driver);
// Check that a valid logger is set
$this->assertInstanceOf(LoggerInterface::class, $friend->getLogger(), "Logger exists after being set");
$this->assertInstanceOf(LoggerInterface::class, $driverFriend->getLogger(), "Logger exists on driver after being set");
// Make sure we aren't just getting the default Null logger
$this->assertFalse(is_a($friend->getLogger(), NullLogger::class));
$this->assertFalse(is_a($driverFriend->getLogger(), NullLogger::class));
}
public function testGetSet(): void
{
foreach ($this->testValues as $key => $value)