Cache-control improvements (#81)
* cache-control improvements * Update src/NXP/MathExecutor.php yeah, you're right. Co-authored-by: Alexander Kiryukhin <a.kiryukhin@mail.ru> * Update MathExecutor.php braces qfix * Update MathExecutor.php Co-authored-by: Alexander Kiryukhin <a.kiryukhin@mail.ru>
This commit is contained in:
parent
5ed72fda6f
commit
a4b0fac121
2 changed files with 50 additions and 5 deletions
|
@ -412,22 +412,26 @@ class MathExecutor
|
|||
/**
|
||||
* Execute expression
|
||||
*
|
||||
* @param $expression
|
||||
* @param string $expression
|
||||
* @param bool $cache
|
||||
* @return number
|
||||
* @throws Exception\IncorrectExpressionException
|
||||
* @throws Exception\IncorrectBracketsException
|
||||
* @throws Exception\IncorrectExpressionException
|
||||
* @throws Exception\UnknownOperatorException
|
||||
* @throws Exception\UnknownVariableException
|
||||
* @throws UnknownVariableException
|
||||
*/
|
||||
public function execute(string $expression)
|
||||
public function execute(string $expression, bool $cache = true)
|
||||
{
|
||||
$cachekey = $expression;
|
||||
if (!array_key_exists($cachekey, $this->cache)) {
|
||||
$tokens = (new Tokenizer($expression, $this->operators))->tokenize()->buildReversePolishNotation();
|
||||
$this->cache[$cachekey] = $tokens;
|
||||
if ($cache) {
|
||||
$this->cache[$cachekey] = $tokens;
|
||||
}
|
||||
} else {
|
||||
$tokens = $this->cache[$cachekey];
|
||||
}
|
||||
|
||||
$calculator = new Calculator($this->functions, $this->operators);
|
||||
return $calculator->calculate($tokens, $this->variables, $this->onVarNotFound);
|
||||
}
|
||||
|
@ -596,6 +600,23 @@ class MathExecutor
|
|||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cache array with tokens
|
||||
* @return array
|
||||
*/
|
||||
public function getCache() : array
|
||||
{
|
||||
return $this->cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear token's cache
|
||||
*/
|
||||
public function clearCache() : void
|
||||
{
|
||||
$this->cache = [];
|
||||
}
|
||||
|
||||
public function __clone()
|
||||
{
|
||||
$this->addDefaults();
|
||||
|
|
|
@ -589,4 +589,28 @@ class MathTest extends TestCase
|
|||
];
|
||||
}
|
||||
|
||||
public function testCache()
|
||||
{
|
||||
$calculator = new MathExecutor();
|
||||
$this->assertEquals(256, $calculator->execute('2 ^ 8')); // second arg $cache is true by default
|
||||
|
||||
$this->assertIsArray($calculator->getCache());
|
||||
$this->assertEquals(1, count($calculator->getCache()));
|
||||
|
||||
$this->assertEquals(512, $calculator->execute('2 ^ 9', true));
|
||||
$this->assertEquals(2, count($calculator->getCache()));
|
||||
|
||||
$this->assertEquals(1024, $calculator->execute('2 ^ 10', false));
|
||||
$this->assertEquals(2, count($calculator->getCache()));
|
||||
|
||||
$calculator->clearCache();
|
||||
$this->assertIsArray($calculator->getCache());
|
||||
$this->assertEquals(0, count($calculator->getCache()));
|
||||
|
||||
$this->assertEquals(2048, $calculator->execute('2 ^ 11', false));
|
||||
$this->assertEquals(0, count($calculator->getCache()));
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue