-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(Nodes): Fixed node offspring resolution cache with new `NodeOffsp…
…ringResolverInterface` service
- Loading branch information
1 parent
11a80e4
commit cf62690
Showing
11 changed files
with
160 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
lib/RoadizCoreBundle/src/EventSubscriber/CachedNodeOffspringEventSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RZ\Roadiz\CoreBundle\EventSubscriber; | ||
|
||
use RZ\Roadiz\CoreBundle\Event\FilterNodeEvent; | ||
use RZ\Roadiz\CoreBundle\Event\Node\NodeCreatedEvent; | ||
use RZ\Roadiz\CoreBundle\Event\Node\NodeDuplicatedEvent; | ||
use RZ\Roadiz\CoreBundle\Event\Node\NodeUpdatedEvent; | ||
use RZ\Roadiz\CoreBundle\Node\CachedNodeOffspringResolverInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
class CachedNodeOffspringEventSubscriber implements EventSubscriberInterface | ||
{ | ||
public function __construct(private readonly CachedNodeOffspringResolverInterface $cachedNodeOffspringResolver) | ||
{ | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
NodeCreatedEvent::class => 'invalidateNodeOffspringCache', | ||
NodeDuplicatedEvent::class => 'invalidateNodeOffspringCache', | ||
NodeUpdatedEvent::class => 'invalidateNodeOffspringCache', | ||
]; | ||
} | ||
|
||
public function invalidateNodeOffspringCache(FilterNodeEvent $event): void | ||
{ | ||
$this->cachedNodeOffspringResolver->purgeOffspringCache($event->getNode()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
lib/RoadizCoreBundle/src/Node/CachedNodeOffspringResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RZ\Roadiz\CoreBundle\Node; | ||
|
||
use Doctrine\Persistence\ManagerRegistry; | ||
use Psr\Cache\CacheException; | ||
use Psr\Cache\CacheItemPoolInterface; | ||
use Psr\Cache\InvalidArgumentException; | ||
use RZ\Roadiz\CoreBundle\Entity\Node; | ||
use Symfony\Contracts\Cache\ItemInterface; | ||
use Symfony\Contracts\Cache\TagAwareCacheInterface; | ||
|
||
final class CachedNodeOffspringResolver implements CachedNodeOffspringResolverInterface | ||
{ | ||
public function __construct( | ||
private readonly CacheItemPoolInterface $cache, | ||
private readonly ManagerRegistry $managerRegistry | ||
) { | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
* @throws InvalidArgumentException | ||
* @throws CacheException | ||
*/ | ||
public function getAllOffspringIds(Node $ancestor): array | ||
{ | ||
$cacheItem = $this->cache->getItem(self::CACHE_PREFIX . $ancestor->getId()); | ||
if (!$cacheItem->isHit()) { | ||
$nodeRepository = $this->managerRegistry->getRepository(Node::class); | ||
$offspringIds = $nodeRepository->findAllOffspringIdByNode($ancestor); | ||
$cacheItem->set($offspringIds); | ||
$cacheItem->expiresAfter(300); | ||
if ($cacheItem instanceof ItemInterface && $this->cache instanceof TagAwareCacheInterface) { | ||
$cacheItem->tag(array_map(function (int $nodeId) { | ||
return self::CACHE_TAG_PREFIX . $nodeId; | ||
}, $offspringIds)); | ||
} | ||
$this->cache->save($cacheItem); | ||
} else { | ||
$offspringIds = $cacheItem->get(); | ||
} | ||
return $offspringIds; | ||
} | ||
|
||
/** | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function purgeOffspringCache(Node $node): void | ||
{ | ||
$this->cache->deleteItem(self::CACHE_PREFIX . $node->getId()); | ||
if ($this->cache instanceof TagAwareCacheInterface) { | ||
/* | ||
* If cache pool supports tags, we can invalidate all nodes at once. | ||
*/ | ||
$this->cache->invalidateTags([self::CACHE_TAG_PREFIX . $node->getId()]); | ||
} else { | ||
$ancestorsId = $this->managerRegistry | ||
->getRepository(Node::class) | ||
->findAllParentsIdByNode($node); | ||
foreach ($ancestorsId as $ancestorId) { | ||
$this->cache->deleteItem(self::CACHE_PREFIX . $ancestorId); | ||
} | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
lib/RoadizCoreBundle/src/Node/CachedNodeOffspringResolverInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RZ\Roadiz\CoreBundle\Node; | ||
|
||
use RZ\Roadiz\CoreBundle\Entity\Node; | ||
|
||
interface CachedNodeOffspringResolverInterface extends NodeOffspringResolverInterface | ||
{ | ||
public const CACHE_PREFIX = 'node_offspring_ids_'; | ||
public const CACHE_TAG_PREFIX = 'node_'; | ||
public function purgeOffspringCache(Node $node): void; | ||
} |
16 changes: 16 additions & 0 deletions
16
lib/RoadizCoreBundle/src/Node/NodeOffspringResolverInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RZ\Roadiz\CoreBundle\Node; | ||
|
||
use RZ\Roadiz\CoreBundle\Entity\Node; | ||
|
||
interface NodeOffspringResolverInterface | ||
{ | ||
/** | ||
* @param Node $ancestor | ||
* @return array<int> | ||
*/ | ||
public function getAllOffspringIds(Node $ancestor): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters