mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-18 15:33:15 +00:00
74 lines
1.7 KiB
PHP
74 lines
1.7 KiB
PHP
<?php
|
|
/*
|
|
* This file is part of Phraseanet
|
|
*
|
|
* (c) 2005-2015 Alchemy
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
namespace Alchemy\Phrasea\Databox;
|
|
|
|
use Alchemy\Phrasea\Application;
|
|
use Doctrine\Common\Cache\Cache;
|
|
|
|
class CachedDataboxRepository implements DataboxRepositoryInterface
|
|
{
|
|
/** @var DataboxRepositoryInterface */
|
|
private $repository;
|
|
/** @var Cache */
|
|
private $cache;
|
|
/** @var string */
|
|
private $cacheKey;
|
|
/** @var DataboxFactory */
|
|
private $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($this->cacheKey);
|
|
|
|
if (isset($rows[$id])) {
|
|
return $this->factory->create($id, $rows[$id]);
|
|
}
|
|
|
|
return $this->repository->find($id);
|
|
}
|
|
|
|
public function findAll()
|
|
{
|
|
$rows = $this->cache->fetch($this->cacheKey);
|
|
|
|
if (is_array($rows)) {
|
|
return $this->factory->createMany($rows);
|
|
}
|
|
|
|
$databoxes = $this->repository->findAll();
|
|
|
|
$this->saveCache($databoxes);
|
|
|
|
return $databoxes;
|
|
}
|
|
|
|
/**
|
|
* @param \databox[] $databoxes
|
|
*/
|
|
private function saveCache(array $databoxes)
|
|
{
|
|
$rows = array();
|
|
|
|
foreach ($databoxes as $databox) {
|
|
$rows[$databox->get_sbas_id()] = $databox->getRawData();
|
|
}
|
|
|
|
$this->cache->save($this->cacheKey, $rows);
|
|
}
|
|
}
|