Cache creation of indexes and types in Elastica to avoid recreation

This commit is contained in:
Tim Nagel 2014-06-18 16:48:00 +10:00
commit afbaf875b9
2 changed files with 29 additions and 1 deletions

View file

@ -14,6 +14,13 @@ use FOS\ElasticaBundle\Logger\ElasticaLogger;
*/
class Client extends BaseClient
{
/**
* Stores created indexes to avoid recreation.
*
* @var array
*/
private $indexCache = array();
/**
* {@inheritdoc}
*/
@ -42,6 +49,10 @@ class Client extends BaseClient
public function getIndex($name)
{
return new Index($this, $name);
if (isset($this->indexCache[$name])) {
return $this->indexCache[$name];
}
return $this->indexCache[$name] = new Index($this, $name);
}
}

View file

@ -3,6 +3,7 @@
namespace FOS\ElasticaBundle\Elastica;
use Elastica\Index as BaseIndex;
use Elastica\Type;
/**
* Overridden Elastica Index class that provides dynamic index name changes.
@ -13,6 +14,13 @@ class Index extends BaseIndex
{
private $originalName;
/**
* Stores created types to avoid recreation.
*
* @var array
*/
private $typeCache = array();
/**
* Returns the original name of the index if the index has been renamed for reindexing
* or realiasing purposes.
@ -24,6 +32,15 @@ class Index extends BaseIndex
return $this->originalName ?: $this->_name;
}
public function getType($type)
{
if (isset($this->typeCache[$type])) {
return $this->typeCache[$type];
}
return $this->typeCache[$type] = parent::getType($type);
}
/**
* Reassign index name
*