mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-18 07:23:13 +00:00
Add array cache repository to reduce load on cache backend
This commit is contained in:
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;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user