mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-17 15:03:25 +00:00
Add de/serialization methods for collection caching
This commit is contained in:
@@ -9,20 +9,41 @@
|
|||||||
*/
|
*/
|
||||||
namespace Alchemy\Phrasea\Collection;
|
namespace Alchemy\Phrasea\Collection;
|
||||||
|
|
||||||
|
use Alchemy\Phrasea\Application;
|
||||||
use Doctrine\Common\Cache\Cache;
|
use Doctrine\Common\Cache\Cache;
|
||||||
|
|
||||||
final class CachedCollectionRepository implements CollectionRepository
|
final class CachedCollectionRepository implements CollectionRepository
|
||||||
{
|
{
|
||||||
|
|
||||||
/** @var CollectionRepository */
|
/**
|
||||||
|
* @var Application
|
||||||
|
*/
|
||||||
|
private $app;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var CollectionRepository
|
||||||
|
*/
|
||||||
private $repository;
|
private $repository;
|
||||||
/** @var Cache */
|
|
||||||
|
/**
|
||||||
|
* @var Cache
|
||||||
|
*/
|
||||||
private $cache;
|
private $cache;
|
||||||
/** @var string */
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
private $cacheKey;
|
private $cacheKey;
|
||||||
|
|
||||||
public function __construct(CollectionRepository $repository, Cache $cache, $cacheKey)
|
/**
|
||||||
|
* @param Application $application
|
||||||
|
* @param CollectionRepository $repository
|
||||||
|
* @param Cache $cache
|
||||||
|
* @param $cacheKey
|
||||||
|
*/
|
||||||
|
public function __construct(Application $application, CollectionRepository $repository, Cache $cache, $cacheKey)
|
||||||
{
|
{
|
||||||
|
$this->app = $application;
|
||||||
$this->repository = $repository;
|
$this->repository = $repository;
|
||||||
$this->cache = $cache;
|
$this->cache = $cache;
|
||||||
$this->cacheKey = $cacheKey;
|
$this->cacheKey = $cacheKey;
|
||||||
@@ -40,6 +61,10 @@ final class CachedCollectionRepository implements CollectionRepository
|
|||||||
if ($collections === false) {
|
if ($collections === false) {
|
||||||
$collections = $this->repository->findAllByDatabox($databoxId);
|
$collections = $this->repository->findAllByDatabox($databoxId);
|
||||||
$this->save($cacheKey, $collections);
|
$this->save($cacheKey, $collections);
|
||||||
|
} else {
|
||||||
|
foreach ($collections as $collection) {
|
||||||
|
$collection->hydrate($this->app);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $collections;
|
return $collections;
|
||||||
@@ -57,6 +82,8 @@ final class CachedCollectionRepository implements CollectionRepository
|
|||||||
if ($collection === false) {
|
if ($collection === false) {
|
||||||
$collection = $this->repository->find($baseId);
|
$collection = $this->repository->find($baseId);
|
||||||
$this->save($cacheKey, $collection);
|
$this->save($cacheKey, $collection);
|
||||||
|
} else {
|
||||||
|
$collection->hydrate($this->app);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $collection;
|
return $collection;
|
||||||
@@ -75,6 +102,8 @@ final class CachedCollectionRepository implements CollectionRepository
|
|||||||
if ($collection === false) {
|
if ($collection === false) {
|
||||||
$collection = $this->repository->findByCollectionId($databoxId, $collectionId);
|
$collection = $this->repository->findByCollectionId($databoxId, $collectionId);
|
||||||
$this->save($cacheKey, $collection);
|
$this->save($cacheKey, $collection);
|
||||||
|
} else {
|
||||||
|
$collection->hydrate($this->app);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $collection;
|
return $collection;
|
||||||
|
@@ -21,21 +21,27 @@ class CollectionFactory
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param int $databoxId
|
||||||
* @param CollectionReference $reference
|
* @param CollectionReference $reference
|
||||||
* @param array $row
|
* @param array $row
|
||||||
* @return \collection
|
* @return \collection
|
||||||
*/
|
*/
|
||||||
public function create(CollectionReference $reference, array $row)
|
public function create($databoxId, CollectionReference $reference, array $row)
|
||||||
{
|
{
|
||||||
|
if ($databoxId != $reference->getDataboxId()) {
|
||||||
|
throw new \InvalidArgumentException('Reference does not belong to given databoxId.');
|
||||||
|
}
|
||||||
|
|
||||||
return new \collection($this->app, $reference->getBaseId(), $reference, $row);
|
return new \collection($this->app, $reference->getBaseId(), $reference, $row);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param int $databoxId
|
||||||
* @param CollectionReference[] $collectionReferences
|
* @param CollectionReference[] $collectionReferences
|
||||||
* @param array $rows
|
* @param array $rows
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function createMany($collectionReferences, array $rows)
|
public function createMany($databoxId, $collectionReferences, array $rows)
|
||||||
{
|
{
|
||||||
Assertion::allIsInstanceOf($collectionReferences, CollectionReference::class);
|
Assertion::allIsInstanceOf($collectionReferences, CollectionReference::class);
|
||||||
|
|
||||||
@@ -47,7 +53,7 @@ class CollectionFactory
|
|||||||
}
|
}
|
||||||
|
|
||||||
foreach ($rows as $row) {
|
foreach ($rows as $row) {
|
||||||
$collections[$row['coll_id']] = $this->create($indexedReferences[$row['coll_id']], $row);
|
$collections[$row['coll_id']] = $this->create($databoxId, $indexedReferences[$row['coll_id']], $row);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $collections;
|
return $collections;
|
||||||
|
@@ -53,7 +53,7 @@ class DbalCollectionRepository implements CollectionRepository
|
|||||||
$query = self::$query . sprintf(' WHERE coll_id IN (%s)', implode(', ', array_keys($params)));
|
$query = self::$query . sprintf(' WHERE coll_id IN (%s)', implode(', ', array_keys($params)));
|
||||||
$rows = $connection->fetchAll($query, $params);
|
$rows = $connection->fetchAll($query, $params);
|
||||||
|
|
||||||
return $this->collectionFactory->createMany($references, $rows);
|
return $this->collectionFactory->createMany($databoxId, $references, $rows);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -74,7 +74,7 @@ class DbalCollectionRepository implements CollectionRepository
|
|||||||
$row = $connection->fetchAssoc($query, [ ':collectionId' => $reference->getCollectionId() ]);
|
$row = $connection->fetchAssoc($query, [ ':collectionId' => $reference->getCollectionId() ]);
|
||||||
|
|
||||||
if ($row !== false) {
|
if ($row !== false) {
|
||||||
return $this->collectionFactory->create($reference, $row);
|
return $this->collectionFactory->create($reference->getDataboxId(), $reference, $row);
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
@@ -99,7 +99,7 @@ class DbalCollectionRepository implements CollectionRepository
|
|||||||
$row = $connection->fetchAssoc($query, [ ':collectionId' => $reference->getCollectionId() ]);
|
$row = $connection->fetchAssoc($query, [ ':collectionId' => $reference->getCollectionId() ]);
|
||||||
|
|
||||||
if ($row !== false) {
|
if ($row !== false) {
|
||||||
return $this->collectionFactory->create($row['baseId'], $reference, $row);
|
return $this->collectionFactory->create($databoxId, $row['baseId'], $reference, $row);
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
@@ -158,7 +158,7 @@ class RepositoriesServiceProvider implements ServiceProviderInterface
|
|||||||
$factory
|
$factory
|
||||||
);
|
);
|
||||||
|
|
||||||
return new CachedCollectionRepository($repository, $app['cache'], 'collection_');
|
return new CachedCollectionRepository($app, $repository, $app['cache'], 'collection_');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -236,6 +236,16 @@ EOT;
|
|||||||
return $collection;
|
return $collection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Application
|
||||||
|
*/
|
||||||
|
protected $app;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var databox
|
||||||
|
*/
|
||||||
|
protected $databox;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var CollectionReference
|
* @var CollectionReference
|
||||||
*/
|
*/
|
||||||
@@ -261,21 +271,17 @@ EOT;
|
|||||||
*/
|
*/
|
||||||
protected $labels = [];
|
protected $labels = [];
|
||||||
|
|
||||||
/**
|
|
||||||
* @var databox
|
|
||||||
*/
|
|
||||||
protected $databox;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var int[]|string
|
* @var int[]|string
|
||||||
*/
|
*/
|
||||||
protected $binary_logo;
|
protected $binary_logo;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Application
|
* @param Application $app
|
||||||
|
* @param $baseId
|
||||||
|
* @param CollectionReference $reference
|
||||||
|
* @param array $row
|
||||||
*/
|
*/
|
||||||
protected $app;
|
|
||||||
|
|
||||||
public function __construct(Application $app, $baseId, CollectionReference $reference, array $row)
|
public function __construct(Application $app, $baseId, CollectionReference $reference, array $row)
|
||||||
{
|
{
|
||||||
$this->app = $app;
|
$this->app = $app;
|
||||||
@@ -295,6 +301,24 @@ EOT;
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function __sleep()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
'reference',
|
||||||
|
'name',
|
||||||
|
'preferences',
|
||||||
|
'pub_wm',
|
||||||
|
'labels',
|
||||||
|
'binary_logo'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function hydrate(Application $app)
|
||||||
|
{
|
||||||
|
$this->app = $app;
|
||||||
|
$this->databox = $app->getApplicationBox()->get_databox($this->reference->getDataboxId());
|
||||||
|
}
|
||||||
|
|
||||||
private function dispatch($eventName, CollectionEvent $event)
|
private function dispatch($eventName, CollectionEvent $event)
|
||||||
{
|
{
|
||||||
$this->app['dispatcher']->dispatch($eventName, $event);
|
$this->app['dispatcher']->dispatch($eventName, $event);
|
||||||
|
Reference in New Issue
Block a user