aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMirosław Sztorc <msztorc@users.noreply.github.com>2021-01-06 03:06:04 +0300
committerGitHub <noreply@github.com>2021-01-06 03:06:04 +0300
commita4b0fac121b19a3e44104b89af50d33979095b6c (patch)
tree4c7d2aadd59038bd830d369bac3c015878726419 /src
parent5ed72fda6f80e862162999a0442f6ea97c91af56 (diff)
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>
Diffstat (limited to 'src')
-rw-r--r--src/NXP/MathExecutor.php31
1 files changed, 26 insertions, 5 deletions
diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php
index 1debc50..ca27539 100644
--- a/src/NXP/MathExecutor.php
+++ b/src/NXP/MathExecutor.php
@@ -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();