diff --git a/lib/Service/FavoritesService.php b/lib/Service/FavoritesService.php index d4737f4b4..5e01cf997 100644 --- a/lib/Service/FavoritesService.php +++ b/lib/Service/FavoritesService.php @@ -37,6 +37,7 @@ class FavoritesService { private IDBConnection $connection; private PermissionsService $permissionsService; private ?string $userId; + private bool $cached = false; private CappedMemoryCache $cache; public function __construct( @@ -53,27 +54,22 @@ public function __construct( public function isFavorite(int $nodeType, int $id): bool { $cacheKey = $nodeType . '_' . $id; - if ($cached = $this->cache->get($cacheKey)) { - return $cached; - } - - $this->cache->clear(); - $qb = $this->connection->getQueryBuilder(); - $qb->select('*') - ->from('tables_favorites') - ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($this->userId))); - - $result = $qb->executeQuery(); - while ($row = $result->fetch()) { - $this->cache->set($row['node_type'] . '_' . $row['node_id'], true); - } - // Set the cache for not found matches still to avoid further queries - if (!$this->cache->get($cacheKey)) { - $this->cache->set($cacheKey, false); + if (!$this->cached) { + $this->cached = true; + $this->cache->clear(); + $qb = $this->connection->getQueryBuilder(); + $qb->select('*') + ->from('tables_favorites') + ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($this->userId))); + + $result = $qb->executeQuery(); + while ($row = $result->fetch()) { + $this->cache->set($cacheKey, true); + } } - return $this->cache->get($cacheKey); + return (bool)$this->cache->get($cacheKey); } /**