Use computed key from appbox.

This commit is contained in:
Benoît Burnichon
2015-07-03 16:44:28 +02:00
parent 26f8869f1e
commit 62ac0e0cbc
4 changed files with 23 additions and 16 deletions

View File

@@ -130,7 +130,7 @@ class RepositoriesServiceProvider implements ServiceProviderInterface
$appbox = $app->getApplicationBox();
$repository = new DbalDataboxRepository($appbox->get_connection(), $factory);
return new CachedDataboxRepository($repository, $app['cache'], $factory);
return new CachedDataboxRepository($repository, $app['cache'], $appbox->get_cache_key($appbox::CACHE_LIST_BASES), $factory);
});
}

View File

@@ -14,25 +14,26 @@ use Doctrine\Common\Cache\Cache;
class CachedDataboxRepository implements DataboxRepositoryInterface
{
const CACHE_KEY = \appbox::CACHE_LIST_BASES;
/** @var DataboxRepositoryInterface */
private $repository;
/** @var Cache */
private $cache;
/** @var string */
private $cacheKey;
/** @var DataboxFactory */
private $factory;
public function __construct(DataboxRepositoryInterface $repository, Cache $cache, DataboxFactory $factory)
public function __construct(DataboxRepositoryInterface $repository, Cache $cache, $cacheKey, DataboxFactory $factory)
{
$this->repository = $repository;
$this->cache = $cache;
$this->cacheKey = $cacheKey;
$this->factory = $factory;
}
public function find($id)
{
$rows = $this->cache->fetch(self::CACHE_KEY);
$rows = $this->cache->fetch($this->cacheKey);
if (isset($rows[$id])) {
return $this->factory->create($id, $rows[$id]);
@@ -43,7 +44,7 @@ class CachedDataboxRepository implements DataboxRepositoryInterface
public function findAll()
{
$rows = $this->cache->fetch(self::CACHE_KEY);
$rows = $this->cache->fetch($this->cacheKey);
if (is_array($rows)) {
return $this->factory->createMany($rows);
@@ -64,9 +65,9 @@ class CachedDataboxRepository implements DataboxRepositoryInterface
$rows = array();
foreach ($databoxes as $databox) {
$rows[$databox->get_sbas_id()] = $databox->getAsRow();
$rows[$databox->get_sbas_id()] = $databox->getRawData();
}
$this->cache->save(self::CACHE_KEY, $rows);
$this->cache->save($this->cacheKey, $rows);
}
}

View File

@@ -1531,7 +1531,7 @@ class databox extends base
*
* @return array
*/
public function getAsRow()
public function getRawData()
{
return [
'ord' => $this->ord,

View File

@@ -20,6 +20,7 @@ final class CachedDataboxRepositoryTest extends \PHPUnit_Framework_TestCase
{
/** @var ObjectProphecy */
private $cache;
private $cacheKey = 'test_key';
/** @var ObjectProphecy */
private $factory;
/** @var ObjectProphecy */
@@ -34,7 +35,12 @@ final class CachedDataboxRepositoryTest extends \PHPUnit_Framework_TestCase
$this->repository = $this->prophesize(DataboxRepositoryInterface::class);
$this->factory = $this->prophesize(DataboxFactory::class);
$this->sut = new CachedDataboxRepository($this->repository->reveal(), $this->cache->reveal(), $this->factory->reveal());
$this->sut = new CachedDataboxRepository(
$this->repository->reveal(),
$this->cache->reveal(),
$this->cacheKey,
$this->factory->reveal()
);
}
public function testItImplementsDataboxRepositoryInterface()
@@ -46,7 +52,7 @@ final class CachedDataboxRepositoryTest extends \PHPUnit_Framework_TestCase
{
$databox = $this->prophesize(\databox::class);
$this->cache->fetch(CachedDataboxRepository::CACHE_KEY)
$this->cache->fetch($this->cacheKey)
->willReturn(false);
$this->repository->find(42)
->willReturn($databox->reveal());
@@ -58,7 +64,7 @@ final class CachedDataboxRepositoryTest extends \PHPUnit_Framework_TestCase
{
$databox = $this->prophesize(\databox::class);
$this->cache->fetch(CachedDataboxRepository::CACHE_KEY)
$this->cache->fetch($this->cacheKey)
->willReturn([42 => ['foo' => 'bar']]);
$this->repository->find(42)
->shouldNotBeCalled();
@@ -73,17 +79,17 @@ final class CachedDataboxRepositoryTest extends \PHPUnit_Framework_TestCase
$databox = $this->prophesize(\databox::class);
$databox->get_sbas_id()
->willReturn(42);
$databox->getAsRow()
$databox->getRawData()
->willReturn(['foo' => 'bar']);
$cache_data = [42 => ['foo' => 'bar']];
$databoxes = [42 => $databox->reveal()];
$this->cache->fetch(CachedDataboxRepository::CACHE_KEY)
$this->cache->fetch($this->cacheKey)
->willReturn(false);
$this->repository->findAll()
->willReturn($databoxes);
$this->cache->save(CachedDataboxRepository::CACHE_KEY, $cache_data)
$this->cache->save($this->cacheKey, $cache_data)
->shouldBeCalled();
$this->factory->createMany(Argument::any())
@@ -99,7 +105,7 @@ final class CachedDataboxRepositoryTest extends \PHPUnit_Framework_TestCase
$cache_data = [42 => ['foo' => 'bar']];
$databoxes = [42 => $databox->reveal()];
$this->cache->fetch(CachedDataboxRepository::CACHE_KEY)
$this->cache->fetch($this->cacheKey)
->willReturn($cache_data);
$this->repository->findAll()
->shouldNotBeCalled();