mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-18 15:33:15 +00:00
Add array cache repository to reduce load on cache backend
This commit is contained in:
@@ -22,6 +22,7 @@ use Alchemy\Phrasea\Collection\Reference\DbalCollectionReferenceRepository;
|
||||
use Alchemy\Phrasea\Collection\Repository\ArrayCacheCollectionRepository;
|
||||
use Alchemy\Phrasea\Collection\Repository\CachedCollectionRepository;
|
||||
use Alchemy\Phrasea\Collection\Repository\DbalCollectionRepository;
|
||||
use Alchemy\Phrasea\Databox\ArrayCacheDataboxRepository;
|
||||
use Alchemy\Phrasea\Databox\CachingDataboxRepositoryDecorator;
|
||||
use Alchemy\Phrasea\Databox\DataboxConnectionProvider;
|
||||
use Alchemy\Phrasea\Databox\DataboxFactory;
|
||||
@@ -156,7 +157,7 @@ class RepositoriesServiceProvider implements ServiceProviderInterface
|
||||
|
||||
$factory->setDataboxRepository($repository);
|
||||
|
||||
return $repository;
|
||||
return new ArrayCacheDataboxRepository($repository);
|
||||
});
|
||||
|
||||
$app['repo.fields.factory'] = $app->protect(function (\databox $databox) use ($app) {
|
||||
|
73
lib/Alchemy/Phrasea/Databox/ArrayCacheDataboxRepository.php
Normal file
73
lib/Alchemy/Phrasea/Databox/ArrayCacheDataboxRepository.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace Alchemy\Phrasea\Databox;
|
||||
|
||||
class ArrayCacheDataboxRepository implements DataboxRepository
|
||||
{
|
||||
/**
|
||||
* @var DataboxRepository
|
||||
*/
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $loaded = false;
|
||||
|
||||
/**
|
||||
* @var \databox[]
|
||||
*/
|
||||
private $databoxes = [];
|
||||
|
||||
/**
|
||||
* @param DataboxRepository $repository
|
||||
*/
|
||||
public function __construct(DataboxRepository $repository)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @return \databox
|
||||
*/
|
||||
public function find($id)
|
||||
{
|
||||
$this->load();
|
||||
|
||||
if (! isset($this->databoxes[$id])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->databoxes[$id];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \databox[]
|
||||
*/
|
||||
public function findAll()
|
||||
{
|
||||
$this->load();
|
||||
|
||||
return $this->databoxes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \databox $databox
|
||||
*/
|
||||
public function save(\databox $databox)
|
||||
{
|
||||
$this->loaded = false;
|
||||
$this->databoxes = [];
|
||||
|
||||
return $this->repository->save($databox);
|
||||
}
|
||||
|
||||
private function load()
|
||||
{
|
||||
if (! $this->loaded) {
|
||||
$this->databoxes = $this->repository->findAll();
|
||||
$this->loaded = true;
|
||||
}
|
||||
}
|
||||
}
|
@@ -22,11 +22,17 @@ final class CachingDataboxRepositoryDecorator implements DataboxRepository
|
||||
/** @var DataboxFactory */
|
||||
private $factory;
|
||||
|
||||
/**
|
||||
* @param DataboxRepository $repository
|
||||
* @param Cache $cache
|
||||
* @param string $cacheKey
|
||||
* @param DataboxFactory $factory
|
||||
*/
|
||||
public function __construct(DataboxRepository $repository, Cache $cache, $cacheKey, DataboxFactory $factory)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
$this->cache = $cache;
|
||||
$this->cacheKey = $cacheKey;
|
||||
$this->cacheKey = 'databoxes:' . hash('sha256', $cacheKey);
|
||||
$this->factory = $factory;
|
||||
}
|
||||
|
||||
@@ -60,7 +66,7 @@ final class CachingDataboxRepositoryDecorator implements DataboxRepository
|
||||
{
|
||||
$this->cache->delete($this->cacheKey);
|
||||
|
||||
$this->repository->save($databox);
|
||||
return $this->repository->save($databox);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user