Skip to content

Commit

Permalink
Cache-control improvements (#81)
Browse files Browse the repository at this point in the history
* cache-control improvements

* Update src/NXP/MathExecutor.php

yeah, you're right.

Co-authored-by: Alexander Kiryukhin <[email protected]>

* Update MathExecutor.php

braces qfix

* Update MathExecutor.php

Co-authored-by: Alexander Kiryukhin <[email protected]>
  • Loading branch information
msztorc and neonxp authored Jan 6, 2021
1 parent 5ed72fd commit a4b0fac
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/NXP/MathExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,22 +412,26 @@ protected function defaultFunctions() : array
/**
* 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);
}
Expand Down Expand Up @@ -596,6 +600,23 @@ public function setDivisionByZeroIsZero() : self
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();
Expand Down
24 changes: 24 additions & 0 deletions tests/MathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -589,4 +589,28 @@ public function providerExpressionValues()
];
}

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()));


}

}

0 comments on commit a4b0fac

Please sign in to comment.