mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-29 05:28:44 +02:00
Merge remote-tracking branch 'origin/support/3.2' into develop
This commit is contained in:
@@ -26,6 +26,8 @@ use Symfony\Component\CssSelector\XPath\Translator;
|
||||
*/
|
||||
class CssSelectorConverter
|
||||
{
|
||||
public static int $maxCachedItems = 1024;
|
||||
|
||||
private Translator $translator;
|
||||
private array $cache;
|
||||
|
||||
@@ -62,6 +64,21 @@ class CssSelectorConverter
|
||||
*/
|
||||
public function toXPath(string $cssExpr, string $prefix = 'descendant-or-self::'): string
|
||||
{
|
||||
return $this->cache[$prefix][$cssExpr] ??= $this->translator->cssToXPath($cssExpr, $prefix);
|
||||
$cacheKey = $prefix."\0".$cssExpr;
|
||||
|
||||
if (isset($this->cache[$cacheKey])) {
|
||||
// Move the item last in cache (LRU)
|
||||
$value = $this->cache[$cacheKey];
|
||||
unset($this->cache[$cacheKey]);
|
||||
|
||||
return $this->cache[$cacheKey] = $value;
|
||||
}
|
||||
|
||||
if (\count($this->cache) >= self::$maxCachedItems) {
|
||||
// Evict the oldest entry
|
||||
unset($this->cache[array_key_first($this->cache)]);
|
||||
}
|
||||
|
||||
return $this->cache[$cacheKey] = $this->translator->cssToXPath($cssExpr, $prefix);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user