Update Teller to use cache driver directly, rather than wrapping the pool
This commit is contained in:
parent
247aedaf5a
commit
1853119188
@ -86,7 +86,7 @@ class ApcuDriver extends AbstractDriver {
|
|||||||
* @param int $expires
|
* @param int $expires
|
||||||
* @return bool
|
* @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);
|
$ttl = $this->getTTLFromExpiration($expires);
|
||||||
|
|
||||||
|
@ -36,7 +36,7 @@ interface DriverInterface {
|
|||||||
* @param int $expires
|
* @param int $expires
|
||||||
* @return bool
|
* @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
|
* Get the value for the selected cache key
|
||||||
|
@ -121,7 +121,7 @@ class MemcachedDriver extends AbstractDriver {
|
|||||||
* @param int $expires
|
* @param int $expires
|
||||||
* @return bool
|
* @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);
|
return $this->conn->set($key, $value, $expires);
|
||||||
}
|
}
|
||||||
|
@ -72,7 +72,7 @@ class NullDriver extends AbstractDriver {
|
|||||||
* @param int $expires
|
* @param int $expires
|
||||||
* @return bool
|
* @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;
|
$this->store[$key] = $value;
|
||||||
return $this->store[$key] === $value;
|
return $this->store[$key] === $value;
|
||||||
|
@ -88,7 +88,7 @@ class RedisDriver extends AbstractDriver {
|
|||||||
* @param int $expires
|
* @param int $expires
|
||||||
* @return bool
|
* @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);
|
$value = serialize($value);
|
||||||
|
|
||||||
|
@ -15,12 +15,16 @@
|
|||||||
*/
|
*/
|
||||||
namespace Aviat\Banker;
|
namespace Aviat\Banker;
|
||||||
|
|
||||||
|
use Aviat\Banker\Driver\DriverInterface;
|
||||||
use Aviat\Banker\Exception\InvalidArgumentException;
|
use Aviat\Banker\Exception\InvalidArgumentException;
|
||||||
|
use Psr\Log\LoggerAwareInterface;
|
||||||
use Psr\SimpleCache;
|
use Psr\SimpleCache;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
|
|
||||||
class Teller implements SimpleCache\CacheInterface {
|
class Teller implements SimpleCache\CacheInterface, LoggerAwareInterface {
|
||||||
private Pool $pool;
|
use LoggerTrait;
|
||||||
|
|
||||||
|
private DriverInterface $driver;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set up the cache backend
|
* Set up the cache backend
|
||||||
@ -30,7 +34,12 @@ class Teller implements SimpleCache\CacheInterface {
|
|||||||
*/
|
*/
|
||||||
public function __construct(array $config, ?LoggerInterface $logger = NULL)
|
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);
|
$this->validateKey($key);
|
||||||
|
|
||||||
$item = $this->pool->getItem($key);
|
return ($this->driver->exists($key)) ? $this->driver->get($key) : $default;
|
||||||
return ($item->isHit()) ? $item->get() : $default;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -70,15 +78,7 @@ class Teller implements SimpleCache\CacheInterface {
|
|||||||
{
|
{
|
||||||
$this->validateKey($key);
|
$this->validateKey($key);
|
||||||
|
|
||||||
$item = $this->pool->getItem($key);
|
return $this->driver->set($key, $value, $ttl);
|
||||||
$item->set($value);
|
|
||||||
|
|
||||||
if ($ttl !== NULL)
|
|
||||||
{
|
|
||||||
$item->expiresAfter($ttl);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->pool->save($item);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -95,7 +95,7 @@ class Teller implements SimpleCache\CacheInterface {
|
|||||||
{
|
{
|
||||||
$this->validateKey($key);
|
$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
|
public function clear(): bool
|
||||||
{
|
{
|
||||||
return $this->pool->clear();
|
return $this->driver->flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -124,14 +124,7 @@ class Teller implements SimpleCache\CacheInterface {
|
|||||||
{
|
{
|
||||||
$this->validateKeys($keys);
|
$this->validateKeys($keys);
|
||||||
|
|
||||||
$output = [];
|
return $this->driver->getMultiple((array)$keys);
|
||||||
|
|
||||||
foreach ($keys as $k)
|
|
||||||
{
|
|
||||||
$output[$k] = $this->get($k, $default);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $output;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -177,7 +170,7 @@ class Teller implements SimpleCache\CacheInterface {
|
|||||||
{
|
{
|
||||||
$this->validateKeys($keys);
|
$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);
|
$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');
|
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']);
|
||||||
|
}
|
||||||
}
|
}
|
@ -15,9 +15,14 @@
|
|||||||
*/
|
*/
|
||||||
namespace Aviat\Banker\Tests;
|
namespace Aviat\Banker\Tests;
|
||||||
|
|
||||||
|
use Aviat\Banker\Pool;
|
||||||
use Aviat\Banker\Teller;
|
use Aviat\Banker\Teller;
|
||||||
use Aviat\Banker\Exception\InvalidArgumentException;
|
use Aviat\Banker\Exception\InvalidArgumentException;
|
||||||
|
use Monolog\Handler\SyslogHandler;
|
||||||
|
use Monolog\Logger;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
use Psr\Log\NullLogger;
|
||||||
|
|
||||||
class TellerTest extends TestCase {
|
class TellerTest extends TestCase {
|
||||||
|
|
||||||
@ -51,6 +56,61 @@ class TellerTest extends TestCase {
|
|||||||
$this->teller->clear();
|
$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
|
public function testGetSet(): void
|
||||||
{
|
{
|
||||||
foreach ($this->testValues as $key => $value)
|
foreach ($this->testValues as $key => $value)
|
||||||
|
Loading…
Reference in New Issue
Block a user