Add cache busting via save method of collection repository

This commit is contained in:
Thibaud Fabre
2015-07-09 19:00:34 +02:00
parent 977e778b61
commit c5cab178a9
5 changed files with 63 additions and 14 deletions

View File

@@ -16,4 +16,9 @@ interface CollectionRepository
*/ */
public function find($collectionId); public function find($collectionId);
/**
* @param \collection $collection
* @return void
*/
public function save(\collection $collection);
} }

View File

@@ -47,4 +47,13 @@ class ArrayCacheCollectionRepository implements CollectionRepository
return null; return null;
} }
public function save(\collection $collection)
{
$this->collectionRepository->save($collection);
if ($this->collectionCache !== null) {
$this->collectionCache[$collection->get_coll_id()] = $collection;
}
}
} }

View File

@@ -55,7 +55,7 @@ final class CachedCollectionRepository implements CollectionRepository
*/ */
public function findAll() public function findAll()
{ {
$cacheKey = hash('sha256', $this->cacheKey . '.findAll'); $cacheKey = hash('sha256', $this->cacheKey);
/** @var \collection[] $collections */ /** @var \collection[] $collections */
$collections = $this->cache->fetch($cacheKey); $collections = $this->cache->fetch($cacheKey);
@@ -86,6 +86,15 @@ final class CachedCollectionRepository implements CollectionRepository
return null; return null;
} }
public function save(\collection $collection)
{
$this->repository->save($collection);
$cacheKey = hash('sha256', $this->cacheKey);
$this->cache->delete($cacheKey);
}
private function putInCache($key, $value) private function putInCache($key, $value)
{ {
$this->cache->save($key, $value); $this->cache->save($key, $value);

View File

@@ -107,4 +107,9 @@ class DbalCollectionRepository implements CollectionRepository
return null; return null;
} }
public function save(\collection $collection)
{
}
} }

View File

@@ -10,6 +10,7 @@
*/ */
use Alchemy\Phrasea\Application; use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Collection\CollectionRepository;
use Alchemy\Phrasea\Collection\CollectionRepositoryRegistry; use Alchemy\Phrasea\Collection\CollectionRepositoryRegistry;
use Alchemy\Phrasea\Collection\Reference\CollectionReference; use Alchemy\Phrasea\Collection\Reference\CollectionReference;
use Alchemy\Phrasea\Collection\Reference\CollectionReferenceRepository; use Alchemy\Phrasea\Collection\Reference\CollectionReferenceRepository;
@@ -49,6 +50,19 @@ class collection implements cache_cacheableInterface, ThumbnailedElement
return $ord['ord'] ?: 1; return $ord['ord'] ?: 1;
} }
/**
* @param Application $app
* @param $databoxId
* @return CollectionRepository
*/
private static function getRepository(Application $app, $databoxId)
{
/** @var CollectionRepositoryRegistry $registry */
$registry = $app['repo.collections-registry'];
return $registry->getRepositoryByDatabox($databoxId);
}
public static function create(Application $app, databox $databox, appbox $appbox, $name, User $user = null) public static function create(Application $app, databox $databox, appbox $appbox, $name, User $user = null)
{ {
$sbas_id = $databox->get_sbas_id(); $sbas_id = $databox->get_sbas_id();
@@ -202,20 +216,27 @@ EOT;
$reference = $referenceRepository->find($base_id); $reference = $referenceRepository->find($base_id);
if (!$reference) { if (!$reference) {
throw new Exception_Databox_CollectionNotFound(sprintf("Collection with base_id %s could not be found", $base_id)); throw new Exception_Databox_CollectionNotFound(sprintf(
"Collection with base_id %s could not be found",
$base_id
));
} }
/** @var CollectionRepositoryRegistry $registry */ $repository = self::getRepository($app, $reference->getDataboxId());
$registry = $app['repo.collections-registry'];
$repository = $registry->getRepositoryByDatabox($reference->getDataboxId());
$collection = $repository->find($reference->getCollectionId()); $collection = $repository->find($reference->getCollectionId());
if (!$collection) { if (!$collection) {
throw new Exception_Databox_CollectionNotFound(sprintf("Collection with base_id %s could not be found", $base_id)); throw new Exception_Databox_CollectionNotFound(sprintf(
"Collection with base_id %s could not be found",
$base_id
));
} }
if (!$app['conf.restrictions']->isCollectionAvailable($collection)) { if (!$app['conf.restrictions']->isCollectionAvailable($collection)) {
throw new Exception_Databox_CollectionNotFound('Collection `' . $collection->get_base_id() . '` is not available here.'); throw new Exception_Databox_CollectionNotFound(sprintf(
'Collection `%d` is not available here.',
$collection->get_base_id()
));
} }
return $collection; return $collection;
@@ -231,9 +252,7 @@ EOT;
{ {
assert(is_int($coll_id)); assert(is_int($coll_id));
/** @var CollectionRepositoryRegistry $registry */ $repository = self::getRepository($app, $databox->get_sbas_id());
$registry = $app['repo.collections-registry'];
$repository = $registry->getRepositoryByDatabox($databox->get_sbas_id());
$collection = $repository->find($coll_id); $collection = $repository->find($coll_id);
if (!$collection) { if (!$collection) {
@@ -246,8 +265,8 @@ EOT;
if (!$app['conf.restrictions']->isCollectionAvailable($collection)) { if (!$app['conf.restrictions']->isCollectionAvailable($collection)) {
throw new Exception_Databox_CollectionNotFound(sprintf( throw new Exception_Databox_CollectionNotFound(sprintf(
'Collection `%d` is not available here.', 'Collection `%d` is not available here.',
$collection->get_base_id()) $collection->get_base_id()
); ));
} }
return $collection; return $collection;
@@ -814,6 +833,8 @@ EOT;
public function delete_data_from_cache($option = null) public function delete_data_from_cache($option = null)
{ {
self::getRepository($this->app, $this->reference->getDataboxId())->save($this);
return $this->databox->delete_data_from_cache($this->get_cache_key($option)); return $this->databox->delete_data_from_cache($this->get_cache_key($option));
} }